1 / 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 exercise. Review. IMUL register/memory If operand is a byte AX= AL * operand. Review. MOV CX, 10 BEGINLOOP DEC CX

devaki
Download Presentation

Executing and Linking an assembly program

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Executing and Linking an assembly program

  2. Lesson plan • Review • Program logic and control • Practice exercise • Assembling, Linking and Executing Programs • Practice exercise

  3. Review • IMUL register/memory If operand is a byte AX= AL * operand

  4. Review MOV CX, 10 BEGINLOOP DEC CX JNZ BEGINLOOP OR MOV CX, 10 BEGINLOOP LOOP BEGINLOOP

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

  6. Transfer operations • JMP Unconditional. Transfer control under all circumstances Syntax: [label:] JMP short/near/far address

  7. Transfer operations • Backward jump BEGINLOOP: …. JMP BEGINLOOP Forward jump JMP STARTLOOP STARTLOOP: ….

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

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

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

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

  12. Example MOV AL, 5 CMP AL, 5 JE label1 JMP exit label1: MOV CX, BX exit: …..

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

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

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

  16. Example MOV AL, -2 CMP AL, 5 JL label1 JMP exit label1: MOV CX, 5 ; in this case AL < 5 exit: …

  17. Practice • Write a program to compute sum of even integers from 1 to 100 using LOOP

  18. Conditional Jump instructions JB/JNAE [label:] JB/JNAE short-address Jump Above/Equal or Jump Not Above/Equal Flag: ZF

  19. Conditional Jump instructions JBE/JNA [label:] JBE/JNA short-address Jump Below/Equal or Jump Not Above Flag: AF, ZF

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

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

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

  23. Create *.asm Assemble *.asm And create *.obj Assembler Link And create *.exe Linker Assembling, Linking and Executing Programs Editor

  24. Assembling a Source Program • Converts your source code into machine code and displays any errors. Assembling *.obj, *.lst, *.crf *.asm

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

  26. 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] : ; ---------------------------------------------- ===================================================================================================

  27. 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] : ; -----------------------------------------------

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

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

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

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

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

  33. Example CALL m1 MOV AX, 2 RET ; return to operating system. m1 PROC MOV BX, 5 CALL m2 RET ; return to caller. m1 ENDP m2 PROC MOV CX, 10 RET; return to caller m2 ENDP

  34. Procedure • NEAR: intra-segment procedure • FAR PROC: inter-segment procedure

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

  36. Example

  37. Example

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

  39. Practice exercise • Basic coding in assembly language

More Related