Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.6KB

  1. ; *** Requirements ***
  2. ; printnstr
  3. ;
  4. ; *** Variables ***
  5. ; Used to store formatted hex values just before printing it.
  6. .equ STDIO_HEX_FMT STDIO_RAMSTART
  7. .equ STDIO_RAMEND @+2
  8. ; *** Code ***
  9. ; Format the lower nibble of A into a hex char and stores the result in A.
  10. fmtHex:
  11. ; The idea here is that there's 7 characters between '9' and 'A'
  12. ; in the ASCII table, and so we add 7 if the digit is >9.
  13. ; daa is designed for using Binary Coded Decimal format, where each
  14. ; nibble represents a single base 10 digit. If a nibble has a value >9,
  15. ; it adds 6 to that nibble, carrying to the next nibble and bringing the
  16. ; value back between 0-9. This gives us 6 of that 7 we needed to add, so
  17. ; then we just condtionally set the carry and add that carry, along with
  18. ; a number that maps 0 to '0'. We also need the upper nibble to be a
  19. ; set value, and have the N, C and H flags clear.
  20. or 0xf0
  21. daa ; now a =0x50 + the original value + 0x06 if >= 0xfa
  22. add a, 0xa0 ; cause a carry for the values that were >=0x0a
  23. adc a, 0x40
  24. ret
  25. ; Formats value in A into a string hex pair. Stores it in the memory location
  26. ; that HL points to. Does *not* add a null char at the end.
  27. fmtHexPair:
  28. push af
  29. ; let's start with the rightmost char
  30. inc hl
  31. call fmtHex
  32. ld (hl), a
  33. ; and now with the leftmost
  34. dec hl
  35. pop af
  36. push af
  37. rra \ rra \ rra \ rra
  38. call fmtHex
  39. ld (hl), a
  40. pop af
  41. ret
  42. ; Print the hex char in A
  43. printHex:
  44. push bc
  45. push hl
  46. ld hl, STDIO_HEX_FMT
  47. call fmtHexPair
  48. ld b, 2
  49. call printnstr
  50. pop hl
  51. pop bc
  52. ret
  53. ; Print the hex pair in HL
  54. printHexPair:
  55. push af
  56. ld a, h
  57. call printHex
  58. ld a, l
  59. call printHex
  60. pop af
  61. ret