include "macros.def" start _clc _cls _ldx stale _stc _ldx ctmp _stc _ldx output0 _stc _ldx icount _adc _ldx count _stc _ldx inumb _adc _ldx numb _stc bcdloop _clc _ldx stale ; Clear accumulator _stc _ldx numb _adc ; Load number _ldx numb _adc ; Shift by one bit _ldx numb _stc _ldx ctmp ; Keep carry here _adc ; Add carry to accumulator _ldx ctmp _stc _clc _ldx output0 ; Shift output left by one _adc _ldx output0 _adc _ldx ctmp ; Add in carry _adc _ldx output0 _stc _ldx ctmp ; Clear carry flag _stc _clc _ldx count ; Count down _adc _ldx neg1 _adc _clc _fnz _ldx count _stc _jmp dshift ; Jump to dshift on zero, else skip _cls _clc _ldx output0 _adc _ldx mask0 _and _ldx nfive0 ; Subtract 5 _adc _ldx maskhi ; Check for highest bit (negative) _and _fnz _ldx stale ; Clear acc for following operations _stc _jmp j0 ; Jump if negative _cls _jmp j1 j0 _clc _ldx output0 ; Add 3 to lowest nibble if >=5h _adc _ldx three0 _adc _ldx output0 _stc _cls j1 _clc _ldx output0 _adc _ldx mask1 _and _ldx nfive1 _adc _ldx maskhi _and _fnz _ldx stale _stc _jmp j2 _cls _jmp j3 j2 _clc _ldx output0 _adc _ldx three1 _adc _ldx output0 _stc j3 _clc _ldx output0 _adc _ldx mask2 _and _ldx nfive2 _adc _ldx maskhi _and _fnz _ldx stale _stc _jmp j4 _cls _jmp j5 j4 _clc _ldx output0 _adc _ldx three2 _adc _ldx output0 _stc j5 _clc _ldx output0 _adc _ldx mask3 _and _ldx nfive3 _adc _ldx maskhi _and _fnz _ldx stale _stc _jmp j6 _cls _jmp j7 j6 _clc _ldx output0 _adc _ldx three3 _adc _ldx output0 _stc j7 _cls ; Always jump _jmp bcdloop dshift _clc _ldx output0 _adc _hlt inumb _initl 1234 neg1 _initl -1 icount _initl 16 nfive0 _initl 0xFFFB nfive1 _initl 0xFFB0 nfive2 _initl 0xFB00 nfive3 _initl 0xB000 mask0 _initl 0x000F mask1 _initl 0x00F0 mask2 _initl 0x0F00 mask3 _initl 0xF000 three0 _initl 0x0003 three1 _initl 0x0030 three2 _initl 0x0300 three3 _initl 0x3000 maskhi _initl 0x8000 nl _initl 15 org 0x2000 numb _ram ctmp _ram output0 _ram count _ram stale _ram