在 NASM 中打印一个数字 - 构建一个 x86 引导扇区
Print a number in NASM - building an x86 Bootsector
我刚开始弄乱汇编语言,我试图在控制台上打印数字 9。这是我写的:
global _main
section .data
digit equ 9
section .bss
section .text
_main:
mov edx, 1
mov ecx, digit
add ecx, 48
mov ebx, 1
mov eax, 4
int 21h
ret
我知道我可以使用 extern _printf
来完成,但我想尝试使用中断。我以为 21h
是一个 windows 中断。那么,我应该使用什么中断代码?
这是我教授的课程中的一个例子。这是一个原始引导扇区,您可以将其直接编译为目标文件,并在 Qemu、VirtualBox、VMWare、Bochs 或真实机器中用作可引导软盘或 USB 映像。
这利用实模式 BIOS 中断 16 (0x10) 进行字符输出。我认为这就是您要解决的问题。 :)
;
; x86 real mode boot sector template
; David Hoelzer, 2011 - Assembly Bootcamp
;
; x86 architecture systems all support MBR style boot sectors. An
; MBR boot sector must be 512 bytes in length and have machine
; language code originating at 0000:7c00. Additionally, it must
; have the signature "0x55aa" as the final word in the sector or it
; is not a valid boot sector.
; This is a basic Hello World example. Here we will uses BIOS interrupt
; 0x10 which can be used for all manner of screen output. This version uses
; the write-string function, which is int 0x10, ah = 13h:
;
; BIOS Write String: INT 10h
; AH = 13h Function number
; AL - Bit 0 - Update cursor position after writing?
; Bit 1 - String contains attributes?
; BH Video page number
; BL Attributes to apply to string for text only strings
; CX Number of characters to print
; DH Row to start printing at (0,0 is top left corner)
; DL Column to start printing at
; [ES:BP] Far pointer to string to print
org 0x7c00 ; BIOS will load the MBR to this location
; and then jump here to continue execution
mov ax, cs ; Where are we now?
; Could be 0000:7c00 or
; 07c0:0000 or some other
; combo.
mov ds, ax ; Our data is here too.
mov es, ax ; ES:BP is the pointer
; to the string. ES should
; match DS and CS.
mov bp, message ; Offset of our message
mov bh, 0 ; Video page 0
mov bl, 00001111b ; Attributes: Bright white foreground
; on a black background, no flashing
mov cx, [length] ; String length
mov al, 1 ; Bit zero is on: Update position
; Bit one is off: No attributes in string
mov ah, 0x13 ; Function number
mov dx, 0 ; Row,Column = 0,0
int 0x10 ; Call the function
jmp $
message db "Hello, World!"
length db (length - message)
; As stated above, the boot sector must
times 510-($-$$) db 0 ; Create padding to fill out to 510 bytes
dw 0xaa55 ; Magic number in the trailer of a boot sector
; We write it as 0xaa55 because we're little
; endian and it will be reversed to the required
; 0x55 0xaa
我刚开始弄乱汇编语言,我试图在控制台上打印数字 9。这是我写的:
global _main
section .data
digit equ 9
section .bss
section .text
_main:
mov edx, 1
mov ecx, digit
add ecx, 48
mov ebx, 1
mov eax, 4
int 21h
ret
我知道我可以使用 extern _printf
来完成,但我想尝试使用中断。我以为 21h
是一个 windows 中断。那么,我应该使用什么中断代码?
这是我教授的课程中的一个例子。这是一个原始引导扇区,您可以将其直接编译为目标文件,并在 Qemu、VirtualBox、VMWare、Bochs 或真实机器中用作可引导软盘或 USB 映像。
这利用实模式 BIOS 中断 16 (0x10) 进行字符输出。我认为这就是您要解决的问题。 :)
;
; x86 real mode boot sector template
; David Hoelzer, 2011 - Assembly Bootcamp
;
; x86 architecture systems all support MBR style boot sectors. An
; MBR boot sector must be 512 bytes in length and have machine
; language code originating at 0000:7c00. Additionally, it must
; have the signature "0x55aa" as the final word in the sector or it
; is not a valid boot sector.
; This is a basic Hello World example. Here we will uses BIOS interrupt
; 0x10 which can be used for all manner of screen output. This version uses
; the write-string function, which is int 0x10, ah = 13h:
;
; BIOS Write String: INT 10h
; AH = 13h Function number
; AL - Bit 0 - Update cursor position after writing?
; Bit 1 - String contains attributes?
; BH Video page number
; BL Attributes to apply to string for text only strings
; CX Number of characters to print
; DH Row to start printing at (0,0 is top left corner)
; DL Column to start printing at
; [ES:BP] Far pointer to string to print
org 0x7c00 ; BIOS will load the MBR to this location
; and then jump here to continue execution
mov ax, cs ; Where are we now?
; Could be 0000:7c00 or
; 07c0:0000 or some other
; combo.
mov ds, ax ; Our data is here too.
mov es, ax ; ES:BP is the pointer
; to the string. ES should
; match DS and CS.
mov bp, message ; Offset of our message
mov bh, 0 ; Video page 0
mov bl, 00001111b ; Attributes: Bright white foreground
; on a black background, no flashing
mov cx, [length] ; String length
mov al, 1 ; Bit zero is on: Update position
; Bit one is off: No attributes in string
mov ah, 0x13 ; Function number
mov dx, 0 ; Row,Column = 0,0
int 0x10 ; Call the function
jmp $
message db "Hello, World!"
length db (length - message)
; As stated above, the boot sector must
times 510-($-$$) db 0 ; Create padding to fill out to 510 bytes
dw 0xaa55 ; Magic number in the trailer of a boot sector
; We write it as 0xaa55 because we're little
; endian and it will be reversed to the required
; 0x55 0xaa