-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmath.asm
86 lines (82 loc) · 1.85 KB
/
math.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
; define label "math" to prevent multiple include of this module
math
.macro multiply (num1, num2)
; multiply two 8-bit numbers
; input: two numbers num1, num2
; output: result in registers x,y (LOW, HIGH)
; example: "multiple 200,3" produces X=$58, Y=$02 (result $0258=600)
lda #0
ldx #0
ldy #0
sta mul1 + 1
lda #:num1
sta mul1 ; store 1st multiplier
lda #:num2
sta mul2 ; store 2nd multiplier
start
lda mul2
beq end; ; 2nd number is exhausted
lsr mul2
bcc multtwo ; only multiply if significant bit is 0
; bit from last binary base == 1, add result to sum
txa
clc
adc mul1
tax
tya
adc mul1 + 1
tay
multtwo
; multiply 1st number by 2
asl mul1
rol mul1 + 1
bcc start ; always jump
mul1 .ds 2
mul2 .ds 1
end
.endm
decDigitCnt = 5
.macro hexToDec (decimal)
; convert word to decimal digits
; input: word in registers x, y (LOW, HIGH)
; output: five decimal digits in indirect location "decimal" (from highest to lowest)
stx wordnum
sty wordnum + 1
ldy #0 ; decimal base index, used for subtraction
nextDigit
ldx #0 ; decimal digit
isBaseGreater
lda wordnum + 1
cmp decbases + 1, y
bcc nextbase ; base is higher, go to lower base
bne subtractBase
lda wordnum
cmp decbases, y
bcc nextBase
subtractBase
lda wordnum
sec
sbc decbases, y
sta wordnum
lda wordnum + 1
sbc decbases + 1, y
sta wordnum + 1
inx
bpl isBaseGreater; always jump
nextBase
tya
lsr
tay
txa
sta (:decimal), y ; save decimal digit
iny
cpy #decDigitCnt ; are we done with all digits?
beq end
tya
asl
tay
bpl nextDigit; always jump and process next digit
decbases .byte <10000, >10000, <1000, >1000, <100, >100, <10, >10, <1, >1
wordnum .ds 2
end
.endm