You are on page 1of 26

CC421 - Computer Engineering Dept.

Micro processors Systems

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

2009 Dr. Hesham EL Zouka

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

LOOP BEGINLOOP

2009 Dr. Hesham EL Zouka

Program logic and control


 Address: distance from the current address


Short: distance from -128 to 127 bytes

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.. Transfer control under all circumstances


Unconditional
Syntax::
Syntax
[label:] JMP

short/near/far address

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:

[label:] LOOP short-address

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

e.g. CMP AX, DX


JE Startloop

2009 Dr. Hesham EL Zouka

Conditional Jump instructions


 Jump based on unsigned data

[label:] JE/JZ

short
short--address

 Jump if equal or Jump if zero

[label:] JNE/JNZ

shortshort-address

 Jump if not equal or Jump if not zero

Flag:: ZF
Flag

2009 Dr. Hesham EL Zouka

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

..

2009 Dr. Hesham EL Zouka

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

shortshort-address

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

2009 Dr. Hesham EL Zouka

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
short--address

2009 Dr. Hesham EL Zouka

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

; in this case AL < 5

2009 Dr. Hesham EL Zouka

Conditional Jump instructions


 JB/JNAE

[label:] JB/JNAE shortshort-address


 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

2009 Dr. Hesham EL Zouka

Conditional Jump instructions


 JBE/JNA

[label:] JBE/JNA

short
short--address

 Jump Below/Equal or Jump Not Above

Flag: AF, ZF

2009 Dr. Hesham EL Zouka

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

2009 Dr. Hesham EL Zouka

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

Linker

Link
And create *.exe

2009 Dr. Hesham EL Zouka

Assembling a Source Program


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

Assembling
*.asm

*.obj, *.lst, *.crf

2009 Dr. Hesham EL Zouka

Linking an Object Program


 Combines more than one assembled module into one executable

program

 Generates an EXE module and initializes it with special instructions to

facilitate its subsequent loading for execution

2009 Dr. Hesham EL Zouka

Linking an Object Program


 *.map

START STOP LENGTH

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

Program entry at 0005:


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]

2009 Dr. Hesham EL Zouka

Example
CALL m1
m1
MOV AX, 2
RET ; return to operating system.
m1

PROC
MOV BX, 5
CALL m2
m2
RET ; return to caller.

m1

ENDP

m2

PROC
MOV CX, 10
RET; return to caller

m2

ENDP