Author Topic: question about stack  (Read 9965 times)

Ruisen Luo

  • Guest
question about stack
« on: July 05, 2009, 01:24:12 PM »
I call a funtion in my code:

push 28    ;DispStr(x,y,color,str_addr,length)
   push Message
   push2ch
   push 00h
   push 00h
   call DispStr
   sub sp, 7

the DispStr function is like this:
DispStr:
   push bp
   mov bp, sp   ;enter 0,0

mov ch, 00h
   mov cl, [bp+12]
   mov bh, 00h
   mov bl, [bp+8]
   mov dl, [bp+6]
   mov dh, [bp+7]
   mov ax, [bp+10]
   push bp
   mov bp, ax
   mov ax, 1301h
   int 10h
   pop bp

mov sp, bp
   pop bp   ;leave
   ret
Message:
   db "Something with this program!"

this program works right, but i was confused, why i have pushed  28, Message, 2ch continuous, but their address will be bp+12, bp+10, bp+12? and [bp+9] is 0?
run in 16-Bit mode

Ruisen Luo

  • Guest
Re: question about stack
« Reply #1 on: July 05, 2009, 01:33:55 PM »
sorry , it's bp+8, bp+10, bp+12, but bp+9 is empty

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: question about stack
« Reply #2 on: July 05, 2009, 02:52:38 PM »
Well, lemme see...

push 28 ;DispStr(x,y,color,str_addr,length)
push Message
push 2ch ; I assume "space"...
push 00h
push 00h
call DispStr
sub sp, 7 ; Wot????

DispStr:
push bp
mov bp, sp ;enter 0,0

mov ch, 00h  ; why not use cx? strlen might be over 256
mov cl, [bp+12] ; length
mov bh, 00h  ; page
mov bl, [bp+8]  ; color
mov dl, [bp+6]  ; column (I may have row/column reversed
mov dh, [bp+4]  ;row
mov ax, [bp+10]
push bp
mov bp, ax
mov ax, 1301h
int 10h
pop bp

mov sp, bp
pop bp ;leave
ret
Message:
db "Something with this program!"
Message_len equ $ - Message ; could use this for length

(all untested)

I would expect the high byte of all your parameters except "Message" to be zero (no such thing as "empty"), so (almost - except 11) any odd number added to bp should fetch zero...

The "odd" thing about int 10h/13h is the es:bp address for the address of the string. Nice job saving/restoring bp! Since it works, you must have es right. Printing to row zero can scroll off the top of the screen before you get a chance to see it, sometimes. Apparently that isn't a problem.

What are you thinking with the "sub sp, 7"??? I'd expect "add sp, 10" - I'd write it as "add sp, 2 * 5" (5 parameters, two bytes each) - to "clean up" the stack. You want to keep your stack aligned on an "even" boundary, at least. You should see the convolutions gcc goes through to align the stack (to 16 bytes)!

If it's working, you're doing good!

Best,
Frank

Ruisen Luo

  • Guest
Re: question about stack
« Reply #3 on: July 06, 2009, 02:20:59 AM »
Thanks, Frank
I have modified it , and it is much clear now:

push Message_len;DispStr(x,y,c,str,length)
   push Message
   push byte 2ch   ;color bgcolor:green forecolor:red
   push 00h   ;y, row
   push 00h   ;x, column
   call DispStr
   add sp, 10   ;clean the stack
...
DispStr:
   push bp
   mov bp, sp   ;enter 0,0

mov cx, [bp+12]
   mov bh, 00h   ;page
   mov bl, [bp+8]   ;color
   mov dl, [bp+4]   ;x, column
   mov dh, [bp+6]   ;y, row
   mov ax, [bp+10]   ;Address of Message
   push bp   ;save bp
   mov bp, ax   ;move the address to bp
   mov ax, 1301h   ;BIOS function code
   int 10h
   pop bp

mov sp, bp
   pop bp   ;leave
   ret

Message:
   db "Something with this program!"
   Message_len equ $ - Message   ;length of message