This section has me thoroughly confused. I have an example problem that I am hoping someone can break down into steps for me so that I may absorb how it is done to apply to other problems.
mc: call subr
mr: mov [val],ax
subr: push ax
push bx
push cx
add ax,dx
pop ax
pop bx
pop cx
ret
The book asks what the hex value in the sp and ax register will be for when the code returns from the subroutine and reaches the instruction mr: mov [val],ax. sp=0100 ax=0002, but I have no clue as to how to arrive at these answers.
The instruction mc: call subr saves the address of the next sequential instruction mr: mov [val],ax on the stack so the subroutine can correctly return. The absolute address in memory where the return address is saved is 1120E. Can anyone please elaborate on this as well?
registers given:
ax = 0000 bx = 0001 cx = 0002 dx = 0004
si = 0000 di = FFFF bp = 0080 sp = 0100
cs = 1000 ds = 1100 es = 1110 ss = 1111
The call at
mc:will save the current address, so when thesubr:returns, control will start again atmr:.Since
subr:pushes ax, bx, and cx in that order. It then pops ax, bx, and cx in that order, so what was pushed from cx gets popped into ax (and vice versa). The effect of these is to swap ax and cx. Theadd ax, dxhas no real effect on the result produced, because immediately after adding dx to ax, it pops ax off the stack. Theadddoes affect the flags, but nothing here does anything based on the flags, so at least in the code you’ve shown, that doesn’t mean much either.After control returns to
mr:, it writes the value inaxto memory, and then flows back intosubr:, thus swapping ax and cx back to where they started.IOW, the whole this is a very slow, roundabout way of achieving roughly the same effect as:
As far as the absolute address goes, there’s not much to say. In particular, chances are pretty good that if you run the same code again, it may be loaded at a different address, o the address saved on the stack may be entirely different.