2017-05-28 54 views
1

我在自举程序结束时设置了4k堆栈空间。之后,我读取了16个扇区(= 8k)的代码到地址0x2000:0x0000。这是我操作系统的核心。我分支给它。在程序集中设置堆栈段和偏移量

问题是,我如何设置8k堆栈空间从内核结束开始?

bootloader.asm

; bootloaders are always loaded to offset 0x7c00. 
; so, define base to 7c00h. 
    org 7c00h 

; jmp to start function. 
    jmp hg._start 

; bootloader function. 
; set stack and segment registers. 
hg._start: 
    ; set stack space. (4K) 
    mov ax, 07c0h 
    add ax, 288 ; (4096+512)/16 bytes per paragraph. 
       ; note that this bootloader loads 
       ; remaining 8 sectors via int 13h:02. 
    mov ss, ax 
    mov sp, 4096 

    mov ax, 07c0h 
    mov ds, ax ; set data segment to base of the 
       ; bootloader. this can get rid of 
       ; some memory access errors. 

    ; from now on, we had set up segments. 
    ; now we can get into real work, loading remaining 
    ; 8 sectors with int 13h:02. 

    ; load code to 0x2000:0x0000. (0x20000) 
    mov bx, 2000h 
    mov es, bx 
    mov bx, 0 

    mov ah, 02 ; int 13h:02 -> bios read function. 
    mov al, 16 ; read 8k of code. 
    mov ch, 01 ; track to read. 
    mov cl, 02 ; sector to read. (from 1 = mbr) 
    mov dh, 01 ; head to read. 
    mov dl, 80h; drive to read (0=fd0, 1=fd1, 80h=hd0, 81h=hd1) 
    int 13h 

    times 510-($-$$) db 0 
    db 55h 
    db 0aah 

回答

2

尝试

mov ax,2000h 
mov ss,ax 
mov sp,4000h 

这应该设置堆栈到您希望它是。一般情况下不需要其他设置。

请注意,您不需要禁用中断以使其工作,因为在将新的段选择器加载到ss后,x86 CPU会隐式地禁用一条指令的中断。

+2

在80年代初期,除了早期的一批[8088s](https://books.google.com/books?id=1L7PVOhfUIoC&pg=PA492&lpg=PA492&d#v=onepage&q&f=false),最后一部分是真实的,没有像预期的那样禁用中断;-)。这是你会发现一些早期版本的DOS在堆栈更新之前明确禁止中断的原因之一。 –