SX Microcontroller Radix Math Method

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

by Nikolai Golovchenko

;Thanks Scott, your code is very instructive.
;Total execution time:
;worst case: 59 + 80 + 66 + 46 = 251 cycles

;Code size: 73

        clr     temp
sub30k
        mov     W, #3
        add     temp, W
        mov     W, #30000 & $FF
        sub     Lo, W

        mov     W, #30000 >> 8
        sb      C
        mov     W, #(30000 >> 8) + 1
        sub     Hi, W
        snb     C
        jmp     sub30k

add10k
        dec     temp
        mov     W, #10000 & $FF
        add     Lo, W

        mov     W, #(10000 >> 8)
        snb     C
        mov     W, #(10000 >> 8) + 1
        add     Hi, W
        sb      C
        jmp     add10k
;       Output(temp)         ;output temp = TenK
;worst case: 12 * 3 + 11 * 3 - 1 = 59

        clr     temp
sub3k
        mov     W, #3
        add     temp, W
        mov     W, #3000 & $FF
        sub     Lo, W

        mov     W, #(3000 >> 8)
        sb      C
        mov     W, #(3000 >> 8) + 1
        sub     Hi, W
        snb     C
        jmp     sub3k
add1k
        dec     temp
        mov     W, #1000 & $FF
        add     Lo, W

        mov     W, #(1000 >> 8)
        snb     C
        mov     W, #(1000 >> 8) + 1
        add     Hi, W
        sb      C
        jmp     add1k
;       Output(temp)         ;output temp = Thou
;worst case: 12 * 4 + 11 * 3 - 1 = 80

        clr     temp
sub300
        mov     W, #3
        add     temp, W
        mov     W, #300 & $FF
        sub     Lo, W

        mov     W, #(300 >> 8)
        sb      C
        mov     W, #(300 >> 8) + 1
        sub     Hi, W
        snb     C
        jmp     sub300

        mov     W, #100
add100
        dec     temp
        add     Lo, W
        sb      C
        jmp     add100
        inc     Hi
        snb     Hi.7
        jmp     add100
;       Output(temp)         ;output temp = Hund
;worst case: 11 * 4 + 6 * 3 + 3 = 66

        clr     temp
        mov     W, #30
sub30
        inc     temp
        sub     Lo, W
        snb     C
        jmp     sub30
        mov     W, temp
        rl      temp
        add     temp, W
        mov     W, #10
add10
        dec     temp
        add     Lo, W
        sb      C
        jmp     add10
;       Output(temp)         ;output temp = Tens
;worst case: 6 * 4 + 6 * 3 + 4 = 46

;       Output(Lo)         ;output temp = Ones