43 lines
1 KiB
ArmAsm
43 lines
1 KiB
ArmAsm
;;; -*- 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
|