[英]Set stack segment and offset in assembly
我設置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
嘗試
mov ax,2000h
mov ss,ax
mov sp,4000h
這應該將堆棧設置為您想要的位置。 通常不需要其他設置。
請注意,您不需要為此禁用中斷,因為x86 CPU在將新的段選擇器加載到ss
后隱式禁用了一條指令的中斷。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.