Binary-to-Decimal Conversion--a reference for all

Go To Last Post
52 posts / 0 new

Pages

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Some may find these changes useful...

 

Eliminated the need for output registers by making it call a procedure to display/store/whatever for each digit.

With a little extra code, this output procedure becomes a helper routine to control the output width, and provide

leading zero suppression.

 

;*******************************************************************************
;    Convert unsigned 32bit to ASCII
;    Author: Peter Dannegger danni@specs.de
;    Viciously hacked about by Rob Storey
;-------------------------------------------------------------------------------
;    Input: 32 bit value 0 ... 4294967295
;       Passed in 4 symbolic registers B4:B3:B2:B1 (from the high register set)
;    Output: 10 ASCII digits passed in TL via successive calls (MSD to LSD)
;       to procedure "PutDecChar", which can do as it pleases with them...
;       EG: Write to output, Store in memory, Zero suppress before sending, etc.
;*******************************************************************************
M1P0        EQU    -1+'0'
TNP0        EQU    10+'0'
MTP0        EQU    -10-'0'

Bin32ToASC  PUSHM    TL,TH,B1,B2,B3,B4     ;Or whatever....
            SUBI    TH,10                  ;Convert width in TH to output char counter
            NEG    TH
;
            LDI    TL,M1P0
Bin2Asc1:   INC   TL
            SUBI     B2,Byte2(1000000000)  ;-1000,000,000 until overflow
            SBCI  B3,Byte3(1000000000)
            SBCI  B4,Byte4(1000000000)
            BRCC  Bin2Asc1
            RCALL    PutDecChar
;
            LDI    TL,TNP0
Bin2Asc2:   DEC   TL
            SUBI  B2,Byte2(-100000000)  ;+100,000,000 until no overflow
            SBCI  B3,Byte3(-100000000)
            SBCI  B4,Byte4(-100000000)
            BRCS  Bin2Asc2
            RCALL    PutDecChar
;
            LDI    TL,M1P0
Bin2Asc3:   INC   TL
            SUBI  B1,Byte1(10000000)    ;-10,000,000
            SBCI  B2,Byte2(10000000)
            SBCI  B3,Byte3(10000000)
            SBCI  B4,0
            BRCC  Bin2Asc3
            RCALL    PutDecChar
;
            LDI    TL,TNP0
Bin2Asc4:   DEC   TL
            SUBI  B1,Byte1(-1000000)    ;+1,000,000
            SBCI  B2,Byte2(-1000000)
            SBCI  B3,Byte3(-1000000)
            BRCS  Bin2Asc4
            RCALL    PutDecChar
;
            LDI    TL,M1P0
Bin2Asc5:   INC   TL
            SUBI  B1,Byte1(100000)      ;-100,000
            SBCI  B2,Byte2(100000)
            SBCI  B3,Byte3(100000)
            BRCC  Bin2Asc5
            RCALL    PutDecChar
;
            LDI    TL,TNP0
Bin2Asc6:   DEC   TL
            SUBI  B1,Byte1(-10000)        ;+10,000
            SBCI  B2,Byte2(-10000)
            SBCI  B3,Byte3(-10000)
            BRCS  Bin2Asc6
            RCALL    PutDecChar
;
            LDI    TL,M1P0
Bin2Asc7:   INC   TL
            SUBI  B1,Byte1(1000)          ;-1000
            SBCI  B2,Byte2(1000)
            BRCC  Bin2Asc7
            RCALL    PutDecChar
;
            LDI    TL,TNP0
Bin2Asc8:   DEC   TL
            SUBI  B1,Byte1(-100)          ;+100
            SBCI  B2,Byte2(-100)
            BRCS  Bin2Asc8
            RCALL    PutDecChar
;
            LDI    TL,M1P0
Bin2Asc9:   INC   TL
            SUBI  B1,10                 ;-10
            BRCC  Bin2Asc9
            RCALL    PutDecChar
;
            SUBI  B1,MTP0
            MOV   TL,B1
            RCALL    PutDecChar
;
            POPM    TL,TH,B1,B2,B3,B4    ;Or whatever....
            RET

 

;*******************************************************************************
;    Output Helper Routine for Bin32ToASC function, TL=Char TH=Width counter
;    Implements Output Width and Override, and Leading Zero Suppression
;    Caller of Bin32ToASC sets Width in TH, uses SET/CLT to turn LZS On/Off
;    If width is set too low, masked non-zero digits are shown anyway
;-------------------------------------------------------------------------------
PutDecChar  DEC    TH             ;Count Digits
            BRPL   PutDecCharC    ;Time to start showing them?
            BRTC   PutDecCharS    ;Yes, Is Zero Suppression Enabled?
; Two reasons for being here: Not showing due to:      Width  | LZS
PutDecCharC CPI    TL,'0'         ;Non-zero masked by width?  | Still in zeros?
            BREQ   PutDecCharX    ;Yes: show anyway           | No: start showing
            CLT                   ;Force suppression off      | End of LZS
PutDecCharS RCALL  PutByte        ;Show output char
PutDecCharX RET

 

A few more lines could make the characters suppressed due to width appear as spaces, thus giving right-justification

 

Pages