在NASM中打印一个数字 - 构建一个x86 Bootsector

时间:2015-06-10 18:14:52

标签: assembly nasm

我刚刚开始使用汇编语言,我试图在控制台上打印数字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中断。那么,我应该使用什么中断代码?

1 个答案:

答案 0 :(得分:2)

以下是我教授的课程示例。这是一个原始的bootsector,你可以直接编译为目标文件,并在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