Reputation: 3605
I'm really confused as to how stack pointers and byte pointers work. Here is the segment of code the problem is referencing:
.data
v1 db 'AB'
v2 db 'CD'
v3 db 'EF
.code
start:
mov ax,@data
mov ds,ax
mov sp,0100h
;call the subroutine
mov ax,offset v1
push ax
mov ax,offset v2
push ax
call subr
retsub:
The subroutine is:
subr:
push bp
mov bp,sp
mov si,[bp+6]
add si,1
mov dl,[si]
mov ah,2
int 21h
pop bp
ret
The three review questions associated with this problem have these answers:
1. After mov bp,sp
in the subroutine, the value of the hex value in bp
is 00F8
.
2. The subroutine writes a single ASCII character to the standard output. It writes B
.
3. The hex value in the sp
register after the subroutine returns to the main program at the instruction at the label retsub
is 00FC
.
Can anyone walk me through the steps so that I can understand this process a little better?
The offset data table that I have is:
offset 00 01 02 03 04 05
data 41 42 43 44 45 46
The way that I approach this problem in my head is:
mov sp,0100h ;sp = 0100
mov ax,offset v1 ;ax = 4142
push ax ;4142 is pushed onto the stack
mov ax,offset v2 ;ax = 4344
push ax ;4344 is pushed onto the stack
call subr
stack
------
|4344|
------
|4142|
------
This is as far as I understand and I'm sure I'm not even doing this part right. If you can, please break it down with what bp and sp is with each step so that I can follow along and hopefully apply this to another review problem.
Upvotes: 0
Views: 1222
Reputation: 7131
After mov bp,sp in the subroutine, the value of the hex value in bp is 00F8
Yes, because sp is initialized with 100h then two word sized arguments are pushed (push ax), the subroutine is called which pushes the 2-byte return address onto the stack, and last push bp is executed which decrease sp with another 2 bytes. So we have 100h-2-2-2-2 = 00F8h. Stack grows downwards in memory on Intel CPUs.
The hex value in the sp register after the subroutine returns to the main program at the instruction at the label retsub is 00FC.
This is because the subroutine exits with a "pop bp" instruction, then a ret instruction which pops (read from and increase sp) an return address from the stack. So 00F8+2+2=00FC.
Upvotes: 1
Reputation: 62106
Every push decrements sp
by the size of the pushed element. Every pop increments sp
similarly.
So, you begin with sp
= 100h, then you
push ax
push ax
call subr
because call pushes the return address and it's 16-bit herepush bp
and arrive at sp
= 0F8hpop bp
ret
and remove the 16-bit return address and arrive at sp
= 0FChUpvotes: 3