Page 1 of 1

jp (hl) explanation

Posted: Thu Mar 07, 2024 10:14 am
by Prototron
So I've been having a headache with this instruction:

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

Re: jp (hl) explanation

Posted: Fri Mar 08, 2024 11:42 am
by akuyou
Yes, JP (HL) Jumps to the address in HL
You would need to load the address into the HL pair first then jump to it.

Re: jp (hl) explanation

Posted: Sat Mar 09, 2024 6:49 am
by Prototron
Great, thanks for the clarification.