-
Notifications
You must be signed in to change notification settings - Fork 8
/
6809_BN2BCD.s
78 lines (76 loc) · 1.53 KB
/
6809_BN2BCD.s
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
;
; Title: Binary to BCD Conversion
;
; Name: BN2BCD
;
; Purpose: Converts one byte of binary data to two
; bytes of BCD data
; Entry: Register A = Binary data
; Exit: Register D = BCD data
;
; Registers Used: A,B,CC
; Time: 140 cycles maximum
;
; Size: Program 30 bytes
; Data 2 bytes on stack
;
;
BN2BCD:
;
; CALCULATE 100'S DIGIT
; DIVIDE DATA BY 100 USING SUBTRACTIONS
; B = QUOTIENT
; A = REMAINDER
;
LDB #$FF ; START QUOTIENT AT -1
D100LP: INCB ; ADD 1 TO QUOTIENT
SUBA #100 ; SUBTRACT 100 FROM DIVIDEND
BCC D100LP ; JUMP IF DIFFERENCE STILL POSITIVE
ADDA #100 ; IF NOT, ADD THE LAST 100 BACK
STB ,S ; SAVE 100'S DIGIT ON STACK
;
; CALCULATE 10'S AND 1'S DIGITS
; DIVIDE THE REMAINDER FROM CALCULATING THE 100'S DIGIT BY 10
; B =10'S DIGIT
; A = 1'S DIGIT
;
LDB #$FF ; START QUOTIENT AT -1
D10LP: INCB ; ADD 1 TO QUOTIENT
SUBA #10 ; SUBTRACT 10 FROM DIVIDEND
BCC D1OLP ; JUMP IF DIFFERENCE STILL POSITIVE
ADDA #10 ; IF NOT, ADD THE LAST 10 BACK
;
; COMBINE 1'S AND 10'S DIGITS
;
LSLB ; MOVE 10'S DIGIT TO HIGH NIBBLE LSLB
LSLB
LSLB
LSLB
STA ,-S ; SAVE 1'S DIGIT ON STACK
ADDB ,S+ ; COMBINE 1'S AND 10'S DIGITS IN B
;
; RETURN WITH D = BCD DATA
;
LDA ,S+ ; RETURN 100'S DIGIT IN A
RTS ;
;
;
; SAMPLE EXECUTION
;
;
SC1A:
; CONVERT 0A HEXADECIMAL TO 10 BCD
;
LDA #$0A
JSR BN2BCD ; D = 0010H (A = 00, B = 10H)
;
; CONVERT FF HEXADECIMAL T0 255 BCD
;
LDA #$FF
JSR BN2BCD ; D = 0255H (A = 02, B = 55H)
;
; CONVERT 0 HEXADECIMAL TO 0 BCD
;
LDA #0
JSR BN2BCD ; D = 0000 (A = 00, B = 00)
END