boot_sect_print_hex.asm 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. mov cx, 0 ; our index variable
  6. ; Strategy: get the last char of 'dx', then convert to ASCII
  7. ; Numeric ASCII values: '0' (ASCII 0x30) to '9' (0x39), so just add 0x30 to byte N.
  8. ; For alphabetic characters A-F: 'A' (ASCII 0x41) to 'F' (0x46) we'll add 0x40
  9. ; Then, move the ASCII byte to the correct position on the resulting string
  10. loop:
  11. cmp cx, 4 ; loop 4 times
  12. je end
  13. ; 1. convert last char of 'dx' to ascii
  14. mov ax, dx ; we will use 'ax' as our working register
  15. and ax, 0x000f ; 0x1234 -> 0x0004 by masking first three to zeros
  16. add al, 0x30 ; add 0x30 to N to convert it to ASCII "N"
  17. cmp al, 0x39 ; if > 9, add extra 8 to represent 'A' to 'F'
  18. jle step2
  19. add al, 7 ; 'A' is ASCII 65 instead of 58, so 65-58=7
  20. step2:
  21. ; 2. get the correct position of the string to place our ASCII char
  22. ; bx <- base address + string length - index of char
  23. mov bx, HEX_OUT + 5 ; base + length
  24. sub bx, cx ; our index variable
  25. mov [bx], al ; copy the ASCII char on 'al' to the position pointed by 'bx'
  26. ror dx, 4 ; 0x1234 -> 0x4123 -> 0x3412 -> 0x2341 -> 0x1234
  27. ; increment index and loop
  28. add cx, 1
  29. jmp loop
  30. end:
  31. ; prepare the parameter and call the function
  32. ; remember that print receives parameters in 'bx'
  33. mov bx, HEX_OUT
  34. call print
  35. popa
  36. ret
  37. HEX_OUT:
  38. db '0x0000',0 ; reserve memory for our new string