SHEET-2 ANSWERS [1] Rewrite Program 2-3 to transfer one word at a time instead of one byte. TITLE PROG2-3 PURPOSE: TRANSFER 6 WORDS OF DATA PAGE 60,132.MODEL SMALL.STACK 64.DATA ORG 10H DATA_IN DW 234DH, 1DE6H, 3BC7H, 566AH, 7890H, 8976H ORG 30H COPY DW 6 DUP(?).CODE MAIN PROC FAR MOV AX, @DATA MOV DS, AX MOV SI, OFFSET DATA_IN ;SI points to data to be copied MOV DI, OFFSET COPY ;DI points to copy of data MOV CX, 06 ;set up loop counter CX=6 MOV_LOOP: MOV AX, [SI] ;move the next word to AX MOV [DI], AX ;copy the word in AX to COPY area INC SI INC SI ;increment SI twice to point to next word ;increment DI twice to point to next position DEC CX ;decrement the counter JNZ MOV_LOOP ;jump if counter not zero MOV AH,4CH INT 21H MAIN ENDP END MAIN [2] Circle one: The linking process comes (after, before) assembling. Prepared By: Samsad Beagum, Lecturer, Department of CS and IT, Taif University. Page 1
The linking process comes after assembling. [3] In some applications it is common practice to save all registers at the beginning of a subroutine. Assume that SP = 1288H before a subroutine CALL. Show the contents of the stack pointer and the exact memory contents of the stack after PUSHF for the following: 1132:0450 CALL PROC1 1132:0453 INC BX PROC1 PROC PUSH AX PUSH BX PUSH CX PUSH DX PUSH SI PUSH Dl PUSHF PROC1 ENDP Prepared By: Samsad Beagum, Lecturer, Department of CS and IT, Taif University. Page 2
(Assume PROC1 is a NEAR procedure). The stack contents after executing PUSHF: SS:1278 SS:1279 SS:127A SS:127B SS:127C SS:127D SS:127E SS:127F SS:1280 SS:1281 SS:1282 SS:1283 SS:1284 SS:1285 SS:1286 SS:1287 SS:1288 Flags(L) Flags(H) DI(L) DI(H) SI(L) SI(H) DL DH CL CH BL BH AL AH 53 04 Low order byte of the flag register. High order byte of the flag register. Low order byte of the DI register. High order byte of the DI register. Low order byte of the SI register. High order byte of the SI register. Low order byte of the DX register. High order byte of the DX register. Low order byte of the CX register. High order byte of the CX register. Low order byte of the BX register. High order byte of the BX register. Low order byte of the AX register. High order byte of the AX register. IP for the INC instruction.(low byte) IP for the INC instruction(high byte) Start of Stack. [4] To restore the original information inside the CPU at the end of a CALL to a subroutine, the sequence of POP instructions must follow a certain order. Write the sequence of POP instructions that will restore the information in Problem 3. At each point, show the contents of the SP. Prepared By: Samsad Beagum, Lecturer, Department of CS and IT, Taif University. Page 3
The number of PUSH and POP instructions must match. The sequence of POP instructions: Initially, SP = 1278 POPF POP DI POP SI SP = 127A SP = 127C SP = 127E POP DX SP = 1280 POP CX SP = 1282 POP BX SP = 1284 POP AX SP = 1286 [5] In a FAR CALL, and are saved on the stack, whereas in a NEAR CALL, is saved on the stack. In a FAR CALL, CS and IP are saved on the stack, whereas in a NEAR CALL, IP is saved on the stack. [6] Compare the number of bytes of stack taken due to NEAR and FAR CALLs. NEAR CALL takes 2 bytes of stack to save the IP. FAR CALL takes 4 bytes of stack to save the CS and IP. [7] Find the contents of the stack and stack pointer after execution of the CALL instruction shown next. Prepared By: Samsad Beagum, Lecturer, Department of CS and IT, Taif University. Page 4
CS :IP 2450:673A CALL SUM 2450:673D DEC AH SUM is a near procedure. Assume the value SS: 1296 right before the execution of CALL. A near CALL saves only the contents of IP on the stack before calling a procedure. SS:1294 SS:1295 SS:1296 3D 67 IP address of the DEC instruction (Low order byte). IP address of the DEC instruction (High order byte). Start of Stack before executing CALL SUM. [8] The following is a section of BIOS of the IBM PC which is described in detail in Chapter 3. All the jumps below are short jumps, meaning that the labels are in the range -128 to+127. IP Code E06C 733F JNC ERROR1.. E072 7139 JNO ERROR1.. E08C 8ED8 C8: MOV DS, AX.. EOA7 EBE3 JMP C8.. EOAD F4 ERROR1: HLT Verify the address calculations of: (a) JNC ERROR1 (b) JNO ERROR1 (c) JMP C8 Prepared By: Samsad Beagum, Lecturer, Department of CS and IT, Taif University. Page 5
(Introduction: All conditional jumps are short jumps. (Short jumps - The address of the target (jumping location) should be within -128 to +127 bytes of the IP). So, short jumps are 2-byte instructions. 1st byte is the op-code of the jump, 2nd byte is the offset - a value between 00 and FF.) To find the target address, Target Address = Address of the instruction next to Jump + Second byte of the Jump instruction. (HOW THIS FORMULA WORKS FOR BACKWARD JUMP? In a backward jump, the second byte is the 2's complement of the offset. So when it is added to the address of the instruction next to Jump, we get the subtracted result - which is an address at the back). a) JNC ERROR1 Address of the instruction next to JNC = E06C+2 = E06E Second byte of the JNC instruction = 3F E06C 733F JNC ERROR1 Target Address (Address of ERROR1: HLT) = E06E + 3F = E0AD b) JNO ERROR1 Address of the instruction next to JNO = E072+2 = E074 Second byte of the JNO instruction = 39 E072 7139 JNO ERROR1 Prepared By: Samsad Beagum, Lecturer, Department of CS and IT, Taif University. Page 6
Target Address (Address of ERROR1: HLT) = E074 + 39 = E0AD c) JMP C8 Address of the instruction next to JMP C8 = E0A7+2 = E0A9 Second byte of the JMP C8 instruction = E3. EOA7 EBE3 JMP C8 Target Address (Address of C8: MOV DS, AX) = E0A9 + E3 = E08C (dropping the carry). [ Note: Second byte = E08C E0A9 = -1D 2 complement of 1D = E3 ] [9] The following program contains some errors. Fix the errors and make the program run correctly. Verify it through the DEBUG program. This program adds four words and saves the result. TITLE PROBLEM (EXE) PROBLEM 16 PROGRAM PAGE 60,132.MODEL SMALL.STACK 32.DATA DATA DW 234DH, DE6H, 3BC7H, 566AH ORG 10H Prepared By: Samsad Beagum, Lecturer, Department of CS and IT, Taif University. Page 7
SUM DW?.CODE START: PROC FAR MOV AX, DATA MOV DS, AX MOV CX, 04 MOV BX, 0 MOV DI, OFFSET DATA ;set up loop counter CX=4 ;initialize BX to zero ;set up data pointer BX LOOP1: ADD BX, [DI] ;add contents pointed at [DL] to BX JNZ LOOP1 MOV SI, OFFSET RESULT MOV [SI],BX ;increment DI ;jump if counter not zero ;load pointer for result ;store the sum MOV AH,4CH INT 21H START ENDP END START TITLE PROBLEM (EXE) PROBLEM 16 PROGRAM PAGE 60,132.MODEL SMALL.STACK 32.DATA DAT DW 234DH, 1DE6H, 3BC7H, 566AH Prepared By: Samsad Beagum, Lecturer, Department of CS and IT, Taif University. Page 8
ORG 10H SUM DW?.CODE START PROC FAR MOV AX, @DATA MOV DS, AX MOV CX, 04 MOV BX, 0 MOV DI, OFFSET DAT LOOP1: ADD BX, [DI] DEC CX JNZ LOOP1 MOV SI, OFFSET SUM MOV [SI],BX MOV AH,4CH INT 21H START ENDP END START ;set up loop counter CX=4 ;initialize BX to zero ;set up data pointer BX ;add contents pointed at [DL] to BX ;increment DI ;increment DI ;decrement the counter ;jump if counter not zero ;load pointer for result ;store the sum Prepared By: Samsad Beagum, Lecturer, Department of CS and IT, Taif University. Page 9