我知道如果它的表示形式在AL中,我可以打印ASCII字符:
DrawChar:
MOV AL, 0x45
MOV AH, 0x0E
MOV BL, 0x07
MOV BH, 0x00
INT 0x10
RET
有没有办法可以使用INT 10H打印实际的寄存器值?例如,在例如AL中,AL是0x45。上面,所以它将打印45(不必是十六进制代表)。我在16位实模式引导加载程序中执行此操作。
答案 0 :(得分:4)
我将给你一种打印16位寄存器的方法。这通过将16位寄存器左4位旋转到最低有效位然后隔离这4位来一次打印半字节(4位)。然后,我们将该值的十六进制数字打印为0
到9
和A
到F
。我们对16位字中的4个半字节中的每一个执行此操作4次。
示例中提供了两个 16位十六进制打印功能。 选择适合您环境的:
print_hex_word
适用于任何8086/8088处理器或更高版本。 print_hex_word
这是一个较小的版本,适用于16位实模式的任何80186/80188处理器。 两种变体都要求您:
下面的示例代码包含一个最小引导加载程序作为示例:
bits 16
org 0x7c00
section .text
global _start
_start:
; Set segment registers to 0
xor ax, ax
mov ds, ax
mov es, ax
; Set stack pointer just below bootloader
mov ss, ax
mov sp, 0x7c00
; Clear direction flag (forward movement)
cld
; print_hex_word/print_hex_word_186 take a second parameter
; that is the page number (upper 8 bits) and foreground color
; in lower 8 bits. We just want 0x0000 so push it on the
; stack first
push ax
; This test just prints SS and SP to the display
push ss ; Push on stack as 1st parameter
; In this case display value in SS
call print_hex_word ; Print 16-bit value as hex
add sp, 2 ; Cleanup stack after call
push sp ; Push on stack as 1st parameter
; In this case display value in SP
call print_hex_word ; Print 16-bit value as hex
add sp, 2 ; Cleanup stack after call
; Print value 0xaa55 to the display
mov ax, 0xaa55
push ax ; Push on stack as 1st parameter
; In this case display value in 0xAA55
call print_hex_word ; Print 16-bit value as hex
cli ; Disable interrupts
hlt ; Halt processor
; Print 16 bit value passed on stack as first parameter
; in hexadecimal. Use page number and foreground color
; passed in second parameter. This routine will work on 8086+
; processors. This code takes advantage of packed BCD to
; determine the ASCII values to print. This code could have
; used compare and branch to do the same or a translation table.
print_hex_word:
push bp
mov bp, sp ; BP=SP, on 8086 can't use sp in memory operand
push dx ; Save all registers we clobber
push cx
push bx
push ax
mov cx, 0x0404 ; CH = number of nibbles to process = 4 (4*4=16 bits)
; CL = Number of bits to rotate each iteration = 4 (a nibble)
mov dx, [bp+4] ; DX = word parameter on stack at [bp+4] to print
mov bx, [bp+6] ; BX = page / foreground attr is at [bp+6]
.loop:
rol dx, cl ; Roll 4 bits left. Lower nibble is value to print
mov ax, 0x0e0f ; AH=0E (BIOS tty print),AL=mask to get lower nibble
and al, dl ; AL=copy of lower nibble
add al, 0x90 ; Work as if we are packed BCD
daa ; Decimal adjust after add.
; If nibble in AL was between 0 and 9, then CF=0 and
; AL=0x90 to 0x99
; If nibble in AL was between A and F, then CF=1 and
; AL=0x00 to 0x05
adc al, 0x40 ; AL=0xD0 to 0xD9
; or AL=0x41 to 0x46
daa ; AL=0x30 to 0x39 (ASCII '0' to '9')
; or AL=0x41 to 0x46 (ASCII 'A' to 'F')
int 0x10 ; Print ASCII character in AL
dec ch
jnz .loop ; Go back if more nibbles to process
pop ax ; Restore registers
pop bx
pop cx
pop dx
pop bp
ret
TIMES 510-($-$$) db 0
DW 0xaa55
print_hex_word
的80186+版本使用PUSHA和POPA来保存和恢复寄存器 AX , CX ,< em> DX , BX ,原始 SP , BP , SI 和 DI :
; Print 16 bit value passed on stack as first parameter
; in hexadecimal. This routine will work on 80186+ processors
; Use page number and foreground color passed in second parameter
print_hex_word:
pusha ; Save all registers, 16 bytes total
mov bp, sp ; BP=SP, on 8086 can't use sp in memory operand
mov cx, 0x0404 ; CH = number of nibbles to process = 4 (4*4=16 bits)
; CL = Number of bits to rotate each iteration = 4 (a nibble)
mov dx, [bp+18] ; DX = word parameter on stack at [bp+18] to print
mov bx, [bp+20] ; BX = page / foreground attr is at [bp+20]
.loop:
rol dx, cl ; Roll 4 bits left. Lower nibble is value to print
mov ax, 0x0e0f ; AH=0E (BIOS tty print),AL=mask to get lower nibble
and al, dl ; AL=copy of lower nibble
add al, 0x90 ; Work as if we are packed BCD
daa ; Decimal adjust after add.
; If nibble in AL was between 0 and 9, then CF=0 and
; AL=0x90 to 0x99
; If nibble in AL was between A and F, then CF=1 and
; AL=0x00 to 0x05
adc al, 0x40 ; AL=0xD0 to 0xD9
; or AL=0x41 to 0x46
daa ; AL=0x30 to 0x39 (ASCII '0' to '9')
; or AL=0x41 to 0x46 (ASCII 'A' to 'F')
int 0x10 ; Print ASCII character in AL
dec ch
jnz .loop ; Go back if more nibbles to process
popa ; Restore all the registers
ret
代码使用一些打包的二进制编码十进制(BCD)技巧将4位值转换为十六进制数字。有关压缩BCD算法的更多信息,请参阅 Processing Packed BCD Numbers 部分中的tutorial。
要组装此引导加载程序,您可以使用:
nasm -f bin boot.asm -o boot.bin
可以在Linux命令行中使用 QEMU 进行测试,如下所示:
qemu-system-i386 -fda boot.bin
如果您使用BOCHS,您可以使用其内置调试器逐步执行引导加载程序,并在代码执行时显示寄存器和内存的内容。