Using EMU8086, is there a direct way to print a variable's hex value?

后端 未结 1 1939
执笔经年
执笔经年 2021-01-25 03:00

I\'m using EMU8086 on a Windows 7 HP x64, Intel i3-2330m PC.

I have spent about two weeks researching and tinkering with this Assembly language program in an effort to p

相关标签:
1条回答
  • 2021-01-25 03:31

    DOS only provides you with interrupts to write characters to the standard output (INT 21H / AH=2 for a single character, and INT 21H / AH=9 for a $-terminated string of characters).

    So you will have to convert any number you want to print into a string first, in whatever base you wish to display the number in. Fortunately that's very easy to do. Below is an example of how one could print 32-bit hexadecimal numbers (I'll leave it as an exercise to you to strip the leading zeroes):

    ; Input:
    ; EAX = value to print
    ;
    print_hex:
        mov cx,8        ; print 8 hex digits (= 32 bits)
        .print_digit:
            rol eax,4   ; move the currently left-most digit into the least significant 4 bits
            mov dl,al
            and dl,0xF  ; isolate the hex digit we want to print
            add dl,'0'  ; and convert it into a character..
            cmp dl,'9'  ; ...
            jbe .ok     ; ...
            add dl,7    ; ... (for 'A'..'F')
        .ok:            ; ...
            push eax    ; save EAX on the stack temporarily
            mov ah,2    ; INT 21H / AH=2: write character to stdout
            int 0x21
            pop eax     ; restore EAX
            loop .print_digit
            ret
    

    Printing numbers in base 10 or base 2 is not that much different. It's just that when we deal with bases that are powers of two (like 16 or 2), we can use shifts (or rotates in my code) and ANDs rather than division and remainders.

    0 讨论(0)
提交回复
热议问题