Page 1 of 1

RMode Stack doesn't work

Posted: Wed Jun 03, 2009 3:41 pm
by Cjreek
Hi,

I'm quite confused. My stack only pops garbage -.-
Here's some code:

Initialization

Code: Select all

    mov ax, 0
    mov ss, ax
    mov sp, 0xFFFF
Part of my code

Code: Select all

    mov dx,1
    mov al,1
    call floppy_read
;  ....

floppy_read:
    xor cx,cx
    mov es,cx     
    mov bx,0x7E00

    push dx 
    call lba_to_chs 
    ;....

lba_to_chs:
    pop ax  ; <--- Here I get 0x7C5A instead of 1
;   ...
This is only 1 example. I don't know what I'm doing wrong :?

Cjreek

Re: RMode Stack doesn't work

Posted: Wed Jun 03, 2009 3:44 pm
by Cjreek
Ok I got it... 0x7C5A is the return address.... #-o

Sorry :roll:

Re: RMode Stack doesn't work

Posted: Wed Jun 03, 2009 3:45 pm
by Troy Martin
You're popping the return IP that's on the stack, not what you want. Use registers for function calls, it works better if you don't understand how to manipulate and work with the stack. And even more so in a bootloader, since it's smaller.

EDIT: Oh, and your mov al,1 before read_sector is probably being trashed by either read_sector or lba_to_chs.

Re: RMode Stack doesn't work

Posted: Wed Jun 03, 2009 4:04 pm
by Cjreek
Troy Martin wrote:Use registers for function calls
Well I did it this way first, but I got problems in following case:

Code: Select all

;prepare params for fun
mov ax,10
mov dl, 2
call calc_third_param
call fun
The problem has been, that changing registers in calc_third_param destroyed my params for "fun". Well you can first call calc_third_param and then set ax and dl, but even with this you can get tricky problems in some cases.
Troy Martin wrote:EDIT: Oh, and your mov al,1 before read_sector is probably being trashed by either read_sector or lba_to_chs.
Oh yes I saved al until I thought, that it's not necessary.. :?

Re: RMode Stack doesn't work

Posted: Wed Jun 03, 2009 4:15 pm
by Troy Martin
Cjreek wrote:The problem has been, that changing registers in calc_third_param destroyed my params for "fun". Well you can first call calc_third_param and then set ax and dl, but even with this you can get tricky problems in some cases.
pusha and popa. Remember that popa pops ALL general-purpose registers off the stack, so you have to save any return values.