Q2. Consider the following code and the variables declared in the memory: [8+2 = 10 POINTS]
ORG 0x0400
Address:
0400H MOV BX, ARRAY
MOV CX, [BX]
MOV AX, [BX+2]
MOV DX, [BX+4]
PUSH CX
CALL SHERIDAN
MOV [BX], CX
MOV [BX+2], AX
MOV [BX+4], DX
ADD AX, DX
MOV [BX+6], AX
SHL AX, 1
MOV [BX+8], AX
0430H INT3
ARRAY DW 03H, 09H, 56H, 67H, 09H
SHERIDAN:
POP CX
AGAIN:
DEC AX
INC DX
LOOP AGAIN
RET
a. After executing the above code what would be the content of ARRAY variable. You must determine values for all the elements (five elements) in the ARRAY. You must show your work.
b. As above, the address allocated for INT3 is 0430H. As such, what are the addresses (the range) allocated for the ARRAY variable?
3 answers
ARRAY = [03H, 09H, 56H, 67H, 09H]
To determine the values for all the elements in the ARRAY, we can follow the code step by step:
1. MOV BX, ARRAY - BX = 0406H (address of the first element in the ARRAY)
2. MOV CX, [BX] - CX = 03H (value of the first element in the ARRAY)
3. MOV AX, [BX+2] - AX = 09H (value of the second element in the ARRAY)
4. MOV DX, [BX+4] - DX = 56H (value of the third element in the ARRAY)
5. PUSH CX - Pushes the value of CX (03H) onto the stack
6. CALL SHERIDAN - Calls the subroutine SHERIDAN
Inside SHERIDAN:
POP CX - Pops the value of CX (03H) from the stack
AGAIN:
DEC AX - Decrements the value of AX (09H)
INC DX - Increments the value of DX (57H)
LOOP AGAIN - Loops back to AGAIN if CX (03H) is not zero
After the loop, AX and DX will have the values AX = 07H and DX = 59H
RET - Returns from the subroutine
7. MOV [BX], CX - Stores the value of CX (03H) in the first element of the ARRAY
8. MOV [BX+2], AX - Stores the value of AX (07H) in the second element of the ARRAY
9. MOV [BX+4], DX - Stores the value of DX (59H) in the third element of the ARRAY
10. ADD AX, DX - AX = AX + DX = 07H + 59H = 60H
11. MOV [BX+6], AX - Stores the value of AX (60H) in the fourth element of the ARRAY
12. SHL AX, 1 - Shifts the bits of AX to the left by 1 position, AX = 00C0H
13. MOV [BX+8], AX - Stores the value of AX (00C0H) in the fifth element of the ARRAY
Therefore, the final content of the ARRAY variable would be:
ARRAY = [03H, 07H, 59H, 60H, 00C0H]
b. The addresses allocated for the ARRAY variable would range from 0406H to 040AH.
In the given code, the address of the first element of the ARRAY variable is assigned to the BX register using the instruction `MOV BX, ARRAY`. So, BX will contain the address 0406H, which points to the first element of the ARRAY.
The size of each element in the ARRAY is 2 bytes because each element is defined as DW (word) in the code. Therefore, to determine the range of addresses allocated for the ARRAY variable, we need to consider the size of each element.
The BX register holds the base address of the ARRAY. Adding an offset of 0 accesses the first element, an offset of 2 accesses the second element, an offset of 4 accesses the third element, and so on.
Since each element is 2 bytes, the address range allocated for the ARRAY variable is:
- Starting address: BX + 0 = 0406H + 0 = 0406H
- Ending address: BX + (number of elements - 1) * size of each element = 0406H + (5 - 1) * 2 = 040AH
Therefore, the addresses allocated for the ARRAY variable range from 0406H to 040AH.