You are on page 1of 26

# CC421 - Computer Engineering Dept.

## Executing and Linking an assembly

program
Lecturer: Dr. Hesham El Zouka

## 2009 Dr. Hesham EL Zouka

Lesson plan
 Review
 Program logic and control
 Practice exercise
 Assembling, Linking and Executing Programs
 Practice exercise

## 2009 Dr. Hesham EL Zouka

Review
 IMUL register/memory

If operand is a byte
AX
AX=
= AL * operand

Review
MOV CX, 10
BEGINLOOP :
.
DEC CX
JNZ BEGINLOOP
OR
MOV CX, 10
BEGINLOOP :

LOOP BEGINLOOP



## Near: -32768 to 32767 bytes

Far: distance over 32K for the same segment address or in another
segment

## 2009 Dr. Hesham EL Zouka

Transfer operations
 JMP

Unconditional
Syntax::
Syntax
[label:] JMP

## 2009 Dr. Hesham EL Zouka

Transfer operations
 Backward jump

BEGINLOOP:
BEGINLOOP:
.
JMP BEGINLOOP
 Forward jump

JMP STARTLOOP
STARTLOOP:
STARTLOOP:
.

## 2009 Dr. Hesham EL Zouka

Loop instruction
 Loops a specified number of times

## Initial value is stored in CX

Each iteration, LOOP deducts 1 from CX.
If CX is not zero, control jumps to the address specified
Otherwise, finish the loop
 Syntax:

## 2009 Dr. Hesham EL Zouka

CMP Instruction
 [label:] CMP register/memory,

register/memory/immediate

##  Compares the first to the second operand

 Affects: AF, CF, OF, PF, SF and ZF flag

JE Startloop

## Conditional Jump instructions

 Jump based on unsigned data

[label:] JE/JZ

short

[label:] JNE/JNZ

Flag:: ZF
Flag

Example
MOV AL, 5
CMP AL, 5
JE label1
label1
JMP exit
label1
label
1: MOV CX, BX
exit:

..

## Conditional Jump instructions

JG: Jump if first operand is Greater then second operand (as set by CMP
instruction). Signed.
if (ZF = 0) and (SF = OF) then jump
Syntax: [label:] JG

## 2009 Dr. Hesham EL Zouka

Example
MOV AL, 5
CMP AL, -5
JG label1
label1
JMP exit
label1
label
1: MOV CX, -5 ; in this case AL > -5
exit::
exit

## Conditional Jump Instruction

 JL: Jump if first operand is Less then second operand (as set by CMP

instruction). Signed.

##  if SF <> OF then jump

 Syntax: [label:] JL short

Example
MOV AL, -2
CMP AL, 5
JL label1
label1
JMP exit
label1
label
1: MOV CX, 5
exit::
exit

## Conditional Jump instructions

 JB/JNAE

 Jump Above/Equal or Jump Not Above/Equal

Flag:: ZF
Flag

e.g. Write a program to compute sum of even integers from 1 to 100 using LOOP

 JBE/JNA

[label:] JBE/JNA

short

Flag: AF, ZF

## Special Arithmetic Test

 JCXZ: Jump if CX is zero
 JC: Jump if carry
 JNC: Jump if no carry
 JO: Jump if overflow
 JNP: Jump if no overflow

e.g. Write a program that adds each value defined in BYTE_TBL and store
the sum in BYTE_TOTAL
BYTE_TBL

DB

BYTE_TOTAL DB

0,5,6,4,9,7
0

## Assembling, Linking and Executing Programs

Assembling: translate source program (written in assembly language) into
machine code (object code)
Linking: complete machine code for the object program, generate an
executable module

## 2009 Dr. Hesham EL Zouka

Executing Programs

Create *.asm
Editor

Assembler

Assemble *.asm
And create *.obj

And create *.exe

## Assembling a Source Program

 Converts your source code into machine code and displays any errors.

Assembling
*.asm

## 2009 Dr. Hesham EL Zouka

 Combines more than one assembled module into one executable

program

 *.map

NAME CLASS

00000H
00000
H 0003FH
0003FH 0040H
0040H

STACK STACK

00040H
00040
H 00045H
00045H 0006H
0006H

DATASEG DATA

00050H
00050
H 00063H
00063H 0014H
0014H

CODESEGCODE

0005:0000

## 2009 Dr. Hesham EL Zouka

Procedures
 Procedure is a part of code that can be called from your

## program in order to make some specific task.

 Procedure makes program more structural and easier to

understand.
 Returns to the same point from where it was called.

Syntax:
name PROC
; the code of the procedure ...
RET
name ENDP

## 2009 Dr. Hesham EL Zouka

Procedure Call
 CALL instruction is used to call a procedure
 CALL is used to transfer controls
 RET is used to end execution of procedure

Syntax:

## [label:] CALL procedure

[label:] RET [immediate]

Example
CALL m1
m1
MOV AX, 2
m1

PROC
MOV BX, 5
CALL m2
m2

m1

ENDP

m2

PROC
MOV CX, 10