|
| 1 | +; receiving the data in 'dx' |
| 2 | +; For the examples we'll assume that we're called with dx=0x1234 |
| 3 | +print_hex: |
| 4 | + pusha |
| 5 | + |
| 6 | + mov cx, 0 ; our index variable |
| 7 | + |
| 8 | +; Strategy: get the last char of 'dx', then convert to ASCII |
| 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 |
| 11 | +; Then, move the ASCII byte to the correct position on the resulting string |
| 12 | +hex_loop: |
| 13 | + cmp cx, 4 ; loop 4 times |
| 14 | + je end |
| 15 | + |
| 16 | + ; 1. convert last char of 'dx' to ascii |
| 17 | + mov ax, dx ; we will use 'ax' as our working register |
| 18 | + and ax, 0x000f ; 0x1234 -> 0x0004 by masking first three to zeros |
| 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 |
| 23 | + |
| 24 | +step2: |
| 25 | + ; 2. get the correct position of the string to place our ASCII char |
| 26 | + ; bx <- base address + string length - index of char |
| 27 | + mov bx, HEX_OUT + 5 ; base + length |
| 28 | + sub bx, cx ; our index variable |
| 29 | + mov [bx], al ; copy the ASCII char on 'al' to the position pointed by 'bx' |
| 30 | + ror dx, 4 ; 0x1234 -> 0x4123 -> 0x3412 -> 0x2341 -> 0x1234 |
| 31 | + |
| 32 | + ; increment index and loop |
| 33 | + add cx, 1 |
| 34 | + jmp hex_loop |
| 35 | + |
| 36 | +end: |
| 37 | + ; prepare the parameter and call the function |
| 38 | + ; remember that print receives parameters in 'bx' |
| 39 | + mov bx, HEX_OUT |
| 40 | + call print |
| 41 | + |
| 42 | + popa |
| 43 | + ret |
| 44 | + |
| 45 | +HEX_OUT: |
| 46 | + db '0x0000',0 ; reserve memory for our new string |
0 commit comments