为什么不打印X?部件

时间:2016-11-19 07:13:56

标签: assembly x86 nasm bootloader bios

为什么这个程序不打印X?有人可以一步一步地解释发生什么事情,使其打印出一个铲子作为输出。

mov ah, 0x0e ; Set higher bit of ax to 0e (probably the instruction opcode)
mov al, the_secret ; Set lower bit of ax to bytes of H
int 0x10

the_secret:
    db 'X'

jmp $ ;Jump to the current address forever

; Padding

times 510 - ($-$$) db 0

dw 0xaa55

1 个答案:

答案 0 :(得分:3)

...使其打印 作为输出。

...现在我真的无法为您提供帮助,因为我不知道♠️字符的确切十六进制-> ASCII值...尽管,我可以帮助您理解简单的步骤来打印标准(按键)ASCII字符。

这是完整的 NASM 示例,说明如何将字符打印到监视器显示屏上;我将其分解为特定于视频显示的块,以帮助您更好地理解该过程(请注意,我仅使用了 REAL MODE em>示例中的汇编指令(16位):

[bits   16]                                             ; Declare CPU Instructions as [REAL_MODE]
[org    0x0000]                                         ; Organize Memory to Start @ [0x0000]
                                                        ; We'll Manually setup Memory Organization Later...


jmp _glEntry                                            ; Relocate to Function:



;========================================================
; This Function Prints an Entire Character Stream until
; NULL Terminated Hex Character (0x00) is Detected...
;========================================================
output16_char_array:                                    ; Legacy BIOS Output CHAR_STREAM Function
    ;push   ax                                          ; Store [AX] Register to [STACK_SPACE]
    xor ax, ax                                          ; Zero [AX] Register
    mov ah, 0x0E                                        ; Assign [0x0E] to [AX] : (Legacy BIOS Function Code for BIOS Display)
                                                        ; Continue Code Execution:
    .output16_char_array_loop:
        lodsb                                           ; Collect Single Byte from [DS:SI] and increment SI by 1 : ([AL] = [DS:SI]; inc [SI])
        or  al, al                                      ; Check Whether [AL] is [0x00]
        jz  .output16_char_array_done                   ; [AL] == [0x00] Proceed to Function:
        int 0x10                                        ; [AL] != [0x00] Continue to Display Hex->ASCII [CHAR]
        jmp .output16_char_array_loop                   ; Relocate [CS] to create an Infinite Loop...
    
    .output16_char_array_done:
        ;pop    ax                                      ; Restore [AX] Register from [STACK_SPACE]
        ret                                             ; Return to Calling [CS] Memory Position...



;========================================================
; For Simplicity I've created a Single Character Output
; Method, but in the form of a MACRO
;
; There is really no difference in the method if Output
; other than that this method only prints a single
; Character and doesn't require a NULL Terminated Hex
; Character (0x00) for EndOf(..) CHAR_STREAM...
;========================================================
%macro  output16_char_byte  1                           ; Define Macro with a single input method...
    ;push   ax                                          ; Store [AX] Register to [STACK_SPACE]
    xor ax, ax                                          ; Zero [AX] Register
    mov ah, 0x0E                                        ; Assign [0x0E] to [AH] : (Legacy BIOS Function Code for BIOS Display)
    mov al, %1                                          ; Assign [MACRO_INPUT] to [AL] : (Refer to ['%macro output16_char_byte <..>'])
    int 0x10                                            ; Legacy BIOS Interrupt for Teletype Output
    xor ax, ax                                          ; Zero [AX] Register
    ;pop    ax                                          ; Restore [AX] Register from [STACK_SPACE]
%endmacro                                               ; End of ['output16_char_byte'] Macro


WELCOME_MSG db  "Hello, World!",    0x0D,   0x0A,   0x00



;========================================================
; This Function is the Entry Point for the Global Master
; Boot Record
;========================================================
_glEntry:
    cli                                                 ; Disable CPU Interrupts
    xor ax, ax                                          ; Zero [AX] Register
    mov ax, 0x07E0                                      ; Assign [0x07E0] to [AX] : (0x07E0)
    mov ss, ax                                          ; Assign [AX] to [SS] : ([AX]*[0x0010]) : (0x7E00)
    mov sp, 0x1000                                      ; Assign [0x1000] to [SP] : (0x1000) ([0x07E0]:[0x1000])
    sti                                                 ; Enable CPU Interrupts
                                                        ; Continue Code Execution:
    xor ax, ax                                          ; Zero [AX] Register
    mov ax, 0x07C0                                      ; Assign [0x07C0] to [AX] : (0x07C0)
    mov ds, ax                                          ; Assign [AX] to [DS] : ([AX]*[0x0010]) : (0x7C00)
                                                        ; Continue Code Execution:
    mov si, WELCOME_MSG                                 ; Assign [WELCOME_MSG] to [SI] : (0x[....])
    call    output16_char_array                         ; Call our Output [CHAR_STREAM] Function
    
    mov si, WELCOME_MSG                                 ; Assign [WELCOME_MSG] to [SI] : (0x[....])
    output16_char_byte  BYTE    [ds:si+0x0008]          ; Assign (BYTE) [DS:SI+0x0008] to Macro Input Method...
                                                        ; we should have the (9th - 1) letter from [WELCOME_MSG] 'o'
    
                                                        ; Continue Code Execution on Return:
    xor ax, ax                                          ; Zero [AX] Register
    int 0x16                                            ; Call BIOS Await Keystroke Interrupt
    xor ax, ax                                          ; Zero [AX] Register
    int 0x19                                            ; Call BIOS Reboot Interrupt
                                                        ; Continue Code Execution if interrupts fail...
    cli                                                 ; Disable CPU Interrupts
    hlt                                                 ; Halt CPU Execution:



times   510 - ( $ - $$ )    db  0x00                    ; Pad [BOOTSECTOR] with [0x00] up to 510 Bytes...
dw  0xAA55                                              ; Assign [MAGIC_MBR] Value...

好的,这就是将字符打印到监视器/显示器上的整个过程... here's photo evidence of the true output:

最常见/最有效的将字符打印/显示到显示器的方法:

;========================================================
; This Function Prints an Entire Character Stream until
; NULL Terminated Hex Character (0x00) is Detected...
;========================================================
output16_char_array:                                    ; Legacy BIOS Output CHAR_STREAM Function
    ;push   ax                                          ; Store [AX] Register to [STACK_SPACE]
    xor ax, ax                                          ; Zero [AX] Register
    mov ah, 0x0E                                        ; Assign [0x0E] to [AX] : (Legacy BIOS Function Code for BIOS Display)
                                                        ; Continue Code Execution:
    .output16_char_array_loop:
        lodsb                                           ; Collect Single Byte from [DS:SI] and increment SI by 1 : ([AL] = [DS:SI]; inc [SI])
        or  al, al                                      ; Check Whether [AL] is [0x00]
        jz  .output16_char_array_done                   ; [AL] == [0x00] Proceed to Function:
        int 0x10                                        ; [AL] != [0x00] Continue to Display Hex->ASCII [CHAR]
        jmp .output16_char_array_loop                   ; Relocate [CS] to create an Infinite Loop...

    .output16_char_array_done:
        ;pop    ax                                      ; Restore [AX] Register from [STACK_SPACE]
        ret                                             ; Return to Calling [CS] Memory Position...

我想指出,我已评论push axpop ax,因为在此简单示例中不需要这些说明。尽管,这些指令只是(在主题之外)预先执行了一个存储器堆栈存储(push)和/或恢复(pop)到AX寄存器。

mov ah, 0x0E非常简单...它将值0x0E分配给AH,这就是int 0x10的{​​{3}}。

lodsb这是执行以下操作的更有效方式:

mov    al,    BYTE    [SI]           ; Assign Single [BYTE] from [SI] to [AL]
inc    si                            ; Increment [SI] by 1 equivalent to [SI]++

or al, al检查[AL]是否等于0x00。这是字符流的NULL终止字符。

jz .output16_char_array_done如果[AL]等于0x00,则在.output16_char_array_done执行代码...如果[AL] 等于0x00继续执行代码...

jmp .output16_char_array_loop一种简单的方法,可以在不使用loop指令的情况下循环迭代某个功能/句柄(loop更多的硬件资源需要执行,因此除非在您的特定情况下更有效,否则通常避免使用此指令。

ret一条简单的指令,返回到先前的(调用)内存地址。

int 0x10这是Teletype Output Function