Last active
June 10, 2019 19:03
-
-
Save KowalczykBartek/c180f17779f77a6717f74946c946d63e to your computer and use it in GitHub Desktop.
print numberic value from given register as hex on the screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Super highly inspired by https://gist.github.com/kthompson/957c635d84b7813945aa9bb649f039b9 | |
[org 0x7c00] | |
mov dx, 0x1fb6 ; set function argument | |
call print_hex ; call the function | |
jmp $ | |
; arg0: dx - numeric value to be printed in hexdecimal format | |
print_hex: | |
pusha ; | |
mov cx, 4 ; we are in 16bit mode, so our 16bit value has 4 * 4bytes (4bytes = one char) | |
mov bx, HEX_OUT | |
add bx, 2 ; skip '0x' charactes | |
char_loop: | |
dec cx | |
mov ax, dx | |
shr dx, 4 | |
and ax, 0xf | |
mov bx, HEX_OUT ; set bx to the memory address of our string | |
add bx, 2 ; skip the '0x' | |
add bx, cx ; add the current counter to the address | |
cmp ax, 9 | |
jle numeric | |
jmp character | |
character: | |
sub ax, 10 | |
add ax, 97 | |
jmp add_letter | |
numeric: | |
add ax, 48 | |
jmp add_letter | |
add_letter: | |
mov byte [bx], al | |
cmp cx, 0 | |
je done | |
jmp char_loop | |
done: | |
call print_string | |
popa | |
ret | |
; arg0: bx - pointer to string to be printed | |
print_string: | |
pusha | |
iteration: | |
mov cx, [bx] | |
cmp cl, 0 | |
je end | |
; print character | |
mov al, cl | |
mov ah, 0x0e | |
int 0x10 | |
; increment string pointer | |
add bx, 1 | |
jmp iteration | |
end: | |
popa | |
ret | |
HEX_OUT: db '0x0000', 0 | |
times 510-($-$$) db 0 | |
dw 0xaa55 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment