From 6c0052578eab3668b1a01295c3e8c54dc6034cd9 Mon Sep 17 00:00:00 2001 From: Clanmaster21 Date: Thu, 17 Oct 2019 20:33:01 +0100 Subject: [PATCH] Lots of minor optimisations Removed an unecessary conditional jump in main.asm, saving 7 cycles and 2 bytes. Removed a function called "subDEfromHL" or something like that, and replaced it with `sbc hl, de`, saving 10 bytes total and 61 cycles. I also rewrote enterParens and strlen to use cpir instead of cpi in a loop, saving 14 cycles per character and also 6 bytes (bc needed to be zeroed in enterParens). --- apps/zasm/instr.asm | 6 ++++-- apps/zasm/main.asm | 3 +-- apps/zasm/util.asm | 28 +++++----------------------- 3 files changed, 10 insertions(+), 27 deletions(-) diff --git a/apps/zasm/instr.asm b/apps/zasm/instr.asm index 20f8455..f03228c 100644 --- a/apps/zasm/instr.asm +++ b/apps/zasm/instr.asm @@ -739,12 +739,14 @@ getUpcode: push de ; Don't let go of this, that's our dest push hl call zasmGetPC ; --> HL - ex de, hl + ex de, hl ; zasmGetPC sets carry on add hl, de + ; Since it's a PC value, a carry would mean we've overflowed memory. + ; That means we can safely assume the carry is unset. pop hl call intoHL dec hl ; what we write is "e-2" dec hl - call subDEFromHL + sbc hl, de ; flags don't need preserving, and carry unset pop de ; Still have it? good ; HL contains our number and we'll check its bounds. If It's negative, ; H is going to be 0xff and L has to be >= 0x80. If it's positive, diff --git a/apps/zasm/main.asm b/apps/zasm/main.asm index d19bb99..9b903cb 100644 --- a/apps/zasm/main.asm +++ b/apps/zasm/main.asm @@ -115,8 +115,7 @@ zasmParseFile: ret nz ; error ld a, b ; TOK_* cp TOK_EOF - jr z, .eof - jr .loop + jr nz, .loop .eof: call zasmIsLocalPass jr nz, .end ; EOF and not local pass diff --git a/apps/zasm/util.asm b/apps/zasm/util.asm index 1c3bc44..f14a6e4 100644 --- a/apps/zasm/util.asm +++ b/apps/zasm/util.asm @@ -12,18 +12,6 @@ callHL: jp (hl) ret -; HL - DE -> HL -subDEFromHL: - push af - ld a, l - sub e - ld l, a - ld a, h - sbc a, d - ld h, a - pop af - ret - ; make Z the opposite of what it is now toggleZ: jp z, unsetZ @@ -37,17 +25,13 @@ strlen: push hl ld bc, 0 ld a, 0 ; look for null char -.loop: - cpi - jp z, .found - jr .loop + cpir .found: ; How many char do we have? the (NEG BC)-1, which started at 0 and ; decreased at each CPI call. In this routine, we stay in the 8-bit ; realm, so C only. ld a, c - neg - dec a + cpl ; -a = (~a)+1, so (-a)-1 = ~a pop hl pop bc ret @@ -132,12 +116,10 @@ enterParens: cp '(' ret nz ; nothing to do push hl - ld a, 0 ; look for null char + ld bc, 0 ; so we can use cpir + ld a, b ; look for null char ; advance until we get null -.loop: - cpi - jp z, .found - jr .loop + cpir .found: dec hl ; cpi over-advances. go back to null-char dec hl ; looking at the last char before null