Re: A86: Efficiency Problem
[Prev][Next][Index][Thread]
Re: A86: Efficiency Problem
At 20:27 1998-12-10 -0500, you wrote:
>
>The following is a fragment(?) of code from a game I'm working on called Nerd
>Quest I. It's the a part that displays the current weapon on the status
>screen. It seems to me that this code is incredibly ineffiecient, and there's
>gotta be a faster way of doing this.
Hehe... your source code is probably _the_ classic example of beginners
mistakes :-)
Instead of all the checks, you use a lookuptable:
ld a,(Weapon) ;Weapon EQU $F602
add a,a
ld hl,lookuptable
ld d,0
ld e,a
add hl,de
call _ldhlind ; $4010 (or LD_HL_MHL)
; And we're done! 14+2*16 = 46 bytes
call_puts
jp somewhere
lookuptable:
.dw W1,W2,W3,W4,W5,W6,W7,W8,W9,W10
.dw W11,W12,W13,W14,W15,W16
...or the smaller, slightly slower, version...
ld hl,DataSection
ld a,(Weapon)
or a
jr z,Done
ld b,a
xor a
SkipStrings:
ld c,$FF ; Make sure the loop doesn't terminate because of the counter
cpir ; Search string for nullterminating zero (that is - end of string)
inc hl ; HL -> next string
djnz SkipStrings
Done:
; Done! 18 bytes
call _puts
jp somewhere
--
Real name: Jimmy Mårdell
Email: mailto:yarin@acc.umu.se
Homepage: http://www.acc.umu.se/~yarin/
Icarus Productions homepage: http://icarus.ganymed.org/
References: