jp (hl)
I'm trying to get an address from a list and jump to it, but the code below is the only way I can get it to work.
I had assumed that JP (HL) would jump to the address in the first entry of the list that HL points to, but it seems that the address from the list has to actually be in HL itself? The Z80 websites I reference don't mention this (they are pretty lean with explanations TBH) so it was quite frustrating.
It makes no sense to me, so I'm curious how this actually works? If it's just a quirk of Z80 then fine, but it's a few extra lines of code to extract the address and put it back in HL, and I'm just wondering if they are necessary?
Thanks!
The code
Code: Select all
; Get ADDR1 from list and jump to it
ld hl,LIST
ld a,(hl)
ld c,a
inc hl
ld a,(hl)
ld b,a
push bc
pop hl
jp (hl)
RETADDR:
RET
LIST:
dw ADDR1
dw ADDR2
ADDR1:
ld bc,&DEAD
jp RETADDR
ADDR2:
ld bc,&BEEF
jp RETADDR