speeding up assembly routine

Page 1/10
| 2 | 3 | 4 | 5 | 6

By norakomi

Paladin (992)

norakomi's picture

09-09-2005, 10:28

high !!

there is this routine to call the movement behavior of a certain enemy.

routine goes like this.

ld a,(move)
cp 1
jp z,move1
cp 2
jp z,move2
cp 3
jp z,move3
....
...
..
.
cp 15
jp z,move15

well, this routine not only takes a lot of space, it also is pretty slow if
move15 is used a lot in my game.
What way is there to speed this routine up???

Login or register to post comments

By AuroraMSX

Paragon (1901)

AuroraMSX's picture

09-09-2005, 10:36

Use a jump table:

    ld    de,table
    ld    hl,(move)  ; note that move now has to be 16 bit!
    add   hl,de
    ex    de,hl
    ld    hl,(de)
    jp    (hl)

table:
    dw move1
    dw move2
    ...

(Although this will be slower for the first few 'moveX' jumps)

[edit]
Ow, besides 'move' having to be 16 bit, you'll need to use values 0, 2, 4, 6 instead of 0, 1, 2, 3, or shift the value of move before adding it to hl, since every entry in the table is 2 bytes in size.
[/edit]

By MicroTech

Champion (384)

MicroTech's picture

09-09-2005, 12:36

(move) could contain the address of the routine to be called instead of a move-code, so you could simply reduce the whole thing to:

   ld hl,(moveRoutineAddress)
   jp (hl)

By marison

Expert (104)

marison's picture

09-09-2005, 12:43

    ld    de,table
    ld    hl,(move)  ; note that move now has to be 16 bit!
    add   hl,de
    ex    de,hl
    ld    hl,(de)
    jp    (hl)

    ld    de,table
    ld    hl,(move)  ; note that move now has to be 16 bit!
    add   hl,de
    ex    de,hl
    ld    hl,(de)
    jp    (hl)

ld hl,(de) is not possible. Use:

    ld    de,table
    ld    hl,(move)  ; note that move now has to be 16 bit!
    add   hl,de
    ld    e,(hl)
    inc   hl
    ld    d,(hl)
    ex   de,hl
    jp    (hl)

instead.

Another way is to use the enemy move parameter as the address of the related move routine.

ld     l,(move)
ld     h,(move+1)
jp     (hl)

Obviously (just remembering) is not neccessary the table in the last sample.

By Sonic_aka_T

Enlighted (4130)

Sonic_aka_T's picture

09-09-2005, 12:50

ld a,(move)
cp 1
jp z,move1
cp 2
jp z,move2
cp 3
jp z,move3
For similar (but short) sequences like that, instead of CP try using DEC A. For shorter sequences, that would prolly be faster than using 16bit routines as well. Since in tests like these A is usually discarded anyhow, it's pretty much the same thing, but it saves you some speed/space.

	LD	A,(MOVE)
	DEC	A
	JP	Z,MOVE01
	DEC	A
	JP	Z,MOVE02
	DEC	A
	JP	Z,MOVE03
	.	.
	.	.

By Tanni

Hero (556)

Tanni's picture

09-09-2005, 13:10

If the MOVE-routines are very short, use JR instead of JP!

By Tanni

Hero (556)

Tanni's picture

09-09-2005, 13:16

If your're using JR, then you can't use jump tables, of course.

By jltursan

Prophet (2180)

jltursan's picture

09-09-2005, 13:25

One more routine!, here's my solution using tables and only HL and A registers... Wink

	
	ld hl,table
	ld a,(move)
	dec a
	add a,a
	add a,l
	jp nc,NOF
	inc h
NOF:	ld l,a
	ld a,(hl)
	inc hl
	ld h,(hl)
	ld l,a
	jp (hl)

Btw, cool tip Sonic_aka_T!, I like it! Smile

By BiFi

Enlighted (4348)

BiFi's picture

09-09-2005, 13:49

Tanni: jr's may be shorter, but jp's are faster...

anyway, I'd go for jump tables as well... they're small and always the same speed, no matter which call you need to do...

By NYYRIKKI

Enlighted (5384)

NYYRIKKI's picture

09-09-2005, 14:31


	org #c000
	jp move1
	jp move2
	jp move3
	jp move4
	.
	.
	.
	jp move15

begin:

	ld a,(move)
	ld b,a
	add a,a
	add a,b
	ld l,a
	ld h,#C0
	jp (hl)

By marison

Expert (104)

marison's picture

09-09-2005, 14:47

@NYYRIKKI:
Good and simple coding. The first jp move statement should be in a address with LSB 00.

Use h register instead of b to avoid change b value.

Page 1/10
| 2 | 3 | 4 | 5 | 6