printf.s 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. .define printf
  2. .sect .text
  3. .sect .rom
  4. .sect .data
  5. .sect .bss
  6. .sect .text
  7. printf:
  8. popl saveret, *RR14
  9. ldm savereg, R4, $10
  10. ld R3, $buff !R3 is pointer to a buffer, in which
  11. !we built the string to be printed.
  12. pop R2, *RR14 !R2 is pointer to format-string
  13. prloop:
  14. ldb RL0, 0(R2)
  15. testb RL0
  16. jr EQ, ready
  17. inc R2
  18. cpb RL0, $045 ! '%'?
  19. jr NE, 1f
  20. ldb RL0, 0(R2)
  21. inc R2
  22. cpb RL0, $0163 ! 's'?
  23. jr EQ, 3f
  24. cpb RL0, $0170 ! 'x'?
  25. ld R4, $16 ! print number hexadecimal
  26. jr EQ, 2f
  27. cpb RL0, $0144 ! 'd'?
  28. ld R4, $10 ! print number decimal
  29. jr EQ, 2f
  30. cpb RL0, $0157 ! 'o'?
  31. ld R4, $8 ! print number octal
  32. jr EQ, 2f
  33. 1: ldb 0(R3), RL0
  34. inc R3
  35. jr prloop
  36. 2: !in case of %x, %d or %o
  37. pop R1, *RR14
  38. test R1
  39. jr PL, 4f
  40. cp R4, $10
  41. jr NE, 4f ! print only '-' in case of %d
  42. ldb 0(R3), $055 ! '-'
  43. inc R3
  44. neg R1
  45. 4: calr printn
  46. jr prloop
  47. 3: !in case of %s
  48. pop R1, *RR14
  49. 6: ldb RL0, 0(R1)
  50. testb RL0
  51. jr EQ, prloop
  52. inc R1
  53. ldb 0(R3), RL0
  54. inc R3
  55. jr 6b
  56. ready: !now really print the string we built in the buffer
  57. ldb 0(R3), RL0 !end string with '\0'
  58. sub R3, $buff-1 !R3 contains the number of characters
  59. ld R1, $buff
  60. push *RR14, R3 !count
  61. push *RR14, R1 !buffer
  62. push *RR14, $2 !file descriptor
  63. calr WRITE
  64. inc R15, $6
  65. ldm R4, savereg, $10
  66. pushl *RR14, saveret
  67. ret
  68. printn:
  69. ldk R0, $0
  70. div RR0, R4 !%x, %d or %o determined by R4
  71. test R1
  72. jr EQ, 5f !if quotient is '0' printn is ready
  73. push *RR14, R0 !push remainder onto the stack
  74. calr printn
  75. pop R0, *RR14
  76. 5: add R0, $060
  77. cp R0, $071 !'9'
  78. jr LE, 8f
  79. add R0, $7
  80. 8: ldb 0(R3), RL0
  81. inc R3
  82. ret
  83. .sect .data
  84. buff:
  85. .space 256