What do opcodes 0xE9 (JP HL) and 0xF8 (LD HL,SP+r8) do?

1.1k Views Asked by At

I think I am struggling to define correctly the following ambiguous opcodes: LD HL,SP+r8 and JP (HL) opcodes (0xE9 and 0xF8 respectively)

In my implementation, LD HL,SP+r8 sets HL to the value of SP+r8, but I have a feeling that it may be to do with loading memory from RAM. JP (HL), I have as PUSHing the PC onto the stack and setting the Program Counter to the value of HL (like JP a16, except with the value of HL), but I've read a few forums that seem to say that that's wrong.

Any clarification of what either of these instructions do would be great, as I'm pretty lost at the moment.

1

There are 1 best solutions below

0
On BEST ANSWER

In my implementation, LD HL,SP+r8 sets HL to the value of SP+r8, but I have a feeling that it may be to do with loading memory from RAM.

No. It just takes an 8-bit immediate, sign-extends it, adds the value of SP to it and stores the result in HL.

JP (HL), I have as PUSHing the PC onto the stack and setting the Program Counter to the value of HL (like JP a16, except with the value of HL)

JP doesn't push the current PC on the stack (maybe you're confusing it with CALL). What JP (HL) does is just PC = HL.