Skip to content

Commit 04625d9

Browse files
committed
fixed bug in print_hex
1 parent ee0eefa commit 04625d9

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

05-bootsector-functions-strings/boot_sect_main.asm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ call print
1111

1212
call print_nl
1313

14-
mov dx, 0x1234
14+
mov dx, 0x12fe
1515
call print_hex
1616

1717
; that's it! we can hang now

05-bootsector-functions-strings/boot_sect_print_hex.asm

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ print_hex:
66
mov cx, 0 ; our index variable
77

88
; Strategy: get the last char of 'dx', then convert to ASCII
9-
; ASCII values: '0' (ASCII 0x30) to '9' (0x39), so just add 0x30 to byte N.
9+
; Numeric ASCII values: '0' (ASCII 0x30) to '9' (0x39), so just add 0x30 to byte N.
10+
; For alphabetic characters A-F: 'A' (ASCII 0x41) to 'F' (0x46) we'll add 0x40
1011
; Then, move the ASCII byte to the correct position on the resulting string
1112
loop:
1213
cmp cx, 4 ; loop 4 times
@@ -15,13 +16,17 @@ loop:
1516
; 1. convert last char of 'dx' to ascii
1617
mov ax, dx ; we will use 'ax' as our working register
1718
and ax, 0x000f ; 0x1234 -> 0x0004 by masking first three to zeros
18-
add ax, 0x30 ; add 0x30 to N to convert it to ASCII "N"
19+
add al, 0x30 ; add 0x30 to N to convert it to ASCII "N"
20+
cmp al, 0x39 ; if > 9, add extra 8 to represent 'A' to 'F'
21+
jle step2
22+
add al, 7 ; 'A' is ASCII 65 instead of 58, so 65-58=7
1923

24+
step2:
2025
; 2. get the correct position of the string to place our ASCII char
2126
; bx <- base address + string length - index of char
2227
mov bx, HEX_OUT + 5 ; base + length
2328
sub bx, cx ; our index variable
24-
or [bx], ax ; copy the ASCII char on 'ax' to the position pointed by 'bx'
29+
mov [bx], al ; copy the ASCII char on 'al' to the position pointed by 'bx'
2530
ror dx, 4 ; 0x1234 -> 0x4123 -> 0x3412 -> 0x2341 -> 0x1234
2631

2732
; increment index and loop

0 commit comments

Comments
 (0)