Help needed with a bit of code...
My minds gone blank again I'm afraid, I'm having trouble with this routine that loads variables into memory then calls the corresponding routine.
Theres 3 variables which makes it awkward. I'm sure theres an easy way, but I just cant see the wood for the trees at the minute...
Thanks in advance...
Theres 3 variables which makes it awkward. I'm sure theres an easy way, but I just cant see the wood for the trees at the minute...
Thanks in advance...
ld b,3 ld hl, cor ld de,adds loop ld a,(de) ld (caller+1),a inc de ld a,(de) ld (caller+2),a; loads address to be called in de inc de ;ld a,(hl) ;hl stores variables ;now i need to load the variables into the address stored in de ??? push af push de push bc caller call 56003 ;routine to be called pop bc pop de pop af djnz loop ret adds defw 58239 defw 56003 defw 51173 ;start adress of routines cor defb 2,2,4 defb 10,10,4 defb 12,14,6 ;variables for routine 3 bytes prior to start
Post edited by daveysludge on
Comments
There's a JP (HL) at $1715, but it only takes one byte, so it might as well just be in your own code. Still thinking about the variables question ...
--
OK, here's my other thought. Store each sub-routine address followed by its three variables: address, byte, byte, byte. Point HL to the one you want, then:
LD E,(HL) : INC HL : LD D,(HL) : INC HL : EX DE,HL : CALL $1715
then when the sub-routine is entered DE will be pointing to its first variable.
If you need that 'adds' table to point to your routines, why not have them point three bytes earlier? And instead of programming the addresses, just refer to a label for each routine like:
defw routine_1 - 3
defw routine_2 - 3
defw routine_3 - 3
Then you load the address you get from the table into DE, point HL at your numerical data, then do LDI three times to copy three bytes from (HL) to (DE). PUSH everything you want to preserve, then EX HL,DE to swap the pointers over and CALL (HL), since it will now be pointing to the start of your routine. When you POP those values again, DE and HL will be back as they were.
And don't forget, if you run out of address pointers, you can use IX and IY in most places where you use HL, it'll just be a fraction slower. They can be very convenient though to use as table pointers, freeing up DE and HL for different tasks. And EX DE,HL can come in very handy when using one address to look up another. And don't forget you can always PUSH DE and later POP the value into HL instead later, for example.
- IONIAN-GAMES.com -
The poking the call routine isn't the problem, that works fine, its the variables, particularly this bit:
I need to load a register with the value of the contents of DE and then load that address with the contents of HL.
I cant seem to find the right command,
Although that doesn't seem terribly efficient and looks like it would trash your address table (unless there is some in-between code missing). I can't help but feel if it were clearer what you were trying to achieve overall, there'd be a better approach. Calling a parameterized jump block for example.
I'm basically trying to avoid typing out loads of this...
Stack abuse FTW. :-)
37 bytes, can anyone improve on it?
with
http://z80-heaven.wikidot.com/instructions-set:ldi
Memory is getting tight so any saving is essential!
You could use the af/'af for the count, that was in my mind originally then went off on a merry road of insanity with the exx.
ld a,2 ld hl,dat loop ex af,af' ld e,(hl) inc hl ld d,(hl) inc hl ldi ldi ldi ld (caller+1),de push de push hl caller call 56000 pop hl pop de ex af,af' dec a jr nz, loop ret dat defw 56000 defb 14,20,3 defw 56000 defb 5,5,6