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
This commit is contained in:
Clanmaster21 2019-10-17 21:45:27 +01:00 committed by Virgil Dupras
parent f806786bd3
commit cca3157c66
2 changed files with 28 additions and 23 deletions

View File

@ -110,12 +110,14 @@ symRegister:
; Is our new name going to make us go out of bounds? ; Is our new name going to make us go out of bounds?
push hl ; --> lvl 2 push hl ; --> lvl 2
push de ; --> lvl 3 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 e, (ix+2) ; DE --> pointer to record list, which is also
ld d, (ix+3) ; the end of names pool ld d, (ix+3) ; the end of names pool
; DE --> names end ; DE --> names end
ld a, c
call addHL sbc hl, de ; compares hl and de destructively
call cpHLDE
pop de ; <-- lvl 3 pop de ; <-- lvl 3
pop hl ; <-- lvl 2 pop hl ; <-- lvl 2
jr nc, .outOfMemory ; HL >= DE jr nc, .outOfMemory ; HL >= DE
@ -190,9 +192,15 @@ _symFind:
jr z, .end ; match! Z already set, IY and HL placed. jr z, .end ; match! Z already set, IY and HL placed.
.skip: .skip:
; ok, next! ; ok, next!
ld a, (iy) ; name len again
call addHL ; advance HL by A chars push de ; --> lvl 1
inc iy \ inc iy \ inc iy 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 djnz .loop
; end of the chain, nothing found ; end of the chain, nothing found
.nothing: .nothing:

View File

@ -53,28 +53,25 @@ intoIX:
ret ret
; add the value of A into HL ; add the value of A into HL
; affects carry flag according to the 16-bit addition, Z, S and P untouched.
addHL: addHL:
push af push de
add a, l ld d, 0
jr nc, .end ; no carry? skip inc ld e, a
inc h add hl, de
.end: pop de
ld l, a
pop af
ret ret
; subtract the value of A from HL ; subtract the value of A from HL
; affects flags according to the 16-bit subtraction.
subHL: subHL:
push af push de
; To avoid having to swap L and A, we sub "backwards", that is, we add ld d, 0
; a NEGated value. This means that the carry flag is inverted ld e, a
neg or a ;reset carry flag
add a, l sbc hl, de ;There is no 'sub hl, de', so we must use sbc
jr c, .end ; if carry, no carry. :) pop de
dec h
.end:
ld l, a
pop af
ret ret
; Compare HL with DE and sets Z and C in the same way as a regular cp X where ; Compare HL with DE and sets Z and C in the same way as a regular cp X where