Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; number to print in decimal is in R3.
- ; it will be positive.
- ; The way DIV works is as follows:
- ; input R3, R4
- ; out R0-quotient, R1-remainder
- ; PUSH
- ; IN:R0, OUT:R5 (0-success, 1-fail/overflow)
- ; R3: STACK_END R4: STACK_TOP
- ; POP
- ; OUT: R0, OUT R5 (0-success, 1-fail/underflow)
- ; R3 STACK_START R4 STACK_TOP
- .ORIG x3000
- AND R4, R4, #0 ; set up R4
- ADD R4, R4, #10
- MAIN_LOOP
- JSR DIV
- AND R3, R3, #0
- ADD R3, R0, #0 ; Store quotient back into R3
- AND R0, R0, #0 ; put the value of R1-remainder onto R0
- ADD R0, R1, #0 ; and then push it onto stack
- JSR PUSH
- ADD R3, R3, #0 ; set nzp bits for R3
- BRp MAIN_LOOP ; if quotient is not zero, keep looping
- TOP_OF_PRINT_LOOP
- JSR POP
- ADD R5, R5, #-1 ; if nothing was left to pop, we're done
- BRz DONE
- LD R1, ASCII_0 ; add ascii offset of '0'
- ADD R0, R0, R1
- OUT ; print
- BR TOP_OF_PRINT_LOOP ; next value in stack
- DONE
- HALT
- ASCII_0 .FILL x30
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;input R3, R4
- ;out R0-quotient, R1-remainder
- DIV
- AND R0, R0, #0 ; Clear R0, R1, and R2
- AND R1, R1, #0
- AND R2, R2, #0
- ADD R1, R1, R3 ; R1 holds R3
- NOT R2, R4 ; R2 holds negative R4
- ADD R2, R2, #1
- TOP_OF_DIVISION_LOOP
- ADD R1, R1, R2 ; Subtract the value of R3
- BRp POSITIVE
- BRn NEGATIVE
- BRz ZERO
- POSITIVE
- ADD R0, R0, #1 ; Increment quotient result
- BR TOP_OF_DIVISION_LOOP
- NEGATIVE
- ADD R1, R1, R4 ; Bring the remainder back to a positive result
- BR DIV_DONE
- ZERO
- ADD R0, R0, #1 ; Increment quotient result
- BR DIV_DONE ; Since the divison was clean,
- ; nothing else needs to be done
- DIV_DONE
- RET
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;IN:R0, OUT:R5 (0-success, 1-fail/overflow)
- ;R3: STACK_END R4: STACK_TOP
- ;
- PUSH
- ST R3, PUSH_SaveR3 ;save R3
- ST R4, PUSH_SaveR4 ;save R4
- AND R5, R5, #0 ;
- LD R3, STACK_END ;
- LD R4, STACk_TOP ;
- ADD R3, R3, #-1 ;
- NOT R3, R3 ;
- ADD R3, R3, #1 ;
- ADD R3, R3, R4 ;
- BRz OVERFLOW ;stack is full
- STR R0, R4, #0 ;no overflow, store value in the stack
- ADD R4, R4, #-1 ;move top of the stack
- ST R4, STACK_TOP ;store top of stack pointer
- BRnzp DONE_PUSH ;
- OVERFLOW
- ADD R5, R5, #1 ;
- DONE_PUSH
- LD R3, PUSH_SaveR3 ;
- LD R4, PUSH_SaveR4 ;
- RET
- PUSH_SaveR3 .BLKW #1 ;
- PUSH_SaveR4 .BLKW #1 ;
- ;OUT: R0, OUT R5 (0-success, 1-fail/underflow)
- ;R3 STACK_START R4 STACK_TOP
- ;
- POP
- ST R3, POP_SaveR3 ;save R3
- ST R4, POP_SaveR4 ;save R3
- AND R5, R5, #0 ;clear R5
- LD R3, STACK_START ;
- LD R4, STACK_TOP ;
- NOT R3, R3 ;
- ADD R3, R3, #1 ;
- ADD R3, R3, R4 ;
- BRz UNDERFLOW ;
- ADD R4, R4, #1 ;
- LDR R0, R4, #0 ;
- ST R4, STACK_TOP ;
- BRnzp DONE_POP ;
- UNDERFLOW
- ADD R5, R5, #1 ;
- DONE_POP
- LD R3, POP_SaveR3 ;
- LD R4, POP_SaveR4 ;
- RET
- POP_SaveR3 .BLKW #1 ;
- POP_SaveR4 .BLKW #1 ;
- STACK_END .FILL x3FF0 ;
- STACK_START .FILL x4000 ;
- STACK_TOP .FILL x4000 ;
- .END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement