From f753eb9642d4c5926df0d4d31a179913295dfa6a Mon Sep 17 00:00:00 2001 From: "A.M. Rowsell" Date: Sat, 20 Dec 2025 15:45:51 -0500 Subject: [PATCH] dev: fixed assembly errors, added linker script and Makefile Additionally created a .gitignore to avoid committing object code and output formats --- .gitignore | 16 ++ Makefile | 21 ++ float.asm | 571 ++++++++++++++++++++++++++++---------------------- linker.cmd | 6 + printChar.asm | 4 + 5 files changed, 372 insertions(+), 246 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 linker.cmd create mode 100644 printChar.asm diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..abb5995 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# Created by https://www.toptal.com/developers/gitignore/api/assembler +# Edit at https://www.toptal.com/developers/gitignore?templates=assembler + +### Assembler ### +*.exe +*.o +*.obj +*.bc + +# Output +*.hex +*.srec +*.s19 +*.elf +# End of https://www.toptal.com/developers/gitignore/api/assembler + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..03e3b4d --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +ASM=vasm_z80_std +LINK=vlink + +ASMFLAGS=-Fvobj +LINKFLAGS=-b ihex -T linker.cmd + +all: float.hex + +float.o: float.asm + $(ASM) $(ASMFLAGS) -o $@ $< + +printChar.o: printChar.asm + $(ASM) $(ASMFLAGS) -o $@ $< + +float.hex: float.o printChar.o linker.cmd + $(LINK) $(LINKFLAGS) -o $@ float.o printChar.o + +clean: + rm -f float.o printChar.o float.hex + +.PHONY: all clean diff --git a/float.asm b/float.asm index 67d71a4..ae25969 100644 --- a/float.asm +++ b/float.asm @@ -34,12 +34,11 @@ .equ FRAC_DIGITS,6 .equ MAX_FRAC,6 -.global printChar +.extern printChar ; ============================================================ ; CODE ; ============================================================ .text - ; ------------------------------------------------------------ ; External routine you provide: ; printChar: prints ASCII character in A @@ -64,11 +63,11 @@ fp_add: ; zero short-cuts ld a,(A_exp) or a - jr nz,.checkB + jr nz,fp_add_checkB ; A==0 => result=B call fp_pack_from_B_into_A ret -.checkB: +fp_add_checkB: ld a,(B_exp) or a ret z @@ -78,7 +77,7 @@ fp_add: ld b,a ld a,(B_sign) xor b - jr z,fp_add_same_sign + jp z,fp_add_same_sign ; signs differ -> magnitude subtract jp fp_add_diff_sign @@ -126,10 +125,10 @@ fp_mul: ; if A==0 or B==0 => 0 ld a,(A_exp) or a - jr z,fp_store_zero_A + jp z,fp_store_zero_A ld a,(B_exp) or a - jr z,fp_store_zero_A + jp z,fp_store_zero_A ; sign = A_sign XOR B_sign ld a,(A_sign) @@ -171,12 +170,12 @@ fp_div: ; A==0 => 0 ld a,(A_exp) or a - jr z,fp_store_zero_A + jp z,fp_store_zero_A ; B==0 => return 0 (simple “error” behavior) ld a,(B_exp) or a - jr z,fp_store_zero_A + jp z,fp_store_zero_A ; sign = A_sign XOR B_sign ld a,(A_sign) @@ -212,12 +211,12 @@ fp_add_same_sign: call add24_A_plus_B ; if carry: shift right, exponent++ - jr nc,.noCarry + jr nc,fp_add_same_sign_noCarry call shr24_A_1 ld a,(A_exp) inc a ld (A_exp),a -.noCarry: +fp_add_same_sign_noCarry: call normalize_A_mant call fp_packA ret @@ -226,14 +225,14 @@ fp_add_same_sign: fp_add_diff_sign: ; compare |A| vs |B|, do larger - smaller, sign = sign(larger) call compare_mag_A_B - jr c,.A_ge_B + jr c,fp_add_diff_sign_A_ge_B ; |B| > |A| => swap call swap_A_B_unpacked -.A_ge_B: +fp_add_diff_sign_A_ge_B: call align_exponents_A_B call sub24_A_minus_B call is_A_mant_zero - jr z,fp_store_zero_A + jp z,fp_store_zero_A call normalize_A_mant call fp_packA ret @@ -248,18 +247,18 @@ fp_unpackA: ld a,(hl) ld (A_exp),a or a - jr z,.zeroA + jp z,fp_unpackA_zeroA inc hl ld a,(hl) ld b,a ; sign bit -> A_sign (0/1) and 080h - jr z,.sa0 + jp z,fp_unpackA_sa0 ld a,1 - jr .sa1 -.sa0: + jr fp_unpackA_sa1 +fp_unpackA_sa0: xor a -.sa1: +fp_unpackA_sa1: ld (A_sign),a ; mantissa bytes with hidden 1 inserted @@ -274,7 +273,7 @@ fp_unpackA: ld a,(hl) ld (A_m0),a ret -.zeroA: +fp_unpackA_zeroA: xor a ld (A_sign),a ld (A_m2),a @@ -288,17 +287,17 @@ fp_unpackB: ld a,(de) ld (B_exp),a or a - jr z,.zeroB + jp z,fp_unpackB_zeroB inc de ld a,(de) ld b,a and 080h - jr z,.sb0 + jp z,fp_unpackB_sb0 ld a,1 - jr .sb1 -.sb0: + jr fp_unpackB_sb1 +fp_unpackB_sb0: xor a -.sb1: +fp_unpackB_sb1: ld (B_sign),a ld a,b @@ -312,7 +311,7 @@ fp_unpackB: ld a,(de) ld (B_m0),a ret -.zeroB: +fp_unpackB_zeroB: xor a ld (B_sign),a ld (B_m2),a @@ -325,7 +324,7 @@ fp_unpackB: fp_packA: ld a,(A_exp) or a - jr nz,.packNZ + jr nz,fp_packA_packNZ ; store 0 ld (hl),0 inc hl @@ -336,7 +335,7 @@ fp_packA: ld (hl),0 ret -.packNZ: +fp_packA_packNZ: ld a,(A_exp) ld (hl),a inc hl @@ -349,13 +348,13 @@ fp_packA: ; apply sign bit7 ld a,(A_sign) or a - jr z,.sign0 + jp z,fp_packA_sign0 ld a,b or 080h - jr .storeB1 -.sign0: + jr fp_packA_storeB1 +fp_packA_sign0: ld a,b -.storeB1: +fp_packA_storeB1: ld (hl),a inc hl ld a,(A_m1) @@ -376,13 +375,13 @@ fp_pack_from_B_into_A: ld b,a ld a,(B_sign) or a - jr z,.bs0 + jp z,fp_pack_from_B_bs0 ld a,b or 080h - jr .bs1 -.bs0: + jr fp_pack_from_B_bs1 +fp_pack_from_B_bs0: ld a,b -.bs1: +fp_pack_from_B_bs1: ld (hl),a inc hl ld a,(B_m1) @@ -413,10 +412,10 @@ align_exponents_A_B: ld b,a ld a,(B_exp) cp b - jr z,.done - jr c,.A_bigger_exp ; B_exp < A_exp + jr z,align_exponents_A_B_done + jr c,align_exponents_A_B_bigger_exp ; B_exp < A_exp call swap_A_B_unpacked ; make A the larger exponent -.A_bigger_exp: +align_exponents_A_B_bigger_exp: ld a,(A_exp) ld b,a ld a,(B_exp) @@ -426,7 +425,7 @@ align_exponents_A_B: call shr24_B_by_A ld a,(A_exp) ld (B_exp),a -.done: +align_exponents_A_B_done: ret @@ -436,46 +435,46 @@ compare_mag_A_B: ld b,a ld a,(B_exp) cp b - jr z,.cmpMant - jr c,.A_ge + jr z,compare_mag_A_B_cmpMant + jr c,compare_mag_A_B_ge or a ret -.A_ge: +compare_mag_A_B_ge: scf ret -.cmpMant: +compare_mag_A_B_cmpMant: ld a,(A_m2) ld b,a ld a,(B_m2) cp b - jr z,.m1 - jr c,.A_ge2 + jr z,compare_mag_A_B_m1 + jr c,compare_mag_A_B_ge2 or a ret -.A_ge2: +compare_mag_A_B_ge2: scf ret -.m1: +compare_mag_A_B_m1: ld a,(A_m1) ld b,a ld a,(B_m1) cp b - jr z,.m0 - jr c,.A_ge3 + jr z,compare_mag_A_B_m0 + jr c,compare_mag_A_B_ge3 or a ret -.A_ge3: +compare_mag_A_B_ge3: scf ret -.m0: +compare_mag_A_B_m0: ld a,(A_m0) ld b,a ld a,(B_m0) cp b - jr c,.A_ge4 + jr c,compare_mag_A_B_ge4 scf ret -.A_ge4: +compare_mag_A_B_ge4: scf ret @@ -519,35 +518,51 @@ swap_A_B_unpacked: ; ============================================================ add24_A_plus_B: + ld a,(B_m0) + ld b,a ld a,(A_m0) - add a,(B_m0) + add a,b ld (A_m0),a + ld a,(B_m1) + ld b,a ld a,(A_m1) - adc a,(B_m1) + adc a,b ld (A_m1),a + ld a,(B_m2) + ld b,a ld a,(A_m2) - adc a,(B_m2) + adc a,b ld (A_m2),a ret ; carry meaningful sub24_A_minus_B: + ld a,(B_m0) + ld b,a ld a,(A_m0) - sub (B_m0) + sub b ld (A_m0),a + ld a,(B_m1) + ld b,a ld a,(A_m1) - sbc a,(B_m1) + sbc a,b ld (A_m1),a + ld a,(B_m2) + ld b,a ld a,(A_m2) - sbc a,(B_m2) + sbc a,b ld (A_m2),a ret is_A_mant_zero: ld a,(A_m2) - or (A_m1) - or (A_m0) + ld b,a + ld a,(A_m1) + or b + ld b,a + ld a,(A_m0) + or b ret @@ -564,22 +579,35 @@ shr24_A_1: ret +shl24_A_1: + ld a,(A_m0) + add a,a + ld (A_m0),a + ld a,(A_m1) + adc a,a + ld (A_m1),a + ld a,(A_m2) + adc a,a + ld (A_m2),a + ret + + ; Shift B mantissa right by A bits (A=0..255) shr24_B_by_A: ld (SHCNT),a ld a,(SHCNT) cp 24 - jr c,.ok + jr c,shr24_B_by_A_ok xor a ld (B_m2),a ld (B_m1),a ld (B_m0),a ret -.ok: +shr24_B_by_A_ok: ld a,(SHCNT) or a ret z -.loop: +shr24_B_by_A_loop: ld a,(B_m2) srl a ld (B_m2),a @@ -592,21 +620,21 @@ shr24_B_by_A: ld a,(SHCNT) dec a ld (SHCNT),a - jr nz,.loop + jr nz,shr24_B_by_A_loop ret normalize_A_mant: call is_A_mant_zero - jr nz,.nz + jr nz,normalize_A_mant_nz xor a ld (A_exp),a ret -.nz: +normalize_A_mant_nz: ld a,(A_m2) bit 7,a ret nz -.left_loop: +normalize_A_mant_left_loop: ld a,(A_m0) add a,a ld (A_m0),a @@ -621,7 +649,7 @@ normalize_A_mant: ld (A_exp),a ld a,(A_m2) bit 7,a - jr z,.left_loop + jr z,normalize_A_mant_left_loop ret @@ -634,15 +662,15 @@ mul8u: ld h,0 ld l,0 ld b,8 -.m8: +mul8u_m8: srl c - jr nc,.noadd + jr nc,mul8u_noadd ld e,a ld d,0 add hl,de -.noadd: +mul8u_noadd: add a,a - djnz .m8 + djnz mul8u_m8 ret @@ -659,56 +687,65 @@ mul24x24_schoolbook: ld (P5),a ; (0,0) offset 0 + ld a,(B_m0) + ld c,a ld a,(A_m0) - ld c,(B_m0) call mul8u call add16_to_P_at0 ; (0,1) offset 1 + ld a,(B_m1) + ld c,a ld a,(A_m0) - ld c,(B_m1) call mul8u call add16_to_P_at1 ; (0,2) offset 2 + ld a,(B_m2) + ld c,a ld a,(A_m0) - ld c,(B_m2) call mul8u call add16_to_P_at2 ; (1,0) offset 1 + ld a,(B_m0) + ld c,a ld a,(A_m1) - ld c,(B_m0) call mul8u call add16_to_P_at1 ; (1,1) offset 2 + ld a,(B_m1) + ld c,a ld a,(A_m1) - ld c,(B_m1) call mul8u call add16_to_P_at2 ; (1,2) offset 3 + ld a,(B_m2) + ld c,a ld a,(A_m1) - ld c,(B_m2) call mul8u call add16_to_P_at3 ; (2,0) offset 2 + ld a,(B_m0) + ld c,a ld a,(A_m2) - ld c,(B_m0) call mul8u call add16_to_P_at2 ; (2,1) offset 3 + ld a,(B_m1) + ld c,a ld a,(A_m2) - ld c,(B_m1) call mul8u call add16_to_P_at3 ; (2,2) offset 4 + ld a,(B_m2) + ld c,a ld a,(A_m2) - ld c,(B_m2) call mul8u call add16_to_P_at4 @@ -764,17 +801,17 @@ add16_to_P_at4: norm_product_to_A: ld a,(P5) bit 7,a - jr z,.shift23 + jr z,norm_product_shift23 ld a,24 call shr48_P_by_A ld a,(A_exp) inc a ld (A_exp),a - jr .take -.shift23: + jr norm_product_take +norm_product_shift23: ld a,23 call shr48_P_by_A -.take: +norm_product_take: ld a,(P2) ld (A_m2),a ld a,(P1) @@ -789,7 +826,7 @@ shr48_P_by_A: ld a,(SHCNT) or a ret z -.loop: +shr48_P_by_A_loop: ld a,(P5) srl a ld (P5),a @@ -811,7 +848,7 @@ shr48_P_by_A: ld a,(SHCNT) dec a ld (SHCNT),a - jr nz,.loop + jr nz,shr48_P_by_A_loop ret @@ -842,22 +879,22 @@ div_mantissas_to_A: ld (A_m0),a ld b,24 -.div_loop: +div_mantissas_loop: call shl24_A_1 call shl48_P_1 ; subtract divisor from high 24 bits of P (P5..P3) call sub24_Phigh_minus_B - jr c,.restore + jr c,div_mantissas_restore ; success => set quotient LSB = 1 ld a,(A_m0) or 001h ld (A_m0),a - jr .next -.restore: + jr div_mantissas_next +div_mantissas_restore: call add24_Phigh_plus_B -.next: - djnz .div_loop +div_mantissas_next: + djnz div_mantissas_loop ret @@ -866,12 +903,12 @@ shl48_P_by_A: ld a,(SHCNT) or a ret z -.loop: +shl48_P_by_A_loop: call shl48_P_1 ld a,(SHCNT) dec a ld (SHCNT),a - jr nz,.loop + jr nz,shl48_P_by_A_loop ret @@ -898,27 +935,39 @@ shl48_P_1: sub24_Phigh_minus_B: + ld a,(B_m0) + ld b,a ld a,(P3) - sub (B_m0) + sub b ld (P3),a + ld a,(B_m1) + ld b,a ld a,(P4) - sbc a,(B_m1) + sbc a,b ld (P4),a + ld a,(B_m2) + ld b,a ld a,(P5) - sbc a,(B_m2) + sbc a,b ld (P5),a ret ; carry set indicates borrow add24_Phigh_plus_B: + ld a,(B_m0) + ld b,a ld a,(P3) - add a,(B_m0) + add a,b ld (P3),a + ld a,(B_m1) + ld b,a ld a,(P4) - adc a,(B_m1) + adc a,b ld (P4),a + ld a,(B_m2) + ld b,a ld a,(P5) - adc a,(B_m2) + adc a,b ld (P5),a ret @@ -932,19 +981,19 @@ fp_print: ; zero? ld a,(hl) or a - jr nz,.nz + jr nz,fp_print_nz ld a,'0' call printChar ld a,'.' call printChar ld b,FRAC_DIGITS -.zf: +fp_print_zf: ld a,'0' call printChar - djnz .zf + djnz fp_print_zf ret -.nz: +fp_print_nz: ; EXP -> PR_E (unbiased) ld a,(hl) sub FP_BIAS @@ -955,12 +1004,12 @@ fp_print: ld a,(hl) ld b,a and 080h - jr z,.ps0 + jp z,fp_print_ps0 ld a,1 - jr .ps1 -.ps0: + jr fp_print_ps1 +fp_print_ps0: xor a -.ps1: +fp_print_ps1: ld (PR_SIGN),a ; mantissa with hidden 1 inserted @@ -978,10 +1027,10 @@ fp_print: ; print '-' ld a,(PR_SIGN) or a - jr z,.mag + jp z,fp_print_mag ld a,'-' call printChar -.mag: +fp_print_mag: ; S = (E - 23) ld a,(PR_E) sub 23 @@ -995,7 +1044,7 @@ fp_print: ld (PR_R3),a bit 7,a - jr z,.S_nonneg + jp z,fp_print_S_nonneg ; S negative: INT = [M2][M1][M0][00] (i.e., M << 8), then shift right by -S neg @@ -1011,14 +1060,14 @@ fp_print: ld (PR_INT3),a call shr32_INT_to_INT_with_remainder - jr .print_int_and_frac + jr fp_print_print_int_and_frac -.S_nonneg: +fp_print_S_nonneg: ; S non-negative: INT = M (24-bit) then shift left S (cap at 31) cp 32 - jr c,.doShl + jr c,fp_print_doShl ld a,31 -.doShl: +fp_print_doShl: ld b,a ld a,(PR_M0) ld (PR_INT0),a @@ -1030,19 +1079,19 @@ fp_print: ld (PR_INT3),a call shl32_INT_by_B -.print_int_and_frac: +fp_print_print_int_and_frac: call print_u32_dec ld a,'.' call printChar ld b,FRAC_DIGITS -.fr: +fp_print_fr: call mul_remainder_by_10 ld a,(PR_R3) add a,'0' call printChar xor a ld (PR_R3),a - djnz .fr + djnz fp_print_fr ret @@ -1053,7 +1102,7 @@ shr32_INT_to_INT_with_remainder: ld a,b or a ret z -.loop: +shr32_INT_to_INT_with_remainder_loop: ld a,(PR_INT3) srl a ld (PR_INT3),a @@ -1071,7 +1120,7 @@ shr32_INT_to_INT_with_remainder: add a,a adc a,0 ld (PR_R3),a - djnz .loop + djnz shr32_INT_to_INT_with_remainder_loop ret @@ -1079,7 +1128,7 @@ shl32_INT_by_B: ld a,b or a ret z -.loop: +shl32_INT_by_B_loop: ld a,(PR_INT0) add a,a ld (PR_INT0),a @@ -1092,7 +1141,7 @@ shl32_INT_by_B: ld a,(PR_INT3) adc a,a ld (PR_INT3),a - djnz .loop + djnz shl32_INT_by_B_loop ret @@ -1111,21 +1160,28 @@ mul_remainder_by_10: ; Print PR_INT (u32) as decimal print_u32_dec: ld a,(PR_INT0) - or (PR_INT1) - or (PR_INT2) - or (PR_INT3) - jr nz,.nz + ld b,a + ld a,(PR_INT1) + or b + ld b,a + ld a,(PR_INT2) + or b + ld b,a + ld a,(PR_INT3) + or b + jr nz,print_u32_dec_nz ld a,'0' call printChar ret -.nz: +print_u32_dec_nz: xor a ld (DIGLEN),a -.dloop: +print_u32_dec_dloop: call u32_div10_inplace ; remainder in A, quotient back in PR_INT ld hl,DIGBUF ld b,0 - ld c,(DIGLEN) + ld a,(DIGLEN) + ld c,a add hl,bc add a,'0' ld (hl),a @@ -1133,15 +1189,21 @@ print_u32_dec: inc a ld (DIGLEN),a ld a,(PR_INT0) - or (PR_INT1) - or (PR_INT2) - or (PR_INT3) - jr nz,.dloop + ld b,a + ld a,(PR_INT1) + or b + ld b,a + ld a,(PR_INT2) + or b + ld b,a + ld a,(PR_INT3) + or b + jr nz,print_u32_dec_dloop ; print in reverse ld a,(DIGLEN) ld b,a -.pr: +print_u32_dec_pr: dec b ld hl,DIGBUF ld c,b @@ -1151,7 +1213,7 @@ print_u32_dec: call printChar ld a,c or a - jr nz,.pr + jr nz,print_u32_dec_pr ret @@ -1159,29 +1221,29 @@ print_u32_dec: u32_div10_inplace: ld b,0 ; remainder ld hl,PR_INT3 - call .step + call u32_div10_step inc hl - call .step + call u32_div10_step inc hl - call .step + call u32_div10_step inc hl - call .step + call u32_div10_step ld a,b ret -.step: +u32_div10_step: ; DE = remainder*256 + byte ld a,b ld d,a ld e,(hl) ld c,0 ; quotient byte -.div: +u32_div10_div: ld a,d or a - jr nz,.sub + jr nz,u32_div10_sub ld a,e cp 10 - jr c,.done -.sub: + jr c,u32_div10_done +u32_div10_sub: ld a,e sub 10 ld e,a @@ -1189,8 +1251,8 @@ u32_div10_inplace: sbc a,0 ld d,a inc c - jr .div -.done: + jr u32_div10_div +u32_div10_done: ld (hl),c ld b,e ret @@ -1213,22 +1275,22 @@ fp_parse: ; optional sign ld a,(de) cp '-' - jr nz,.chkplus + jr nz,fp_parse_chkplus ld a,1 ld (P_SIGN),a inc de - jr .intpart -.chkplus: + jr fp_parse_intpart +fp_parse_chkplus: ld a,(de) cp '+' - jr nz,.intpart + jr nz,fp_parse_intpart inc de -.intpart: +fp_parse_intpart: ld a,(de) call is_digit - jr nc,.maybe_dot -.il: + jr nc,fp_parse_maybe_dot +fp_parse_il: ld a,(de) sub '0' ld c,a @@ -1237,19 +1299,19 @@ fp_parse: inc de ld a,(de) call is_digit - jr c,.il + jr c,fp_parse_il -.maybe_dot: +fp_parse_maybe_dot: ld a,(de) cp '.' - jr nz,.finish_scaled + jr nz,fp_parse_finish_scaled inc de ld b,MAX_FRAC -.fl: +fp_parse_fl: ld a,(de) call is_digit - jr nc,.finish_scaled + jr nc,fp_parse_finish_scaled ld a,(de) sub '0' ld c,a @@ -1259,16 +1321,16 @@ fp_parse: inc a ld (P_FRACN),a inc de - djnz .fl + djnz fp_parse_fl -.finish_scaled: +fp_parse_finish_scaled: ; convert scaled u32 to float into (HL) call fp_from_u32_scaled_to_A ; divide by 10^k if needed ld a,(P_FRACN) or a - jr z,.apply_sign + jp z,fp_parse_apply_sign ; DE = &pow10_table[k] push hl @@ -1283,7 +1345,7 @@ fp_parse: pop hl call fp_div -.apply_sign: +fp_parse_apply_sign: ld a,(P_SIGN) or a ret z @@ -1296,12 +1358,12 @@ fp_parse: is_digit: cp '0' - jr c,.no + jr c,is_digit_no cp '9'+1 - jr nc,.no + jr nc,is_digit_no scf ret -.no: +is_digit_no: or a ret @@ -1335,17 +1397,25 @@ u32_mul10_scaled: call shl32_R_by_B ; P = PR_INT + PR_R + ld a,(PR_R0) + ld b,a ld a,(PR_INT0) - add a,(PR_R0) + add a,b ld (P_S0),a + ld a,(PR_R1) + ld b,a ld a,(PR_INT1) - adc a,(PR_R1) + adc a,b ld (P_S1),a + ld a,(PR_R2) + ld b,a ld a,(PR_INT2) - adc a,(PR_R2) + adc a,b ld (P_S2),a + ld a,(PR_R3) + ld b,a ld a,(PR_INT3) - adc a,(PR_R3) + adc a,b ld (P_S3),a ret @@ -1354,7 +1424,7 @@ shl32_R_by_B: ld a,b or a ret z -.loop: +shl32_R_by_B_loop: ld a,(PR_R0) add a,a ld (PR_R0),a @@ -1367,7 +1437,7 @@ shl32_R_by_B: ld a,(PR_R3) adc a,a ld (PR_R3),a - djnz .loop + djnz shl32_R_by_B_loop ret @@ -1391,10 +1461,16 @@ u32_add8_scaled: ; Convert P_S (u32) to float at (HL). Positive only; sign handled by caller. fp_from_u32_scaled_to_A: ld a,(P_S0) - or (P_S1) - or (P_S2) - or (P_S3) - jr nz,.nz + ld b,a + ld a,(P_S1) + or b + ld b,a + ld a,(P_S2) + or b + ld b,a + ld a,(P_S3) + or b + jr nz,fp_from_u32_scaled_to_A_nz ld (hl),0 inc hl ld (hl),0 @@ -1404,34 +1480,36 @@ fp_from_u32_scaled_to_A: ld (hl),0 ret -.nz: +fp_from_u32_scaled_to_A_nz: ; find MSB index in B (0..31) ld b,31 ld a,(P_S3) ld c,a or a - jr nz,.scan + jr nz,fp_from_u32_scaled_to_A_scan ld b,23 ld a,(P_S2) ld c,a or a - jr nz,.scan + jr nz,fp_from_u32_scaled_to_A_scan ld b,15 ld a,(P_S1) ld c,a or a - jr nz,.scan + jr nz,fp_from_u32_scaled_to_A_scan ld b,7 ld a,(P_S0) ld c,a -.scan: -.find: +fp_from_u32_scaled_to_A_scan: +fp_from_u32_scaled_to_A_find: bit 7,c - jr nz,.found - add c,c + jr nz,fp_from_u32_scaled_to_A_found + ld a,c + add a,a + ld c,a dec b - jr .find -.found: + jr fp_from_u32_scaled_to_A_find +fp_from_u32_scaled_to_A_found: ; EXP = FP_BIAS + B ld a,b add a,FP_BIAS @@ -1466,7 +1544,64 @@ fp_from_u32_scaled_to_A: ld (hl),a ret -.data + +; ============================================================ +; BSS / WORKSPACE +; ============================================================ +.balign 16 +.bss + +; Unpacked A +A_exp: .space 1 +A_sign: .space 1 +A_m2: .space 1 +A_m1: .space 1 +A_m0: .space 1 + +; Unpacked B +B_exp: .space 1 +B_sign: .space 1 +B_m2: .space 1 +B_m1: .space 1 +B_m0: .space 1 + +; 48-bit workspace (P0 LSB .. P5 MSB) +P0: .space 1 +P1: .space 1 +P2: .space 1 +P3: .space 1 +P4: .space 1 +P5: .space 1 + +SHCNT: .space 1 + +; Print temps +PR_SIGN: .space 1 +PR_E: .space 1 +PR_M2: .space 1 +PR_M1: .space 1 +PR_M0: .space 1 +PR_INT0: .space 1 +PR_INT1: .space 1 +PR_INT2: .space 1 +PR_INT3: .space 1 +PR_R0: .space 1 +PR_R1: .space 1 +PR_R2: .space 1 +PR_R3: .space 1 + +; Parse temps +P_SIGN: .space 1 +P_FRACN: .space 1 +P_S0: .space 1 +P_S1: .space 1 +P_S2: .space 1 +P_S3: .space 1 + +; Digit buffer +DIGBUF: .space 1 +DIGLEN: .space 1 + ; ============================================================ ; pow10_table: 10^k constants (k=0..6) in THIS float encoding ; Verified: @@ -1478,6 +1613,7 @@ fp_from_u32_scaled_to_A: ; 100000.0 = 143 43 50 00 ; 1000000.0= 146 74 24 00 ; ============================================================ +.text pow10_table: .byte 127, 0x00, 0x00, 0x00 ; 10^0 = 1 .byte 130, 0x20, 0x00, 0x00 ; 10^1 = 10 @@ -1486,60 +1622,3 @@ pow10_table: .byte 140, 0x1C, 0x40, 0x00 ; 10^4 = 10000 .byte 143, 0x43, 0x50, 0x00 ; 10^5 = 100000 .byte 146, 0x74, 0x24, 0x00 ; 10^6 = 1000000 - - -; ============================================================ -; BSS / WORKSPACE -; ============================================================ -.bss - -; Unpacked A -.comm A_exp,1 -.comm A_sign,1 -.comm A_m2,1 -.comm A_m1,1 -.comm A_m0,1 - -; Unpacked B -.comm B_exp,1 -.comm B_sign,1 -.comm B_m2,1 -.comm B_m1,1 -.comm B_m0,1 - -; 48-bit workspace (P0 LSB .. P5 MSB) -.comm P0,1 -.comm P1,1 -.comm P2,1 -.comm P3,1 -.comm P4,1 -.comm P5,1 - -.comm SHCNT,1 - -; Print temps -.comm PR_SI,1 -.comm PR_E,1 -.comm PR_M2,1 -.comm PR_M1,1 -.comm PR_M0,1 -.comm PR_INT0,1 -.comm PR_INT1,1 -.comm PR_INT2,1 -.comm PR_INT3,1 -.comm PR_R0,1 -.comm PR_R1,1 -.comm PR_R2,1 -.comm PR_R3,1 - -; Parse temps -.comm P_SIGN,1 -.comm P_FRACN,1 -.comm P_S0,1 -.comm P_S1,1 -.comm P_S2,1 -.comm P_S3,1 - -; Digit buffer -.comm DIGBUF,1 -.comm DIGLEN,1 diff --git a/linker.cmd b/linker.cmd new file mode 100644 index 0000000..addb566 --- /dev/null +++ b/linker.cmd @@ -0,0 +1,6 @@ +SECTIONS +{ + .text 0xE000 : { *(.text) } + .data 0x7000 : { *(.data) } + .bss : { *(.bss) } +} diff --git a/printChar.asm b/printChar.asm new file mode 100644 index 0000000..dec7ebe --- /dev/null +++ b/printChar.asm @@ -0,0 +1,4 @@ +; Stub printChar: does nothing +.global printChar +printChar: + ret