39
Executing and Linking an assembly program

Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Embed Size (px)

Citation preview

Page 1: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Executing and Linking an assembly program

Page 2: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Lesson plan

Review Program logic and controlPractice exerciseAssembling, Linking and Executing

ProgramsPractice exercise

Page 3: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Review

IMUL register/memory

If operand is a byte

AX= AL * operand

Page 4: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Review

MOV CX, 10BEGINLOOP

DEC CXJNZ BEGINLOOP

OR

MOV CX, 10BEGINLOOP

LOOP BEGINLOOP

Page 5: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Program logic and control

Address: distance from the current addressShort: distance from -128 to 127 bytesNear: -32768 to 32767 bytesFar: distance over 32K for the same segment

address or in another segment

Page 6: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Transfer operations

JMP

Unconditional. Transfer control under all circumstances

Syntax:

[label:] JMP short/near/far address

Page 7: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Transfer operations

Backward jump

BEGINLOOP:

….

JMP BEGINLOOP

Forward jump

JMP STARTLOOP

STARTLOOP:

….

Page 8: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Practice with JMP instruction

Write a program to continuously add 1 to AX and add AX to BX

(Initially, assign AX and BX any values)

Page 9: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

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 loopSyntax:

[label:] LOOP short-address

Page 10: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

CMP Instruction

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

Compares the first to the second operandAffects: AF, CF, OF, PF, SF and ZF flag

CMP AX, DX

JE Startloop

Page 11: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Conditional Jump instructions

Jump based on unsigned data

[label:] JE/JZ short-address

Jump if equal or Jump if zero

[label:] JNE/JNZ short-address

Jump if not equal or Jump if not zero

Flag: ZF

Page 12: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Example

MOV AL, 5

CMP AL, 5

JE label1

JMP exit

label1: MOV CX, BX

exit: …..

Page 13: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

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

Page 14: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Example

MOV AL, 5

CMP AL, -5

JG label1

JMP exit

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

exit:

Page 15: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

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

Page 16: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Example

MOV AL, -2

CMP AL, 5

JL label1

JMP exit

label1: MOV CX, 5; in this case AL < 5

exit: …

Page 17: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Practice

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

Page 18: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Conditional Jump instructions

JB/JNAE

[label:] JB/JNAE short-address

Jump Above/Equal or Jump Not Above/Equal

Flag: ZF

Page 19: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Conditional Jump instructions

JBE/JNA

[label:] JBE/JNA short-address

Jump Below/Equal or Jump Not Above

Flag: AF, ZF

Page 20: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Special Arithmetic Test

JCXZ: Jump if CX is zeroJC: Jump if carryJNC: Jump if no carryJO: Jump if overflowJNP: Jump if no overflow

Page 21: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Practice

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

BYTE_TBL DB 0,5,6,4,9,7

BYTETOTAL DB 0

Page 22: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Assembling, Linking and Executing Programs

Assembling: translate sourse program (written in assembly language) into machine code (object code)

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

Page 23: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Assembling, Linking and Executing Programs

Editor

Create *.asm

Assemble *.asmAnd create *.obj

Assembler

LinkAnd create *.exe

Linker

Page 24: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Assembling a Source Program

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

*.asm *.obj, *.lst, *.crfAssembling

Page 25: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Example

; Add two numbers and store the results into the third variablepage 60,132TITLE A04ASM1 (EXE) Move and add operations; ---------------------------------------------STACK SEGMENT PARA STACK 'Stack'

DW 32 DUP(0)STACK ENDS; ----------------------------------------------DATASEG SEGMENT PARA 'Data'

FLDD DW 215FLDE DW 125FLDF DW ?

DATASEG ENDS; -----------------------------------------------CODESEG SEGMENT PARA 'Code'MAIN PROC FAR

ASSUME SS:STACK,DS:DATASEG,CS:CODESEGMOV AX,DATASEG ;Set address of data MOV DS,AX ; segment in DS MOV AX,FLDD ;Move 0215 to AXADD AX,FLDE ;Add 0125 to AXMOV FLDF,AX ;Store sum in FLDFMOV AX,4C00H ;End processingINT 21H

MAIN ENDP ;End of procedureCODESEG ENDS ;End of segment

END MAIN ;End of program

Page 26: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Example ===================================================================================================

[LINE] LOC: MACHINE CODE SOURCE=================================================================================================== [ 1] : ; Add two numbers and store the results into the third variable[ 2] : page 60,132[ 3] : TITLE A04ASM1 (EXE) Move and add operations[ 4] : ; ---------------------------------------------[ 5] : STACK SEGMENT PARA STACK 'Stack'

[ 6] 0000: 00 00 00 00 00 00 00 00 00 00 00 00 DW 32 DUP(0) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 7] : STACK ENDS[ 8] : ; ----------------------------------------------===================================================================================================

Page 27: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Example ===================================================================================================

[LINE] LOC: MACHINE CODE SOURCE===================================================================================================

[ 9] : DATASEG SEGMENT PARA 'Data'

[ 10] 0040: D7 00 FLDD DW 215[ 11] 0042: 7D 00 FLDE DW 125[ 12] 0044: 00 00 FLDF DW ?

[ 13] : DATASEG ENDS[ 14] : ; -----------------------------------------------

Page 28: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Example ===================================================================================================

[LINE] LOC: MACHINE CODE SOURCE===================================================================================================

[ 16] 0050: MAIN PROC FAR[ 17] : ASSUME SS:STACK,DS:DATASEG,CS:CODESEG

[ 18] 0051: B8 04 00 MOV AX,DATASEG

[ 19] 0054: 8E D8 MOV DS,AX [ 20] 0056: A1 00 00 MOV AX,FLDD

[ 21] 0059: 03 06 02 00 ADD AX,FLDE

[ 22] 005D: A3 04 00

Page 29: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

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

Page 30: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Linking an Object Program

*.map

START STOP LENGTH NAME CLASS

00000H 0003FH 0040H STACK STACK

00040H 00045H 0006H DATASEG DATA

00050H 00063H 0014H CODESEGCODE

Program entry at 0005:0000

Page 31: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Procedures

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

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 ...

RETname ENDP

Page 32: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Procedures

CALL instruction is used to call a procedure

CALL is used to transfer controlsRET is used to end execution of

procedure

Syntax: [label:] CALL procedure

[label:] RET [immediate]

Page 33: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

ExampleCALL m1

MOV AX, 2 RET ; return to operating system.

m1 PROC MOV BX, 5 CALL m2RET ; return to caller.

m1 ENDP m2 PROC

MOV CX, 10RET; return to caller

m2 ENDP

Page 34: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Procedure

NEAR: intra-segment procedureFAR PROC: inter-segment procedure

Page 35: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Program execution and Stack

CALL and PUSH store one word address or value onto stack

RET and POP pop and stack and access the previously pushed word

Page 36: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Example

Page 37: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Example

Page 38: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Practice exercise

Code a procedure using LOOP that calculate Fibonaci series:1,1,2,3,5,8,13..

Limits to 7 loopsEach number is the sum of the preceding two numbersAX should store the current Fibonaci number

Call this procedure from the main program

Page 39: Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice

Practice exercise

Basic coding in assembly language