26
Micro processors Systems Executing and Linking an assembly Executing and Linking an assembly program program Lecturer: Dr. Lecturer: Dr. Hesham Hesham El El Zouka Zouka CC421 - Computer Engineering Dept.

Lecture 4 Executing an assembly program.pdf

Embed Size (px)

Citation preview

Page 1: Lecture 4 Executing an assembly program.pdf

Micro processors Systems

Executing and Linking an assembly Executing and Linking an assembly

programprogram

Lecturer: Dr. Lecturer: Dr. HeshamHesham El El ZoukaZouka

CC421 - Computer Engineering Dept.

Page 2: Lecture 4 Executing an assembly program.pdf

Lesson plan

�� Review Review

�� Program logic and controlProgram logic and control

�� Practice exercisePractice exercise

�� Assembling, Linking and Executing ProgramsAssembling, Linking and Executing Programs

�� Practice exercisePractice exercise

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 3: Lecture 4 Executing an assembly program.pdf

Review

�� IMUL register/memoryIMUL register/memory

If If operand is a byteoperand is a byte

AXAX= = AL * operandAL * operand

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 4: Lecture 4 Executing an assembly program.pdf

Review

MOV CX, MOV CX, 1010

BEGINLOOP :BEGINLOOP :

….….

DEC CXDEC CX

JNZ BEGINLOOPJNZ BEGINLOOP

OROR

MOV CX,MOV CX, 1010

BEGINLOOP :BEGINLOOP :

……

LOOP BEGINLOOPLOOP BEGINLOOP

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 5: Lecture 4 Executing an assembly program.pdf

Program logic and control

�� Address: distance from the current Address: distance from the current addressaddress

� 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

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 6: Lecture 4 Executing an assembly program.pdf

Transfer operations

�� JMPJMP

UnconditionalUnconditional. . Transfer control under all circumstancesTransfer control under all circumstances

SyntaxSyntax::

[[label:] JMPlabel:] JMP short/near/far addressshort/near/far address

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 7: Lecture 4 Executing an assembly program.pdf

Transfer operations

�� Backward jumpBackward jump

BEGINLOOPBEGINLOOP::

….….

JMP JMP BEGINLOOPBEGINLOOP

�� Forward Forward jumpjump

JMP JMP STARTLOOPSTARTLOOP

STARTLOOPSTARTLOOP::

….….

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 8: Lecture 4 Executing an assembly program.pdf

Loop instruction

�� Loops a specified number of times Loops a specified number of times

Initial value is stored in CXInitial value is stored in CX

Each iteration, LOOP deducts Each iteration, LOOP deducts 1 1 from CX.from CX.

If CX is not zero, control jumps to the address specifiedIf CX is not zero, control jumps to the address specified

Otherwise,Otherwise, finish the finish the looploop

�� Syntax:Syntax:

[label:] LOOP short-address

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 9: Lecture 4 Executing an assembly program.pdf

CMP Instruction

�� [[label:] CMP register/memory, label:] CMP register/memory, register/memory/immediateregister/memory/immediate

�� Compares the first to the second Compares the first to the second operandoperand

�� Affects: AF,Affects: AF, CF, OF, PF, SF and ZF flagCF, OF, PF, SF and ZF flag

e.g.e.g. CMP AX, DXCMP AX, DX

JE JE StartloopStartloop

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 10: Lecture 4 Executing an assembly program.pdf

Conditional Jump instructions

�� Jump based on unsigned dataJump based on unsigned data

[[label:] JE/JZlabel:] JE/JZ shortshort--addressaddress

�� Jump if equal or Jump if zeroJump if equal or Jump if zero

[[label:] JNE/JNZlabel:] JNE/JNZ shortshort--addressaddress

�� Jump Jump if not equal or Jump if not zeroif not equal or Jump if not zero

FlagFlag: : ZFZF

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 11: Lecture 4 Executing an assembly program.pdf

Example

MOV AL, MOV AL, 5 5

CMP AL, CMP AL, 5 5

JE labelJE label1 1

JMP exit JMP exit

labellabel11: : MOV CX, BXMOV CX, BX

exit: exit: …..…..

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 12: Lecture 4 Executing an assembly program.pdf

Conditional Jump instructions

JG: Jump if first operand is Greater then second operand (as set by CMP JG: Jump if first operand is Greater then second operand (as set by CMP instruction). instruction). “Signed”. “Signed”.

if (ZF = if (ZF = 00) and (SF = OF) then jump ) and (SF = OF) then jump

Syntax:Syntax: [label:] JG[label:] JG shortshort--addressaddress

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 13: Lecture 4 Executing an assembly program.pdf

Example

MOV AL, MOV AL, 5 5

CMP AL, CMP AL, --5 5

JG labelJG label1 1

JMP exit JMP exit

labellabel11: MOV CX, : MOV CX, --5 5 ; in this case AL > ; in this case AL > --5 5

exitexit: :

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 14: Lecture 4 Executing an assembly program.pdf

Conditional Jump Instruction

�� JL: Jump if first operand is Less then second operand (as set by CMP JL: Jump if first operand is Less then second operand (as set by CMP instruction). Signed. instruction). Signed.

