Assembly %sp寄存器没有';t指向堆栈

Assembly %sp寄存器没有';t指向堆栈,assembly,gdb,stack,qemu,osdev,Assembly,Gdb,Stack,Qemu,Osdev,我正在写一个非常基本的内核。我试图编写一个函数,参数通过堆栈传递。内核使用nasm编译(如中所述),并使用QEMU运行。我正在使用gdb进行调试 在经历了很长一段时间的问题之后,我写了这篇文章来测试一些基本的堆栈操作: BITS 16 global start start: mov ax, 0x7C00 add ax, 288 mov ss, ax mov sp, 4096 mov ax, 0x7C00 mov ds, ax test:

我正在写一个非常基本的内核。我试图编写一个函数,参数通过堆栈传递。内核使用nasm编译(如中所述),并使用QEMU运行。我正在使用gdb进行调试

在经历了很长一段时间的问题之后,我写了这篇文章来测试一些基本的堆栈操作:

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

我想这可能与mov sp 4096行有关。所以我把它注释掉了。这也没用。唯一的区别是,
sp
指向的值现在是一些其他值,而不是我推到那里的值

是否需要执行一些操作来初始化堆栈或类似的操作?

说明
  • 您想查看GDB中的
    16*$ss+$esp
    。(就像杰斯特在评论中建议的那样)
  • 这一点在本文中进行了解释。注:这同样适用于数据存储器访问和
    DS
    寄存器
  • 您将
    SS
    设置为
    0x7C00+288
    并将
    SP
    设置为4096。因此,物理堆栈指针地址是
    ((0x7c00+0x0120)0x00007c12:6A2B push$0x2b)
    堆栈指针:0x7e1fc,AX:0x7c00
    0x7e1f8:0x00 0x00 0x00 0x00 0x2b 0x00 0x2a 0x00
    15推‘T’
    =>0x00007c14:6a 54推送$0x54
    堆栈指针:0x7e1fa,AX:0x7c00
    0x7e1f8:0x00 0x00 0x54 0x00 0x2b 0x00 0x2a 0x00
    16把斧头
    =>0x00007c16:58%ax
    堆栈指针:0x7e1fc,AX:0x0054
    0x7e1f8:0x00 0x00 0x54 0x00 0x2b 0x00 0x2a 0x00
    17把斧头
    =>0x00007c17:58%ax
    堆栈指针:0x7e1fe,AX:0x002b
    0x7e1f8:0x00 0x00 0x54 0x00 0x2b 0x00 0x2a 0x00
    18推44
    =>0x00007c18:6A2C推送$0x2c
    堆栈指针:0x7e1fc,AX:0x002b
    0x7e1f8:0x00 0x00 0x54 0x00 0x2c 0x00 0x2a 0x00
    19弹弓
    =>0x00007c1a:58%ax
    堆栈指针:0x7e1fe,AX:0x002c
    0x7e1f8:0x00 0x00 0x54 0x00 0x2c 0x00 0x2a 0x00
    20把斧头
    =>0x00007c1b:58%ax
    堆栈指针:0x7e200,AX:0x002a
    0x7e1f8:0x00 0x00 0x54 0x00 0x2c 0x00 0x2a 0x00
    21HLT
    =>0x00007c1c:f4 hlt
    
    您确定正在查找正确的段吗?否。我如何选择要查找的段。以及必须查找的段?您已将
    ss
    设置为
    0x7d20
    288=0x120
    )。您应该查看该段,或者,如果调试器使用物理地址,则应用通常的实模式计算
    16*ss+sp
    。 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
    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
    
    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