Printing out a number in assembly language?

后端 未结 10 1921
感动是毒
感动是毒 2020-12-03 04:39
mov al,10
add al,15

How do I print the value of \'al\'?

10条回答
  •  無奈伤痛
    2020-12-03 05:14

    Assuming you are writing a bootloader or other application that has access to the BIOS, here is a rough sketch of what you can do:

    • Isolate the first digit of the hex byte
    • If it is greater than 9 (i.e. 0x0A to 0x0F), subtract 10 from it (scaling it down to 0 to 5), and add 'A' (0x41).
    • If it is less than or equal to 9 (i.e. 0x00 to 0x09), add '0' to it.
    • Repeat this with the next hex digit.

    Here is my implementation of this:

    ; Prints AL in hex.
    printhexb:
        push ax
        shr al, 0x04
        call print_nibble
        pop ax
        and al, 0x0F
        call print_nibble
        ret
    print_nibble:
        cmp al, 0x09
        jg .letter
        add al, 0x30
        mov ah, 0x0E
        int 0x10
        ret
    .letter:
        add al, 0x37
        mov ah, 0x0E
        int 0x10
        ret   
    

提交回复
热议问题