printf.s 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. .define _printf
  2. .sect .text
  3. .sect .rom
  4. .sect .data
  5. .sect .bss
  6. .sect .bss
  7. _getal:
  8. .space 12
  9. _char:
  10. .space 1
  11. .align 4
  12. .sect .data
  13. hexs:
  14. .ascii "0123456789abcdef"
  15. .align 4
  16. .sect .text
  17. _printf:
  18. movem.l d0/d1/d2/a0/a1/a2/a3/a4/a5/a6, -(sp)
  19. lea 44(sp), a6 ! a6 <- address of arguments
  20. move.l (a6)+, a5 ! a5 <- address of format
  21. next: move.b (a5)+, d0
  22. beq out
  23. cmp.b #'%', d0
  24. beq procnt
  25. put: move.l d0, -(sp)
  26. jsr _putchar ! long argument on stack
  27. tst.l (sp)+
  28. bra next
  29. procnt: move.b (a5)+, d0
  30. cmp.b #'d', d0 ! NOTE: %d means unsigned.
  31. beq digit
  32. cmp.b #'x', d0
  33. beq hex
  34. cmp.b #'s', d0
  35. beq string
  36. cmp.b #'%', d0 ! second % has to be printed.
  37. beq put
  38. tst.b -(a5) ! normal char should be printed
  39. bra next
  40. string: move.l (a6)+, a2 ! a2 <- address of string
  41. sloop: move.b (a2)+, d0
  42. beq next
  43. move.l d0, -(sp)
  44. jsr _putchar ! long argument on stack
  45. tst.l (sp)+
  46. bra sloop
  47. digit: move.l (a6)+, d1 ! d1 <- integer
  48. move.l #_getal+12, a2 ! a2 <- ptr to last part of buf
  49. clr.b -(a2) ! stringterminator
  50. 1:
  51. move.l d1,-(sp)
  52. move.l #10,-(sp)
  53. jsr .dvu ! d1 <- qotient; d0 <- remainder
  54. add.l #'0', d0
  55. move.b d0, -(a2)
  56. tst.l d1 ! if quotient = 0 then ready
  57. bne 1b
  58. bra sloop ! print digitstring.
  59. hex: move.l (a6)+, d1 ! d1 <- integer
  60. move.l #_getal+12, a2 ! a2 <- ptr to last part of buf
  61. clr.b -(a2) ! stringterminator
  62. move.l #7, d2 ! loop control
  63. 1: move.l d1, d0
  64. and.l #15, d0
  65. move.l #hexs,a0
  66. add.l d0,a0
  67. move.b (a0), -(a2) ! hex digit
  68. asr.l #4, d1
  69. dbf d2, 1b
  70. bra sloop
  71. out:
  72. movem.l (sp)+, d0/d1/d2/a0/a1/a2/a3/a4/a5/a6
  73. rts
  74. _putchar:
  75. move.l #1, -(sp)
  76. pea 11(sp)
  77. move.l #1, -(sp)
  78. jsr _write
  79. lea 12(sp), sp
  80. rts
  81. .align 2