Difference between revisions of "Z80 Instruction Set"
Line 45: | Line 45: | ||
===POP=== | ===POP=== | ||
Same syntax as PUSH. Copies (SP) to regLSB, increments SP, copies (SP) to regMSB, then increments SP again. The word based at the starting value of SP is zeroed. | Same syntax as PUSH. Copies (SP) to regLSB, increments SP, copies (SP) to regMSB, then increments SP again. The word based at the starting value of SP is zeroed. | ||
+ | |||
+ | |||
+ | ==Control== | ||
+ | ===JP=== | ||
+ | ''JP imm16'' Imm16 is copied to PC, causing execution to jump there. Can also be conditional with Z, NZ, C, NC, PO, PE, P, or M as additional arguments such as ''JP Z, imm16''. (HL) may be substituted for imm16 to have the current value of HL copied to PC | ||
+ | |||
+ | |||
+ | ===JR=== | ||
+ | ''JR imm8'' The signed 8 bit value imm8 is added to PC, allowing a jump within 128 bytes of the instruction. May also be conditional, but only with Z, NZ, C, and NC arguments. | ||
+ | |||
+ | |||
+ | ===CALL=== | ||
+ | ''CALL imm16'' The current value of PC+3 is PUSHed, then imm16 is loaded into PC. May be conditional with the same syntax and arguments as JP, although (HL) is not valid. | ||
+ | |||
+ | |||
+ | ===RET=== | ||
+ | ''RET'' The top stack value is POPed into PC. It may be conditional with the same arguments and syntax as JP. This is usually used to return from a CALL instruction. | ||
+ | |||
+ | |||
+ | ===RST=== | ||
+ | ''RST imm8'' PC+3 is PUSHed and PC is loaded with 00imm8. This opcode is only 1 byte, so it is an alternative to CALLing routines that are mapped into the beginning of memory. However, it may only be used for the following values of imm8: 00h, 08h, 18h, 20h, 28h, 30h, and 38h. Imm8 must also be in -h suffix format. | ||
+ | |||
+ | |||
+ | ===DJNZ=== | ||
+ | ''DJNZ imm8'' B is decremented and if is 0, the rest of the instruction is skipped. Otherwise, the signed 8 bit value imm8 is added to PC. This instruction is usually used like a [[For(]] loop in BASIC. | ||
+ | |||
+ | |||
+ | ===NOP=== | ||
+ | ''NOP'' As the name implies, it is "no operation". The CPU does nothing for four clock cycles. |
Revision as of 19:35, 22 February 2006
Not a complete list, but most commonly used instructions are given. For a complete list with opcodes and number of clock cycles taken, go here
Contents
Data Movement
EX
Three possible arguments:
EX DE, HL Swaps H with D and L with E
EX AF, AF' Swaps AF with its shadow
EX (SP), HL Swaps (SP) with L and (SP+1) with H. Index registers are also valid
EXX
Swap BC, DE and HL with their shadows
LD
Four main arguments:
LD reg, # Sets the eight or 16 bit contents of reg to #
LD reg2, reg1 Copies the contents of 8 bit reg1 to reg2
LD (imm16), reg Copies the value of 8 or 16 bit reg to 16 bit memory address imm16. Programmers should remember that 16 bit values are stored little-endian.
LD reg, (imm16) Does the opposite of LD (imm16), reg
LDIR
Copies a byte from (HL) to (DE), increments DE and HL, then decrements BC. The instruction is repeated if BC is not 0. Interrupts are allowed to trigger while this instruction is running.
PUSH
Used as PUSH reg16 where reg16 is a register pair or index register. Decrements SP, copies regMSB to (SP), decrements SP again, then copies regLSB to (SP).
POP
Same syntax as PUSH. Copies (SP) to regLSB, increments SP, copies (SP) to regMSB, then increments SP again. The word based at the starting value of SP is zeroed.
Control
JP
JP imm16 Imm16 is copied to PC, causing execution to jump there. Can also be conditional with Z, NZ, C, NC, PO, PE, P, or M as additional arguments such as JP Z, imm16. (HL) may be substituted for imm16 to have the current value of HL copied to PC
JR
JR imm8 The signed 8 bit value imm8 is added to PC, allowing a jump within 128 bytes of the instruction. May also be conditional, but only with Z, NZ, C, and NC arguments.
CALL
CALL imm16 The current value of PC+3 is PUSHed, then imm16 is loaded into PC. May be conditional with the same syntax and arguments as JP, although (HL) is not valid.
RET
RET The top stack value is POPed into PC. It may be conditional with the same arguments and syntax as JP. This is usually used to return from a CALL instruction.
RST
RST imm8 PC+3 is PUSHed and PC is loaded with 00imm8. This opcode is only 1 byte, so it is an alternative to CALLing routines that are mapped into the beginning of memory. However, it may only be used for the following values of imm8: 00h, 08h, 18h, 20h, 28h, 30h, and 38h. Imm8 must also be in -h suffix format.
DJNZ
DJNZ imm8 B is decremented and if is 0, the rest of the instruction is skipped. Otherwise, the signed 8 bit value imm8 is added to PC. This instruction is usually used like a For( loop in BASIC.
NOP
NOP As the name implies, it is "no operation". The CPU does nothing for four clock cycles.