boot_sect_print_hex.asm 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. loop:
  7. cmp cx, 4 ; loop 4 times
  8. je end
  9. mov ax, dx ; we will work on ax
  10. and ax, 0x000f ; 0x1234 -> 0x0004 by masking first three to zeros
  11. ; convert each hex value to its ASCII value. '0' (ASCII 0x30) to '9' (0x39)
  12. ; We will need to convert byte N to byte 0x3N by just adding 0x30
  13. add ax, 0x30 ; 0x0004 -> ASCII '4'
  14. ; bx <- address of the character to replace = base + string length - index
  15. mov bx, HEX_OUT + 5 ; last char of string, beware not to replace null char
  16. sub bx, cx ; address of char to replace
  17. or [bx], ax ; copy the ASCII char to the string position
  18. ror dx, 4 ; 0x1234 -> 0x4123 -> 0x3412 -> 0x2341 -> 0x1234
  19. ; increment index and loop
  20. add cx, 1
  21. jmp loop
  22. end:
  23. ; prepare the parameter and call the function
  24. ; remember that print receives parameters in 'bx'
  25. mov bx, HEX_OUT
  26. call print
  27. popa
  28. ret
  29. HEX_OUT:
  30. db '0x0000',0 ; reserve memory for our new string