commit cd930e915c61f55708e01c2838257002a2bc7c38 Author: Michaelangel007 Date: Sun Jun 11 07:18:38 2017 -0700 Initial version diff --git a/README.md b/README.md new file mode 100644 index 0000000..701890d --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# Print Fixed Point #.16 Fraction + +# Assembly (Source) + +See file [print_fract.s](print_fract.s) + +# Binary (Executable) + +``` + 0800:a2 b5 a0 e9 20 15 08 a2 00 bd 70 08 20 ed fd e8 + 0810:e0 05 d0 f5 60 a9 00 8d 6a 08 8e 6b 08 8c 6c 08 + 0820:a2 00 20 60 08 20 54 08 20 60 08 20 60 08 20 44 + 0830:08 ad 6a 08 09 b0 9d 70 08 a9 00 8d 6a 08 e8 e0 + 0840:05 d0 df 60 18 a0 02 b9 6a 08 79 6d 08 99 6a 08 + 0850:88 10 f4 60 a0 02 b9 6a 08 99 6d 08 88 10 f7 60 + 0860:0e 6c 08 2e 6b 08 2e 6a 08 60 00 00 00 00 00 00 + 0870:00 00 00 00 00 +``` diff --git a/print_fract.s b/print_fract.s new file mode 100644 index 0000000..ba2799b --- /dev/null +++ b/print_fract.s @@ -0,0 +1,146 @@ + ORG $800 + +; ==================== Demo ==================== + +Demo + ;0.7106 * 65536 = $B5E9 + ; A*B5E9 + ; ======= + ; '.' + ; 7.1B1A + ; 1.0F04 + ; 0.9628 + ; 5.5DD90 + ; 8.A7A0 + ;0.71058_________^ + + LDX #$B5 + LDY #$E9 + JSR PrintFract + + LDX #0 +PrintDigit + LDA D0,X + JSR $FDED + INX + CPX #5 + BNE PrintDigit + RTS + +; ==================== Conversion ==================== + +; -------------------- +; Convert 16-bit fractional value to Decimal +; IN +; X = high 16-bit val +; Y = low 16-bit val +; OUT +; [D0..D4] Decimal Fraction +; -------------------- +PrintFract + STX A2 + STY A3 + + JSR ZeroInt + TAX + + ; Psuedo Code + ; int8_t a[3] + ; int8_t t[3] + ; char d[5] + ; + ; for( iDigit = 0; iDigit < 5; iDigit++ ) + ; a *= 2 + ; t = a + ; a *= 4 + ; a += t + ; d[i] = a[0] | '0' + ; a[0] = 0 +NextDigit + JSR Mul2 ; *2 + JSR CopyT2 ; + JSR Mul2 ; *4 + JSR Mul2 ; *8 + JSR AddT2 ; x*10 = x*8 + x*2 + + LDA A1 ; Convert binary num to ASCII + ORA #$30+$80 + STA D0,X + + JSR ZeroInt + + INX + CPX #5 + BNE NextDigit + RTS + + +; ==================== Utility ==================== + +; -------------------- +; Add Arrays +; A += T +; -------------------- +AddT2 + CLC + LDY #2 +_AddT2 + LDA A2-1,Y + ADC T2-1,Y + STA A2-1,Y + DEY + BPL _AddT2 + RTS + +; -------------------- +; Copy Array +; T = A +; -------------------- +CopyT2 + LDY #2 +_CopyT2 + LDA A2-1,Y + STA T2-1,Y + DEY + BPL _CopyT2 + RTS + + +; -------------------- +; 24-bit SHL +; A *= 2 +; -------------------- +Mul2 + ASL A3 + ROL A2 + ROl A1 + RTS + +; -------------------- +; Clear Integer in 8.16 format +; A[0] = 0 +; -------------------- +ZeroInt + LDA #0 + STA A1 + RTS + +; ==================== Data ==================== + +; 24-bit Number +A1 DB 0 +A2 DB 0 +A3 DB 0 + +; Temp +T1 DB 0 ; Always zero +T2 DB 0 +T3 DB 0 + +; String Output in ASCII +D0 DB 0 +D1 DB 0 +D2 DB 0 +D3 DB 0 +D4 DB 0 +