forked from ept221/pet-on-a-chip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switched from subtract with borrow to subtract with carry. fixed bug …
…in control for pus and pos.
- Loading branch information
Showing
3 changed files
with
68 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,63 @@ | ||
;****************************************************************************** | ||
.define uart_baud, 0x0A | ||
.define uart_ctrl, 0x0B | ||
.define uart_buffer, 0x0C | ||
;****************************************************************************** | ||
|
||
.code | ||
|
||
ldi r14, 0xff ; setup the stack pointer | ||
ldi r15, 0x00 | ||
|
||
ldi r0, 103 ; set the baud-rate to 9600 | ||
out r0, uart_baud | ||
|
||
ldi r0, 253 ; dividend | ||
ldi r1, 7 ; divisor | ||
call divmod | ||
|
||
loop: in r5, uart_ctrl | ||
ani r5, 2 | ||
bz loop ; poll for empty buffer | ||
|
||
out r2, uart_buffer ; print the quotient | ||
|
||
hlt | ||
;****************************************************************************** | ||
; r0 holds the dividend | ||
; r1 holds the divisor | ||
; r2 holds the quotient | ||
; r3 holds the remainder | ||
|
||
divide: push r0 | ||
divmod: push r0 | ||
push r4 | ||
|
||
csr 0b1110 ; clear the carry | ||
|
||
ldi r3, 0 ; initilize the remander to zero | ||
ldi r4, 8 ; initilize the counter | ||
csr 0 | ||
|
||
divmod_loop: pus | ||
cpi r4, 0 ; check the counter | ||
bz divide_end ; branch if we've shifted all 8 times | ||
adi r4, -1 | ||
pos | ||
rlc r0 ; shift the dividend left and bring in the next bit of the quotient | ||
rlc r3 ; shift the remainder left and bring in the next bit of the remainder | ||
|
||
sub r3, r1 ; subtract the divisor from the remainder | ||
bc divide_loop ; don't restore if the result was positive | ||
add r3, r1 ; else restore | ||
csr 0 | ||
br divide_loop | ||
|
||
cpi r4, 0 | ||
bz divide_end ; finish if we've shifted all 8 times | ||
divmod_end: pos | ||
|
||
rlc r0 ; shift the dividend left and bring in the msb of the remainder | ||
rlc r3 ; shift the remainder left and bring in the msb of the dividend | ||
rlc r0 ; get the last bit of the quotient | ||
mov r2, r0 ; copy the quotient into r2 | ||
|
||
divide_end: pop r4 | ||
pop r0 | ||
pop r0 ; restore the counter reg | ||
pop r4 ; restore the dividend | ||
ret | ||
;****************************************************************************** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters