Advertisement
arealloudbird

Untitled

Feb 7th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. ; number to print in decimal is in R3.
  2. ; it will be positive.
  3.  
  4. ; The way DIV works is as follows:
  5. ; input R3, R4
  6. ; out R0-quotient, R1-remainder
  7.  
  8. ; PUSH
  9. ; IN:R0, OUT:R5 (0-success, 1-fail/overflow)
  10. ; R3: STACK_END R4: STACK_TOP
  11.  
  12. ; POP
  13. ; OUT: R0, OUT R5 (0-success, 1-fail/underflow)
  14. ; R3 STACK_START R4 STACK_TOP
  15.  
  16. .ORIG x3000
  17.  
  18. AND R4, R4, #0 ; set up R4
  19. ADD R4, R4, #10
  20.  
  21. MAIN_LOOP
  22. JSR DIV
  23.  
  24. AND R3, R3, #0
  25. ADD R3, R0, #0 ; Store quotient back into R3
  26.  
  27. AND R0, R0, #0 ; put the value of R1-remainder onto R0
  28. ADD R0, R1, #0 ; and then push it onto stack
  29. JSR PUSH
  30.  
  31. ADD R3, R3, #0 ; set nzp bits for R3
  32.  
  33. BRp MAIN_LOOP ; if quotient is not zero, keep looping
  34.  
  35.  
  36. TOP_OF_PRINT_LOOP
  37. JSR POP
  38.  
  39. ADD R5, R5, #-1 ; if nothing was left to pop, we're done
  40. BRz DONE
  41.  
  42. LD R1, ASCII_0 ; add ascii offset of '0'
  43. ADD R0, R0, R1
  44.  
  45. OUT ; print
  46.  
  47. BR TOP_OF_PRINT_LOOP ; next value in stack
  48.  
  49. DONE
  50. HALT
  51.  
  52.  
  53. ASCII_0 .FILL x30
  54. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  55. ;input R3, R4
  56. ;out R0-quotient, R1-remainder
  57. DIV
  58. AND R0, R0, #0 ; Clear R0, R1, and R2
  59. AND R1, R1, #0
  60. AND R2, R2, #0
  61.  
  62. ADD R1, R1, R3 ; R1 holds R3
  63. NOT R2, R4 ; R2 holds negative R4
  64. ADD R2, R2, #1
  65.  
  66. TOP_OF_DIVISION_LOOP
  67. ADD R1, R1, R2 ; Subtract the value of R3
  68. BRp POSITIVE
  69. BRn NEGATIVE
  70. BRz ZERO
  71.  
  72. POSITIVE
  73. ADD R0, R0, #1 ; Increment quotient result
  74. BR TOP_OF_DIVISION_LOOP
  75. NEGATIVE
  76. ADD R1, R1, R4 ; Bring the remainder back to a positive result
  77. BR DIV_DONE
  78. ZERO
  79. ADD R0, R0, #1 ; Increment quotient result
  80. BR DIV_DONE ; Since the divison was clean,
  81. ; nothing else needs to be done
  82.  
  83. DIV_DONE
  84. RET
  85.  
  86. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  87. ;IN:R0, OUT:R5 (0-success, 1-fail/overflow)
  88. ;R3: STACK_END R4: STACK_TOP
  89. ;
  90. PUSH
  91. ST R3, PUSH_SaveR3 ;save R3
  92. ST R4, PUSH_SaveR4 ;save R4
  93. AND R5, R5, #0 ;
  94. LD R3, STACK_END ;
  95. LD R4, STACk_TOP ;
  96. ADD R3, R3, #-1 ;
  97. NOT R3, R3 ;
  98. ADD R3, R3, #1 ;
  99. ADD R3, R3, R4 ;
  100. BRz OVERFLOW ;stack is full
  101. STR R0, R4, #0 ;no overflow, store value in the stack
  102. ADD R4, R4, #-1 ;move top of the stack
  103. ST R4, STACK_TOP ;store top of stack pointer
  104. BRnzp DONE_PUSH ;
  105. OVERFLOW
  106. ADD R5, R5, #1 ;
  107. DONE_PUSH
  108. LD R3, PUSH_SaveR3 ;
  109. LD R4, PUSH_SaveR4 ;
  110. RET
  111.  
  112.  
  113. PUSH_SaveR3 .BLKW #1 ;
  114. PUSH_SaveR4 .BLKW #1 ;
  115.  
  116.  
  117. ;OUT: R0, OUT R5 (0-success, 1-fail/underflow)
  118. ;R3 STACK_START R4 STACK_TOP
  119. ;
  120. POP
  121. ST R3, POP_SaveR3 ;save R3
  122. ST R4, POP_SaveR4 ;save R3
  123. AND R5, R5, #0 ;clear R5
  124. LD R3, STACK_START ;
  125. LD R4, STACK_TOP ;
  126. NOT R3, R3 ;
  127. ADD R3, R3, #1 ;
  128. ADD R3, R3, R4 ;
  129. BRz UNDERFLOW ;
  130. ADD R4, R4, #1 ;
  131. LDR R0, R4, #0 ;
  132. ST R4, STACK_TOP ;
  133. BRnzp DONE_POP ;
  134. UNDERFLOW
  135. ADD R5, R5, #1 ;
  136. DONE_POP
  137. LD R3, POP_SaveR3 ;
  138. LD R4, POP_SaveR4 ;
  139. RET
  140.  
  141.  
  142. POP_SaveR3 .BLKW #1 ;
  143. POP_SaveR4 .BLKW #1 ;
  144. STACK_END .FILL x3FF0 ;
  145. STACK_START .FILL x4000 ;
  146. STACK_TOP .FILL x4000 ;
  147.  
  148. .END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement