%SP寄存器没有指向堆 [英] %sp register doesn't point to stack
问题描述
我在写一个非常基本的内核。我试着写一个函数,通过堆栈传递的参数。内核编译时NASM(像<一个描述href=\"http://stackoverflow.com/questions/22300739/wrong-memory-locations-when-debugging-in-qemu-with-gdb/22348746\">this问题)和QEMU运行。我用gdb调试。
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 $
通过这一步步走向和找什么 SP
包含看着那尖尖的地址是什么揭示了 SP
是德/递增的权利,但它指向的地址总是包含为0x0000。
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.
我想这可能会涉及到 MOV SP,4096
行。所以我评论一下。这也不起作用。唯一的区别是,值 SP
点,现在有一些人但不是我把那里的人。
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?
推荐答案
- 您想看看
16 * $ SS + $ ESP
在GDB。 (小丑一样在他的评论中所建议的) - 这是 86分割解释。注意这同样适用于数据存储访问和
DS
注册。 - 您设置
SS
到0x7C00 + 288
和SP
4096这样的物理堆栈指针地址((0x7c00 + 0x0120)LT; 4;)+ 0×1000
给0x7e200
。 - 写作十六进制的code所有的内存地址和偏移量可能会与算术帮助。
Explanation
- You want to look at
16*$ss + $esp
in GDB. (Like Jester suggested in his comment) - This is explained in x86 Segmentation. Note the same applies to data memory access and the
DS
register. - You set
SS
to0x7C00 + 288
andSP
to 4096. Thus the physical stack pointer address is((0x7c00+0x0120)<<4) + 0x1000
giving0x7e200
. - Writing all memory addresses and offsets in your code in hexadecimal may help with the arithmetic.
boot.asm
BITS 16
global start
start:
mov ax, 0x7C00
add ax, 0x0120
mov ss, ax
mov sp, 0x1000
mov ax, 0x7C00
mov ds, ax
test:
push 42
push 43
push 'T'
pop ax
pop ax
push 44
pop ax
pop ax
hlt
检查-stack.gdb
set confirm 0
set pagination 0
set architecture i8086
target remote localhost:1234
file boot
set disassemble-next-line 1
define hook-stop
printf "Stack Pointer: 0x%04x, AX: 0x%04x\n", ($ss*16 + $esp), $ax
# after stack setup, the linear stack pointer address is 0x7e200
set variable $sp_linear = 0x7e200
x/8xb ($sp_linear - 8)
end
break test
continue
set variable $i = 0
while $i < 8
stepi
set variable $i = $i + 1
end
monitor quit
disconnect
quit
x86的boot.ld
ENTRY(start);
SECTIONS
{
. = 0x7C00;
.text : AT(0x7C00)
{
_text = .;
*(.text);
_text_end = .;
}
.data :
{
_data = .;
*(.bss);
*(.bss*);
*(.data);
*(.rodata*);
*(COMMON)
_data_end = .;
}
.sig : AT(0x7DFE)
{
SHORT(0xaa55);
}
/DISCARD/ :
{
*(.note*);
*(.iplt*);
*(.igot*);
*(.rel*);
*(.comment);
/* add any unwanted sections spewed out by your version of gcc and flags here */
}
}
构建:
nasm -g -f elf -F dwarf boot.asm -o boot.o
cc -nostdlib -m32 -T x86-boot.ld -Os -Wall -g3 -I. -Wl,--build-id=none boot.o -o boot
objcopy -O binary boot boot.good.bin
会话示例
$ qemu-system-x86_64 -s -S boot.good.bin &
$ gdb -q -x examine-stack.gdb
The target architecture is assumed to be i8086
0x0000fff0 in ?? ()
Breakpoint 1 at 0x7c10: file boot.asm, line 13.
Stack Pointer: 0x7e200, AX: 0x7c00
0x7e1f8: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
Breakpoint 1, test () at boot.asm:13
13 push 42
=> 0x00007c10 : 6a 2a push $0x2a
Stack Pointer: 0x7e1fe, AX: 0x7c00
0x7e1f8: 0x00 0x00 0x00 0x00 0x00 0x00 0x2a 0x00
14 push 43
=> 0x00007c12 : 6a 2b push $0x2b
Stack Pointer: 0x7e1fc, AX: 0x7c00
0x7e1f8: 0x00 0x00 0x00 0x00 0x2b 0x00 0x2a 0x00
15 push 'T'
=> 0x00007c14 : 6a 54 push $0x54
Stack Pointer: 0x7e1fa, AX: 0x7c00
0x7e1f8: 0x00 0x00 0x54 0x00 0x2b 0x00 0x2a 0x00
16 pop ax
=> 0x00007c16 : 58 pop %ax
Stack Pointer: 0x7e1fc, AX: 0x0054
0x7e1f8: 0x00 0x00 0x54 0x00 0x2b 0x00 0x2a 0x00
17 pop ax
=> 0x00007c17 : 58 pop %ax
Stack Pointer: 0x7e1fe, AX: 0x002b
0x7e1f8: 0x00 0x00 0x54 0x00 0x2b 0x00 0x2a 0x00
18 push 44
=> 0x00007c18 : 6a 2c push $0x2c
Stack Pointer: 0x7e1fc, AX: 0x002b
0x7e1f8: 0x00 0x00 0x54 0x00 0x2c 0x00 0x2a 0x00
19 pop ax
=> 0x00007c1a : 58 pop %ax
Stack Pointer: 0x7e1fe, AX: 0x002c
0x7e1f8: 0x00 0x00 0x54 0x00 0x2c 0x00 0x2a 0x00
20 pop ax
=> 0x00007c1b : 58 pop %ax
Stack Pointer: 0x7e200, AX: 0x002a
0x7e1f8: 0x00 0x00 0x54 0x00 0x2c 0x00 0x2a 0x00
21 hlt
=> 0x00007c1c : f4 hlt
这篇关于%SP寄存器没有指向堆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!