speeding up assembly routine

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

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

Use a jump table:

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

table:
dw move1
dw move2
...
```

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

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]

(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)
```

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

```    ld    de,table
ld    hl,(move)  ; note that move now has to be 16 bit!
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!
ld    e,(hl)
inc   hl
ld    d,(hl)
ex   de,hl
jp    (hl)
```

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.

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
.	.
.	.```

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

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

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

```
ld hl,table
ld a,(move)
dec a
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!

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

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

begin:

ld a,(move)
ld b,a