1
0

boot_sect_print_hex.asm 1.3 KB

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