From cca3157c66456ca8bd7bec57673f6a871e8d5701 Mon Sep 17 00:00:00 2001 From: Clanmaster21 Date: Thu, 17 Oct 2019 21:45:27 +0100 Subject: [PATCH] addHL and subHL affect flags, and are smaller (#30) * addHL and subHL affect flags, and are smaller Most importantly, addHL and subHL now affect the flags as you would expect from a 16 bit addition/subtraction. This seems like it'd be preferred behaviour, however I realise any code relying on it not affecting flags would break. One byte saved in addHL, and two bytes saved in subHL. Due to the branching nature of the original code, it's difficult to compare speeds, subHL is either 1 or 6 cycles faster depending on branching, and addHL is between -1 and 3 cycles faster. If the chance of a carry is 50%, addHL is expected to be a cycle faster, but for a chance of carry below 25% (so a < 0x40) this will be up to a cycle slower. * Update core.asm * Reworked one use of addHL By essentially inlining both addHL and cpHLDE, 100 cycles are saved, but due to the registers not needing preserving, a byte is saved too. * Corrected spelling error in comment * Reworked second use of addHL 43 cycles saved, and no more addHL in critical loops. No bytes saved or used. * Fixed tabs and spacing, and made a comment clearer. * Clearer comments * Adopted push/pop notation --- apps/zasm/symbol.asm | 20 ++++++++++++++------ kernel/core.asm | 31 ++++++++++++++----------------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/apps/zasm/symbol.asm b/apps/zasm/symbol.asm index 33a7aaa..1504bd3 100644 --- a/apps/zasm/symbol.asm +++ b/apps/zasm/symbol.asm @@ -110,12 +110,14 @@ symRegister: ; Is our new name going to make us go out of bounds? push hl ; --> lvl 2 push de ; --> lvl 3 + ld d, 0 + ld e, c + add hl, de ; if carry set here, sbc will carry too ld e, (ix+2) ; DE --> pointer to record list, which is also ld d, (ix+3) ; the end of names pool ; DE --> names end - ld a, c - call addHL - call cpHLDE + + sbc hl, de ; compares hl and de destructively pop de ; <-- lvl 3 pop hl ; <-- lvl 2 jr nc, .outOfMemory ; HL >= DE @@ -190,9 +192,15 @@ _symFind: jr z, .end ; match! Z already set, IY and HL placed. .skip: ; ok, next! - ld a, (iy) ; name len again - call addHL ; advance HL by A chars - inc iy \ inc iy \ inc iy + + push de ; --> lvl 1 + ld de, 0x0003 + add iy, de ; faster and shorter than three inc's + ld e, (iy-3) ; offset is also compulsory, so no extra bytes used + ; (iy-3) holds the name length of the string just processed + add hl, de ; advance HL by (iy-3) characters + pop de ; <-- lvl 1 + djnz .loop ; end of the chain, nothing found .nothing: diff --git a/kernel/core.asm b/kernel/core.asm index a88cf2f..1289351 100644 --- a/kernel/core.asm +++ b/kernel/core.asm @@ -53,28 +53,25 @@ intoIX: ret ; add the value of A into HL +; affects carry flag according to the 16-bit addition, Z, S and P untouched. addHL: - push af - add a, l - jr nc, .end ; no carry? skip inc - inc h -.end: - ld l, a - pop af + push de + ld d, 0 + ld e, a + add hl, de + pop de ret + ; subtract the value of A from HL +; affects flags according to the 16-bit subtraction. subHL: - push af - ; To avoid having to swap L and A, we sub "backwards", that is, we add - ; a NEGated value. This means that the carry flag is inverted - neg - add a, l - jr c, .end ; if carry, no carry. :) - dec h -.end: - ld l, a - pop af + push de + ld d, 0 + ld e, a + or a ;reset carry flag + sbc hl, de ;There is no 'sub hl, de', so we must use sbc + pop de ret ; Compare HL with DE and sets Z and C in the same way as a regular cp X where