fixed bug in print_hex

pull/6/head
Carlos Fenollosa 10 years ago
parent ee0eefad66
commit 04625d9a1f

@ -11,7 +11,7 @@ call print
call print_nl
mov dx, 0x1234
mov dx, 0x12fe
call print_hex
; that's it! we can hang now

@ -6,7 +6,8 @@ print_hex:
mov cx, 0 ; our index variable
; Strategy: get the last char of 'dx', then convert to ASCII
; ASCII values: '0' (ASCII 0x30) to '9' (0x39), so just add 0x30 to byte N.
; Numeric ASCII values: '0' (ASCII 0x30) to '9' (0x39), so just add 0x30 to byte N.
; For alphabetic characters A-F: 'A' (ASCII 0x41) to 'F' (0x46) we'll add 0x40
; Then, move the ASCII byte to the correct position on the resulting string
loop:
cmp cx, 4 ; loop 4 times
@ -15,13 +16,17 @@ loop:
; 1. convert last char of 'dx' to ascii
mov ax, dx ; we will use 'ax' as our working register
and ax, 0x000f ; 0x1234 -> 0x0004 by masking first three to zeros
add ax, 0x30 ; add 0x30 to N to convert it to ASCII "N"
add al, 0x30 ; add 0x30 to N to convert it to ASCII "N"
cmp al, 0x39 ; if > 9, add extra 8 to represent 'A' to 'F'
jle step2
add al, 7 ; 'A' is ASCII 65 instead of 58, so 65-58=7
step2:
; 2. get the correct position of the string to place our ASCII char
; bx <- base address + string length - index of char
mov bx, HEX_OUT + 5 ; base + length
sub bx, cx ; our index variable
or [bx], ax ; copy the ASCII char on 'ax' to the position pointed by 'bx'
mov [bx], al ; copy the ASCII char on 'al' to the position pointed by 'bx'
ror dx, 4 ; 0x1234 -> 0x4123 -> 0x3412 -> 0x2341 -> 0x1234
; increment index and loop

Loading…
Cancel
Save