�� if SF <> OF then jump if SF <> OF then jump

�� Syntax: [label:] Syntax: [label:] JL shortJL short--addressaddress

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 15: Lecture 4 Executing an assembly program.pdf

Example

MOV AL,MOV AL, --2 2

CMP AL, CMP AL, 5 5

JL labelJL label1 1

JMP exit JMP exit

labellabel11: MOV CX, : MOV CX, 55 ; ; in this case AL < in this case AL < 55

exitexit: …: …

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 16: Lecture 4 Executing an assembly program.pdf

Conditional Jump instructions

�� JB/JNAEJB/JNAE

[label:] [label:] JB/JNAE shortJB/JNAE short--addressaddress

�� Jump Above/Equal or Jump Not Above/EqualJump Above/Equal or Jump Not Above/Equal

FlagFlag: : ZFZF

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

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 17: Lecture 4 Executing an assembly program.pdf

Conditional Jump instructions

�� JBE/JNAJBE/JNA

[[label:]label:] JBE/JNAJBE/JNA shortshort--addressaddress

�� Jump Below/Equal or Jump Not AboveJump Below/Equal or Jump Not Above

Flag: AF,Flag: AF, ZFZF

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 18: Lecture 4 Executing an assembly program.pdf

Special Arithmetic Test

�� JCXZ: Jump if CX is JCXZ: Jump if CX is zerozero

�� JC: Jump if JC: Jump if carrycarry

�� JNC: Jump if no JNC: Jump if no carrycarry

�� JO: Jump if JO: Jump if overflowoverflow

�� JNP: Jump if no JNP: Jump if no overflowoverflow

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

the sum in BYTE_TOTALthe sum in BYTE_TOTAL

BYTE_TBL BYTE_TBL DB DB 00,,55,,66,,44,,99,,77

BYTE_TOTAL BYTE_TOTAL DB DB 00

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 19: Lecture 4 Executing an assembly program.pdf

Assembling, Linking and Executing Programs

Assembling: translate Assembling: translate source source program (written in assembly language) into program (written in assembly language) into

machine machine code (object code) code (object code)

Linking: complete machine code for the object program, generate an Linking: complete machine code for the object program, generate an

executable executable modulemodule

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 20: Lecture 4 Executing an assembly program.pdf

Executing Programs

Editor

Create *.asm

Assemble *.asm

And create *.obj

Assembler

Link

And create *.exe

Linker

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 21: Lecture 4 Executing an assembly program.pdf

Assembling a Source Program

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

*.asm *.obj, *.lst, *.crf

Assembling

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 22: Lecture 4 Executing an assembly program.pdf

Linking an Object Program

�� Combines more than one assembled module into one executable Combines more than one assembled module into one executable programprogram

�� Generates an EXE module and initializes it with special instructions to Generates an EXE module and initializes it with special instructions to facilitate its subsequent loading for executionfacilitate its subsequent loading for execution

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 23: Lecture 4 Executing an assembly program.pdf

Linking an Object Program

�� *.map *.map

STARTSTART STOPSTOP LENGTH NAME LENGTH NAME CLASSCLASS

0000000000HH 00030003FH FH 00400040H H STACK STACK STACKSTACK

0004000040H H 0004500045H H 00060006H H DATASEG DATASEG DATADATA

0005000050H H 0006300063H H 00140014H H CODESEGCODECODESEGCODE

Program entry at Program entry at 00050005::00000000

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 24: Lecture 4 Executing an assembly program.pdf

Procedures

�� Procedure is a part of code that can be called from your Procedure is a part of code that can be called from your program in order to make some specific task. program in order to make some specific task.

�� Procedure makes Procedure makes program more structural and easier to program more structural and easier to understand. understand.

�� Returns Returns to the same point from where it was called. to the same point from where it was called.

Syntax: Syntax:

name PROCname PROC

; ; the code of the procedure ...the code of the procedure ...

RETRETname ENDP name ENDP

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 25: Lecture 4 Executing an assembly program.pdf

Procedure Call

�� CALL instruction is used to call a procedure CALL instruction is used to call a procedure

�� CALL is used to transfer controlsCALL is used to transfer controls

�� RET is used to end execution of procedureRET is used to end execution of procedure

Syntax:Syntax: [label:] CALL [label:] CALL procedureprocedure

[[label:]label:] RET [immediate]RET [immediate]

20

09

Dr. H

es

ha

mE

L Z

ou

ka

Page 26: Lecture 4 Executing an assembly program.pdf

Example

CALL mCALL m1 1

MOV AX, MOV AX, 2 2

RET ; return to operating system.RET ; return to operating system.

mm1 1 PROC PROC

MOV BX, MOV BX, 5 5

CALL mCALL m22

RET ; return to caller. RET ; return to caller.

mm1 1 ENDP ENDP

mm22 PROCPROC

MOV CX, MOV CX, 1010

RET;RET; return to callerreturn to caller

mm22 ENDPENDP

20

09

Dr. H

es

ha

mE

L Z

ou

ka