iPr*_*ram 1 assembly bootloader x86-16
我正在按照本教程了解如何检查和启用 A20 线路。我想我明白了,但是有人可以帮我澄清一下吗?
该教程中已有的评论开始; <comment>,
我的评论开始;<comment>
; The following code is public domain licensed
[bits 16]
; Function: check_a20
;
; Purpose: to check the status of the a20 line in a completely self-contained state-preserving way.
; The function can be modified as necessary by removing push's at the beginning and their
; respective pop's at the end if complete self-containment is not required.
;
; Returns: 0 in ax if the a20 line is disabled (memory wraps around)
; 1 in ax if the a20 line is enabled (memory does not wrap around)
check_a20:
pushf ;Backup the current flags onto the stack
;Backup the below registers onto the stack
push ds ;|
push es ;|
push di ;|
push si ;-----
cli ;Disable interupts
xor ax, ax ; ax = 0
mov es, ax ;es = ax
not ax ; ax = 0xFFFF
mov ds, ax ; ds = ax
mov di, 0x0500 ;Boot signature part one (0x55)
mov si, 0x0510 ;Boot signature part two (0xAA)
mov al, byte [es:di] ;al = value at AA:55
push ax ;Backup ax register onto the stack
mov al, byte [ds:si] ;al = value at 55:AA
push ax ;Backup al onto the stack
mov byte [es:di], 0x00 ;Memory location AA:55 = 0
mov byte [ds:si], 0xFF ;Memory location at 55:AA = 0xFF
cmp byte [es:di], 0xFF ;Does value at AA:55 = 0xFF? If so, this means A20 is disabled
pop ax ;Restore saved ax register
mov byte [ds:si], al ;Set 55:AA to al
pop ax ;Restore ax register
mov byte [es:di], al ;set AA:55 to al
mov ax, 0 ;Return status of this function = 0 (Disabled)
je check_a20__exit ;A20 is disabled. Go to check_a20__exit
mov ax, 1 ;Return status of this function = 1 (Enabled)
check_a20__exit:
;Backup registers
pop si
pop di
pop es
pop ds
popf ;Backup flags
ret ;Return
Run Code Online (Sandbox Code Playgroud)
如果我不明白某些部分,您能解释一下原因吗?
该代码通过写入两个地址来检查FFFF:0510和是否0000:0500引用相同的地址,并查看写入一个地址是否会覆盖另一个地址。
事实证明,FFFF:0510可以表示线性地址0x100500而不是0x500,但前提是 A20 已启用。es:di因此,代码将全零写入(aka )处的字节0000:0500,并将全 1 写入ds:si(aka FFFF:0510) 处的字节。如果启用 A20,则两个段:偏移对引用不同的地址,第一个写入将保留,并且[es:di]将包含零。否则,这两对引用相同的地址,第二次写入将破坏第一个,并且[es:di]将包含0xff.
(顺便说一句,0x55和0xAA不是其中的一部分;我不确定你从哪里得到这些数字。启动签名通常位于 0x7dfe,IIRC。)