RMode Stack doesn't work

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Cjreek
Member
Member
Posts: 70
Joined: Thu May 28, 2009 2:41 pm
Location: Germany

RMode Stack doesn't work

Post 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
Cjreek
Member
Member
Posts: 70
Joined: Thu May 28, 2009 2:41 pm
Location: Germany

Re: RMode Stack doesn't work

Post by Cjreek »

Ok I got it... 0x7C5A is the return address.... #-o

Sorry :roll:
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: RMode Stack doesn't work

Post 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.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
Cjreek
Member
Member
Posts: 70
Joined: Thu May 28, 2009 2:41 pm
Location: Germany

Re: RMode Stack doesn't work

Post 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.. :?
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: RMode Stack doesn't work

Post 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.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
Post Reply