I'm writing on a very basic kernel. I tried to write a function, with parameters passed through the stack. The kernel is compiled with nasm (like described in this question) and run with QEMU. I'm using gdb for debugging.
After a long while having problems I wrote this to test some basic stack operations:
BITS 16
global start
start:
mov ax, 0x7C00
add ax, 288
mov ss, ax
mov sp, 4096
mov ax, 0x7C00
mov ds, ax
test:
push 42
push 43
push "T"
pop ax
pop ax
push 44
pop ax
pop ax
jmp $
Going through this step by step and looking what sp
contains and looking what at the pointed address is reveals that sp
is de-/incremented right, but the address it's pointing to always contains 0x0000.
I thought this could be related to the mov sp, 4096
line. So I commented it out. This didn't work either. The only difference was that the values sp
points to are now some others but not the ones I pushed there.
Is there something I have to do to initialize the stack or something similar?
Explanation
16*$ss + $esp
in GDB. (Like Jester suggested in his comment)DS
register.SS
to0x7C00 + 288
andSP
to 4096. Thus the physical stack pointer address is((0x7c00+0x0120)<<4) + 0x1000
giving0x7e200
.Scripting GDB to Examine the Stack
boot.asm
examine-stack.gdb
x86-boot.ld
Build with:
Sample Session