Executing and Linking an assembly program Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice exercise Review IMUL register/memory If operand is a byte AX= AL * operand Review MOV CX, 10 BEGINLOOP DEC CX JNZ BEGINLOOP OR MOV CX, 10 BEGINLOOP LOOP BEGINLOOP 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 Transfer operations JMP Unconditional. Transfer control under all circumstances Syntax: [label:] JMP short/near/far address Transfer operations Backward jump BEGINLOOP: …. JMP BEGINLOOP Forward jump JMP STARTLOOP STARTLOOP: …. 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) 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 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 CMP AX, DX JE Startloop 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 Example MOV AL, 5 CMP AL, 5 JE label1 JMP exit label1: 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 short-address Example MOV AL, 5 CMP AL, -5 JG label1 JMP exit label1: MOV CX, -5 ; in this case AL > -5 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-address Example MOV AL, -2 CMP AL, 5 JL label1 JMP exit label1: MOV CX, 5 ; in this case AL < 5 exit: … Practice Write a program to compute sum of even integers from 1 to 100 using LOOP Conditional Jump instructions JB/JNAE [label:] JB/JNAE short-address Jump Above/Equal or Jump Not Above/Equal Flag: ZF Conditional Jump instructions JBE/JNA [label:] JBE/JNA short-address Jump Below/Equal or Jump Not Above 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 Practice Write a program that adds each value defined in BYTE_TBL and store the sum in BYTE_TOTAL BYTE_TBL BYTETOTAL DB 0,5,6,4,9,7 DB 0 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 Assembling, Linking and Executing Programs Create *.asm Editor Assembler Assemble *.asm And create *.obj Linker Link And create *.exe Assembling a Source Program Converts your source code into machine code and displays any errors. Assembling *.asm *.obj, *.lst, *.crf Example ; Add two numbers and store the results into the third variable page 60,132 TITLE A04ASM1 (EXE) Move and add operations ; --------------------------------------------STACK SEGMENT PARA STACK 'Stack' DW 32 DUP(0) STACK ENDS ; ---------------------------------------------DATASEG SEGMENT PARA 'Data' FLDD DW 215 FLDE DW 125 FLDF DW ? DATASEG ENDS ; ----------------------------------------------CODESEG SEGMENT PARA 'Code' MAIN PROC FAR ASSUME SS:STACK,DS:DATASEG,CS:CODESEG MOV AX,DATASEG ;Set address of data MOV DS,AX ; segment in DS MOV AX,FLDD ;Move 0215 to AX ADD AX,FLDE ;Add 0125 to AX MOV FLDF,AX ;Store sum in FLDF MOV AX,4C00H ;End processing INT 21H MAIN ENDP ;End of procedure CODESEG ENDS ;End of segment END MAIN ;End of program Example =================================================================================================== [LINE] LOC: MACHINE CODE SOURCE =================================================================================================== [ [ [ [ [ 1] 2] 3] 4] 5] : : : : : [ 6] ; Add two numbers and store the results into the third variable page 60,132 TITLE A04ASM1 (EXE) Move and add operations ; --------------------------------------------STACK SEGMENT PARA STACK 'Stack' 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] : ; ---------------------------------------------=================================================================================================== Example =================================================================================================== [LINE] LOC: MACHINE CODE SOURCE =================================================================================================== [ 9] : [ 10] 0040: D7 00 [ 11] 0042: 7D 00 [ 12] 0044: 00 00 [ 13] : [ 14] : ------------ DATASEG FLDD FLDE FLDF SEGMENT PARA 'Data' DW DW DW 215 125 ? DATASEG ENDS ; ----------------------------------- Example =================================================================================================== [LINE] LOC: MACHINE CODE [ 16] 0050: MAIN FAR =================================================================================================== [ 17] : SOURCE PROC ASSUME SS:STACK,DS:DATASEG,CS:CODESEG [ 18] 0051: B8 04 00 MOV AX,DATASEG [ 19] 0054: 8E D8 [ 20] 0056: A1 00 00 MOV MOV DS,AX AX,FLDD [ 21] 0059: 03 06 02 00 ADD AX,FLDE [ 22] 005D: A3 04 00 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 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 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 ... RET name ENDP Procedures 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 m1 m1 m2 m2 CALL m1 MOV AX, 2 RET ; return to operating system. PROC MOV BX, 5 CALL m2 RET ; return to caller. ENDP PROC MOV CX, 10 RET; return to caller ENDP Procedure NEAR: intra-segment procedure FAR PROC: inter-segment procedure 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 Example Example Practice exercise Code a procedure using LOOP that calculate Fibonaci series: 1,1,2,3,5,8,13.. Limits to 7 loops Each number is the sum of the preceding two numbers AX should store the current Fibonaci number Call this procedure from the main program Practice exercise Basic coding in assembly language