jp (hl) explanation

Posts about Z80 programming that do not relate to a particular computer
Post Reply
Prototron
Posts: 4
Joined: Tue Mar 05, 2024 8:43 am

jp (hl) explanation

Post by Prototron » Thu Mar 07, 2024 10:14 am

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

User avatar
akuyou
Posts: 564
Joined: Mon Apr 22, 2019 3:19 am
Contact:

Re: jp (hl) explanation

Post by akuyou » Fri Mar 08, 2024 11:42 am

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.
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

Prototron
Posts: 4
Joined: Tue Mar 05, 2024 8:43 am

Re: jp (hl) explanation

Post by Prototron » Sat Mar 09, 2024 6:49 am

Great, thanks for the clarification.

Post Reply

Return to “General Z80 Programming”