printf.s 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. .define _printf, _putchar, _getal, _char
  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: divul.l #10, d2:d1 ! d1 <- qotient; d2 <- remainder
  51. add.l #'0', d2
  52. move.b d2, -(a2)
  53. tst.l d1 ! if quotient = 0 then ready
  54. bne 1b
  55. bra sloop ! print digitstring.
  56. hex: move.l (a6)+, d1 ! d1 <- integer
  57. move.l #_getal+12, a2 ! a2 <- ptr to last part of buf
  58. clr.b -(a2) ! stringterminator
  59. move.l #7, d2 ! loop control
  60. 1: move.l d1, d0
  61. and.l #15, d0
  62. move.b (hexs,d0.w), -(a2) ! hex digit
  63. asr.l #4, d1
  64. dbf d2, 1b
  65. bra sloop
  66. out:
  67. movem.l (sp)+, d0/d1/d2/a0/a1/a2/a3/a4/a5/a6
  68. rts
  69. _putchar:
  70. move.l #1, -(sp)
  71. pea (11,sp)
  72. move.l #1, -(sp)
  73. jsr (_write)
  74. lea (12, sp), sp
  75. rts
  76. .align 2