BX = 0 DX = 0 read a (signed) value into AX // you can use CALL READINT // (READINT returns value in AX) while (AX > 0){ for (int i = AX; i >= 1; i--){ DX = DX + 1 } BX = BX - 1 read a (signed) value into AX } ANSWER: mov bx, 0 mov dx, 0 call readint while: cmp ax, 0 jle endwhile mov cx, ax lup: inc dx loop lup dec bx call readint jmp while endwhile:
.data List dw -2, -1, 0, 1, 2Each of the following sets of instructions is supposed to change the value of the 3rd item in
List
from 0 to 1. For each set of instructions,
circle Valid if the code works as intended. Circle Invalid
if the code contains any assembly errors or if it assembles correctly but
does not work as intended. If you circle Invalid, explain what is wrong
or what needs to be changed to fix the code.
MOV BX, List ADD BX, 4 MOV AX, [BX] INC AX MOV [BX], AX
Invalid Should be MOV BX, OFFSET List
MOV SI, OFFSET List+4 MOV AX, [SI] ADD AX, 1 MOV [SI], AX
Valid
MOV SI, List[2] INC SI MOV List[2], SI
Invalid Should be List[4]
MOV SI, OFFSET List MOV DI, 4 MOV AX, [SI][DI] INC AX MOV [SI][DI], AX
Invalid Assembly error, can't use SI and DI together
MOV BX, OFFSET List ADD BX, 4 ADD [BX], 1
Invalid Should be ADD WORD PTR [BX], 1
.data List10 dw 5, 7, 12, -4, 16, 2, 43, 9, 19, 6 Sum dw 0 ; procedure returns the answer here .code .startup MOV AX, OFFSET List10 PUSH AX MOV AX, OFFSET Sum PUSH AX CALL SumTenElts (...)Here is part of the procedure SumTenElts. Complete the procedure by filling in the missing instructions next to the **'d comments.
SumTenElts proc PUSH BP MOV BP, SP PUSH AX ; push the registers that will be PUSH BX ; used by the procedure PUSH CX PUSH SI MOV CX, 10 ; 10 is the loop counter MOV BX, [BP+6];** move 1st argument (address of array) into BX MOV SI, [BP+4};** move 2nd argument (address of sum) into SI Lup: MOV AX, [BX] ;** move array element into AX ADD [SI], AX ;** add AX to the sum variable ADD BX, 2 ; make BX point to next array element LOOP Lup ; loop for 10 elements POP SI ;** restore all registers and return to the POP CX ; calling program with a clean stack POP BX ; You will need to write several instructions here. POP AX POP BP RET 4 SumTenElts endp
.model small .stack 100h .code .startup MOV AH, 0 MOV AL, '1' PUSH AX CALL Countup .exit Countup PROC ; Countup destroys PUSH BP ; the DX register MOV BP, SP MOV DX, [BP+4] CMP DL, '0' JE Print DEC DL PUSH DX CALL Countup MOV DX, [BP+4] Print: PUSH AX MOV AH, 02h ; function 02h displays the INT 21h ; character stored in DL POP AX POP BP RET 2 Countup ENDP end
Draw a picture of the stack as it appears the first time the instruction
MOV AH, 02h
executes in this program. In your picture, show which locations SP and
BP point to. You may show the stack as a stack of 16-bit words. Indicate as
best you can the contents of each word on the stack (if you don't know the
exact value that's stored, you can just give the name of the register that's
stored or indicate that a return address is stored). Your picture should indicate the direction of increasing memory addresses.
ANSWER: Stack should show two ``frames'', one for each of the two calls to Countup. Each frame contains (from least-recently-pushed to most-recently-pushed) the character passed as an argument, the return address of the calling function, and the ``old'' value of BP. There is one additional item on the stack at the time of the first execution of the INT 21H instruction, it is the value of AX that gets pushed at the label Print:
A B C F ----------- 0 0 0 0 0 0 1 0 0 1 0 1 0 1 1 0 1 0 0 0 1 0 1 1 1 1 0 0 1 1 1 0