Browse Source

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
pull/66/head
Clanmaster21 Virgil Dupras 4 years ago
parent
commit
cca3157c66
2 changed files with 28 additions and 23 deletions
  1. +14
    -6
      apps/zasm/symbol.asm
  2. +14
    -17
      kernel/core.asm

+ 14
- 6
apps/zasm/symbol.asm View File

@@ -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:


+ 14
- 17
kernel/core.asm View File

@@ -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


Loading…
Cancel
Save