implement general divition
This commit is contained in:
parent
3283f9a68a
commit
89dcec60ff
4 changed files with 59 additions and 3 deletions
|
@ -1,4 +1,5 @@
|
|||
;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*-
|
||||
;; public args
|
||||
VAL_A = ARGVEC + 0
|
||||
VAL_B = ARGVEC + 1
|
||||
VAL_B = ARGVEC + 0
|
||||
VAL_A = ARGVEC + 1
|
||||
;; Note that we use VAL_A +1 as a temp variable, this is a hack to make it faster.
|
||||
|
|
43
wip-hugo/routines/arithmatic/div.s
Normal file
43
wip-hugo/routines/arithmatic/div.s
Normal file
|
@ -0,0 +1,43 @@
|
|||
;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*-
|
||||
.proc div; user-procedure :clobbers (A,X) :clobbers-arguments 3
|
||||
;; https://en.wikipedia.org/wiki/Division_algorithm#Restoring_division
|
||||
;;VAL_A / VAL_B = X
|
||||
;; Remainder (after) = VAL_A
|
||||
;; only works if VAL_A > VAL_B
|
||||
|
||||
;; Because the algorithm R needs to be signed 16-bit
|
||||
;; We make it unsigned by storing the sign-bit in the branching.
|
||||
.include "arithmatic.inc"
|
||||
R = VAL_A
|
||||
LDA #$00
|
||||
STA VAL_A + 1
|
||||
D = VAL_B
|
||||
count =$E0
|
||||
|
||||
LDA #%10000000
|
||||
STA count ;; this is 2^i
|
||||
LDX #$00
|
||||
for_i:; for i in 7-0 do
|
||||
;; R = R*2 -D*2^8
|
||||
;; R*2
|
||||
Mult_16 R, R + 1
|
||||
|
||||
;; -D* 2^8
|
||||
SEC
|
||||
LDA R + 1
|
||||
SBC D
|
||||
BMI R_neg
|
||||
STA R + 1
|
||||
R_pos: ; R >= 0
|
||||
;; X(i) = 1
|
||||
TXA
|
||||
EOR count
|
||||
TAX
|
||||
R_neg: ; R < 0
|
||||
;; We need to take back R
|
||||
;; We did that by not saving the arithmatic :)
|
||||
LSR count
|
||||
BNE for_i
|
||||
for_i_end:
|
||||
RTS
|
||||
.endproc
|
10
wip-hugo/routines/arithmatic/div_test.s
Normal file
10
wip-hugo/routines/arithmatic/div_test.s
Normal file
|
@ -0,0 +1,10 @@
|
|||
;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*-
|
||||
;;Calculates $EE /$05 = $33
|
||||
.scope div_test
|
||||
.include "arithmatic.inc"
|
||||
LDA #$05
|
||||
STA VAL_A
|
||||
LDA #$02
|
||||
STA VAL_B
|
||||
JSR div; X = $02 if it works as it should
|
||||
.endscope
|
|
@ -8,7 +8,8 @@
|
|||
.include "STARTUP.s"
|
||||
;.include "dubbel_buffer/raster_irqs.s"
|
||||
|
||||
.include "routines/arithmatic/mult_test.s"
|
||||
;.include "routines/arithmatic/mult_test.s"
|
||||
.include "routines/arithmatic/div_test.s"
|
||||
;.include "routines/line/line_test.s"
|
||||
;.include "routines/text/char_draw_test.s"
|
||||
;.include "routines/pixel/pixel_test.s"
|
||||
|
@ -25,4 +26,5 @@ JMP exit
|
|||
.include "routines/memory/memset.s"
|
||||
.include "routines/memory/memcpy.s"
|
||||
.include "routines/arithmatic/mult.s"
|
||||
.include "routines/arithmatic/div.s"
|
||||
.include "END.s"
|
||||
|
|
Loading…
Add table
Reference in a new issue