Skip to content

Double Dabble Forever?

March 26, 2015

A friend and I keep poking at the double dabble algorithm for converting from binary to ascii numbers. His latest effort is very nice and well documented. It needs a little work to fit with the compiler but i’m anxious to see how it performs.
Explanation of the code

TITL	"1802 Cross-Assembler Double-Dabble Source"
EJCT	60

CPU	1802

NUMBER		EQU	0

;
; Register Definitions:
;
R0		EQU	0
R1		EQU	1
R2		EQU	2
R3		EQU	3
R4		EQU	4
R5		EQU	5
R6		EQU	6
R7		EQU	7
R8		EQU	8
R9		EQU	9
R10		EQU	10
R11		EQU	11
R12		EQU	12
R13		EQU	13
R14		EQU	14
R15		EQU	15

; **********************************
; R6  - BINARY BIT COUNT
; R8 & R9 - BINARY NUMBER
; R12 - INDEX REGISTER
; R13 - LOW BYTE STORES LOCAL LOOP COUNTER
; R13 - HIGH BYTE SAVES CURRENT BCD DIGIT
; R14 - LOW BYTE STORES ACTIVE BCD DIGIT COUNT
; R14 - HIGH BYTE SAVES CARRY VALUE (DF)
; **********************************

BGNDIG:         BYTE    0,0,0,0,0,0,0,0,0
ENDDIG:         BYTE    1

CONVRT:         BYTE    $00,$CE,$ED,$BB         ; DECIMAL 847,291

START		ORG	100H
; -----------------
; *** SET UP INDEX REGISTER LOAD
LDI     HIGH(ENDDIG)
PHI     R12
LDI     LOW(ENDDIG)
PLO     R5
; -----------------
; *** LOAD BINARY NUMBER TO CONVERT INTO R8 AND R9
LDI     HIGH(CONVRT)
PHI     R13
LDI     LOW(CONVRT)
PLO     R13
LDA     R13
PHI     R8
LDA     R13
PLO     R8
LDA     R13
PHI     R9
LDA     R13
PLO     R9
; -----------------
; *** SET UPPER BYTE OF R6 TO 1
LDI     1
PHI     R6              ;STORE CONSTANT 1 INTO HIGH BYTE OF R6
PLO     R14             ;ACTIVE BCD DIGIT COUNT -- STARTS AT 1
; -----------------
; *** SET BINARY BIT COUNT IN LOW BYTE OF R6
LDI     32
PLO     R6
; -----------------
; SHIFT OUT HIGH ORDER ZERO BITS OF BINARY NUMBER
SHFOUT:         GLO     R9
SHLC
PLO     R9
;           -----------------
GHI     R9
SHLC
PHI     R9
;           -----------------
GLO     R8
SHLC
PLO     R8
;           -----------------
GHI     R8
SHLC
PHI     R8
;           -----------------
DEC     R6              ;DECREMENT BINARY BIT COUNT
BNF     SHFOUT
;
;
;
; -----------------
; *** MAIN DOUBLE DABBLE LOOP
NLOOP:
; -----------------
; SHIFT OUT HIGH-ORDER BIT OF BINARY NUMBER
; -----------------
GLO     R9
SHLC
PLO     R9
;           -----------------
GHI     R9
SHLC
PHI     R9
;           -----------------
GLO     R8
SHLC
PLO     R8
;           -----------------
GHI     R8
SHLC
PHI     R8
; -----------------
; SET INDEX REGISTER (LOW BYTE ONLY)
GLO     R5              ;GET INDEX LOW BYTE
PLO     R12             ;STORE IN INDEX REG.
; -----------------
; CALCULATE LOOP COUNT
GLO     R14             ;GET ACTIVE BCD DIGIT COUNT
PLO     R13             ;STORE LOCAL LOOP COUNT
UNILP:
; -----------------
; *** SAVE DF (CARRY) IN UPPER BYTE OF R14
SHLC                    ;GET CARRY TO LOW-ORDER BIT OF D
PHI     R14             ;SAVE D IN UPPER BYTE OF R14
; ----------------
; *** TEST ONE BYTE FOR PRE-CORRECTION
LDX                     ;LOAD D FROM INDEX ADDRESS
PHI     R13             ;SAVE BCD DIGIT IN UPPER BYTE OF R13
SMI     5               ;IS D > 4
BNF     NOFIX           ;SKIP CORRECTION IF D < 0
; *** PRE-CORRECT BCD DIGIT
ADI     128             ;ADD 3 CORRECTION + 5 RESTORE + 120
PHI     R13             ;SAVE BCD DIGIT IN UPPER BYTE OF R13
NOFIX:
; -----------------
; SHIFT ONE BCD DIGIT
; -----------------
GHI     R14             ;GET CARRY BACK TO D
SHR                     ;PUT CARRY VALUE INTO DF
GHI     R13             ;GET WORKING BCD DIGIT
SHLC                    ;ROTATE D THROUGH CARRY
STXD                    ;STORE D AND DECREMENT INDEX REGISTER
; -----------------
; *** UNILP COUNTER OVERHEAD
DEC     R13             ;DECREMENT CORRECT-SIFT LOOP COUNTER
GLO     R13             ;D = LOCAL LOOP COUNT
BNZ     UNILP           ;LOOP UNTIL DONE
; -----------------
; *** IF DF = 1, CREATE NEW BCD DIGIT = 1 -- INCREMENT ACTIVE BCD DIGIT COUNT
BNF     SKPDIG          ;IF CARRY IS ZERO, NO NEW BCD DIGIT
INC     R14             ;INCREMENT ACTIVE BCD DIGIT COUNT
GHI     R6              ;GET A BYTE WITH VALUE OF 1
STXD                    ;STORE D IN NEW BCD DIGIT POSITION
; -----------------
; *** MAIN LOOP COUNTER OVERHEAD
SKPDIG:         DEC     R6              ;DECREMENT MASTER LOOP COUNTER
GLO     R6              ;D = BINARY BIT COUNT
BNZ     NLOOP           ;LOOP UNTIL DONE
IDL
END
Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: