SX Microcontroller Radix Math Method

Binary to ASCII, 16 bit to 5 digits (1 at a time) with one temp register

;by James Newton

;Listing from SX Key IDE
;76 instructions and about 25x5x4=500 worst case instructions executed. About (11*4+4)*5+4=100 best case.
                                 watch   ptr,8,udec              ;set watch for regis
                                 watch   Hi,8,uhex               ;set watch for regis
                                 watch   Lo,8,uhex               ;set watch for regis
                                 watch   Lo,16,udec              ;set watch for regis
                                 watch   Hic,8,uhex              ;set watch for regis
                                 watch   Loc,8,uhex              ;set watch for regis
                                 watch   Loc,16,udec             ;set watch for regis
                                 watch   Digit,8,udec            ;set watch for regis
                                 watch   d1,8,udec               ;set watch for regis
                                 watch   d2,8,udec               ;set watch for regis
                                 watch   d3,8,udec               ;set watch for regis
                                 watch   d4,8,udec               ;set watch for regis
                                 watch   d5,8,udec               ;set watch for regis

                                 device  PINS28,PAGES4,BANKS8,TURBO, stackx, optionx
 Addr Opcode    ;source
 7FF- A13                        reset   start
008-	org	8
		;temp register to point to table
 008-            ptr     ds 1
		;input value
 009-            Lo      ds 1
 00A-            Hi      ds 1
		;output value
 00B-            Digit   ds 1

		;variables used for testing
 00C-            Loc     ds 1
 00D-            Hic     ds 1
 00E-            d1      ds 1
 00F-            d2      ds 1
 010-            d3      ds 1
 011-            d4      ds 1
 012-            d5      ds 1

		;This code just exists to test the routine at entry
 013-            start
 013- 06E                clr d1
 014- 06F                clr d2
 015- 070                clr d3
 016- 071                clr d4
 017- 072                clr d5
 018- 06C                clr Loc
 019- 06D                clr Hic
 01A- 068                clr ptr
 01B- 06B                clr digit

 01C-            mainloop
 01C- C0E 024            mov fsr,#d1
 01E- 20D 02A            mov Hi, Hic
 020- 20C 029            mov Lo,Loc
 022- 967                call entry
 023- C12 024            mov fsr,#d5
 025-            :inc10
 025- 2A0                inc ind
 026- C0A 080            cjb ind, #10, :next
 028- 703 A30
 02A- 060                clr ind
 02B- 0E4                dec fsr
 02C- CF2 1C4            cja fsr, #d1-1, :inc10
 02E- 603 A25
 030-            :next
 030- 2AC                inc Loc
 031- 643                snz
 032- 2AD                inc Hic
 033- A1C                jmp mainloop

		;This is the subroutine called to get rid of a result digit
		;For testing, it just compairs the value to the decimal count
		;pointed to by fsr
 034-            SendOutByte
 034- 200 08B            cje Digit, ind, :exit
 036- 643 A3A
 038-            :error
 038-                    break
 038- 010                page :error
 039- A38                jmp :error
 03A-            :exit
 03A- 2A4                inc fsr
 03B- 00C                ret

		;this is the beginning of the routines code.
 03C-            nextHi
 03C- 208                mov w, ptr
 03D- 1E2                jmp pc+w
 03E- 8C3                retw $C3 ; 0 50,000
 03F- 875                retw $75 ; 1 30,000
 040- 827                retw $27 ; 2 10,000
 041- 827                retw $27 ; 3 10,000
 042- 813                retw $13 ; 4  5,000
 043- 80B                retw $0B ; 5  3,000
 044- 803                retw $03 ; 6  1,000
 045- 803                retw $03 ; 7  1,000
 046- 801                retw $01 ; 8    500
 047- 801                retw $01 ; 9    300
 048- 800                retw $00 ;10    100
 049- 800                retw $00 ;11    100
 04A- 800                retw $00 ;12     50
 04B- 800                retw $00 ;13     30
 04C- 800                retw $00 ;14     10
 04D- 800                retw $00 ;15     10

 04E-            nextLo
 04E- 208                mov w, ptr
 04F- 1E2                jmp pc+w
 050- 850                retw $50 ; 0 50,000
 051- 830                retw $30 ; 1 30,000
 052- 810                retw $10 ; 2 10,000
 053- 810                retw $10 ; 3 10,000
 054- 888                retw $88 ; 4  5,000
 055- 8B8                retw $B8 ; 5  3,000
 056- 8E8                retw $E8 ; 6  1,000
 057- 8E8                retw $E8 ; 7  1,000
 058- 8F4                retw $F4 ; 8    500
 059- 82C                retw $2C ; 9    300
 05A- 864                retw $64 ;10    100
 05B- 864                retw $64 ;11    100
 05C- 832                retw $32 ;12     50
 05D- 81E                retw $1E ;13     30
 05E- 80A                retw $0A ;14     10
 05F- 80A                retw $0A ;15     10

 060-                    ror
 060-            nextDg
 060- 208                mov w, ptr
 061- E03                and w, #%00000011
 062- 1E2                jmp pc+w
 063- 805                retw $5
 064- 803                retw $3
 065- 801                retw $1
 066- 801                retw $1


		;this is the entry point for the conversion routine
 067-            entry
 067- 068                clr ptr
 068- 06B                clr digit

 069-            Loop
 069- 93C                call nextHi     ;get the next value
 06A- 08A                mov w, Hi-w
 06B- 703                sc
 06C- A7D                jmp smaller
                 ;if the high byte is less than our value, the whole thing must be sm
                 ;Carry not set so parent will skip nextLo and continue with nextHi
 06D- 743                sz
 06E- A76                jmp largerHi
                 ;if the high byte was exactly equal to our value, then is the low by
 06F- 94E                call nextLo     ;get the next value
 070- 089                mov w, Lo-w
 071- 703                sc
                 ;the low byte was smaller so the whole thing was smaller after all.
 072- A7D                jmp smaller
 073- 06A                clr Hi
                 ;the low byte was larger so we are subtracting and the high byte was
 074- 029                mov Lo, w       ;and save the subtraction from Lo
 075- A7B                jmp larger
                 ;The low byte was larger but it's now smaller and since carry is set
                 ;this digit value to output and continue with nextHi
 076-            largerHi
 076- 02A                mov Hi, w       ;if its not zero and its not smaller, save t
 077- 94E                call nextLo     ;in either case, get the next value
 078- 0A9                sub Lo, w
 079- 703                sc
 07A- 0EA                dec Hi
 07B-            larger
                 ;The value was larger but is now smaller
                 ;add this digit value to output and continue
 07B- 960                call nextDg
                 ;       break
 07C- 1EB                add Digit, w
 07D-            smaller
 07D- 2A8                inc ptr
 07E- 708                sb ptr.0
 07F- 628                snb ptr.1
 080- A69                jmp Loop
 081- 934                call SendOutByte
 082- 06B                clr Digit
 083- 788                sb ptr.4
 084- A69                jmp Loop
 085- 209 02B            mov Digit, Lo
 087- 934                call SendOutByte
 088- 00C                ret

                         END

Decimal Hex Binary
50,000 C350 1100 0011 0101 0000
30,000 7530 0111 0101 0011 0000
20,000 4E20 0100 1110 0010 0000
10,000 2710 0010 0111 0001 0000
5000 1388 0001 0011 1000 1000
3000 0BB8 0000 1011 1011 1000
2000 07D0 0000 0111 1101 0000
1000 03E8 0000 0011 1110 1000
500 01F4 0000 0001 1111 0100
300 012C 0000 0001 0010 1100
200 C8 1100 1000
100 64 0110 0100
50 32 0011 0010
30 1E 0001 1110
20 14 0001 0100
10 A 0000 1010

Old