-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbcd.asm
More file actions
89 lines (73 loc) · 1.2 KB
/
bcd.asm
File metadata and controls
89 lines (73 loc) · 1.2 KB
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
87
88
89
; BCD addition of 8-word numbers
start:
; get the two digits, add them, then add the carry
PUSH x
CALL load_digit
PUSH y
CALL load_digit
ADD
LOAD carry
ADD
PUSH #0
STOR carry
; check if we need to carry out
DUP
PUSH #0A
CGE
JT carry_one
; when we're here, carry has been handled
; write the digit then loop back
write:
PUSH z
CALL store_digit
J increment
carry_one: ; record the carry one, then subtract 10
PUSH #1
STOR carry
PUSH #0A
SUB
J write
; increment pos; if it's 8 we're done
increment:
LOAD pos
PUSH #1
ADD
DUP
STOR pos
PUSH #8
CGE
JT end
; INT
J start
end:
HALT ; end of main
; load_digit: loads mem[s + len - 1 - pos] onto the stack, where
; s is the value on the stack top
load_digit:
LOAD len
ADD
PUSH #1
SUB
LOAD pos
SUB
LODS
RET
; stack: top = address of destination, below = digit
store_digit:
LOAD len
ADD
PUSH #1
SUB
LOAD pos
SUB
STRS
RET
.DATA
; the numbers to add, we want z = x + y
x: 8 = 0 0 0 0 9 9 0 1
y: 8 = 0 0 0 0 0 1 0 5
z: 8
; helpers
pos: 1 = 0
carry: 1 = 0
len: 1 = 8