This lessons and the next one will handle some very usefull instructions that have not been mentioned in the previous lessons.
EX and EXX |
CPL and NEG |
SCF and CCF |
NOP and HALT |
DI and EI
EX and EXX
Remember all those shadow registers? If you don't, take a look at
lesson 2 again, because we will need them here.
Until now, we haven't used these registers. You can't access them directly anyway.
So what can you do with them then?
Well, the only thing that you can do with them is EXchanging their contents with the contents of the normal registers.
There are 2 instructions to do this:
EXX: Exchanges BC with BC', DE with DE' and HL with HL'
EX AF,AF': Exchanges AF with AF'
Please note that you should only use EXX when interrupts are disabled (DI, see lesson 20).
There's also one other handy EX-instruction which exchanges the contents of the HL and the DE register.
EX DE,HL: Exchanges DE with HL (note: don't write ex hl,de !!!)
Next to that, there are 3 other EX-instructions which are for more advanced use:
EX (SP),HL
EX (SP),IX
EX (SP),IY
An example of where one of the previous 3 instructions is used: when you do a ROM_CALL, the line ROM_CALL(addr) is substituted by call romcallfunction \ .dw Addr
The romcallfunction is a function that is inside of the Ash or CrASH shell. Now, when that function will return, you would normally get an error (because the processor wants to execute the bytes of Addr).
To make sure the program counter will point to the byte AFTER the addr word, the romcallfunction will use the EX (SP),HL instruction.
CPL and NEG
This are 2 instruction that perform an operation on the accumulator.
NEG makes the accumulator negative (using two's complement, see a later lesson for more info about this)
and
CPL gets the ones complement of the accumulator, which means that all the bits are complemented.
The result of NEG is not(A)+1 and the result of CPL is not(A)
Example:
LD A,$14 ; A = %00010100
CPL ; A = %11101011
NEG ; A = %00010101
|
SCF and CCF
This are 2 instruction that affect the carry flag only.
SCF sets it while
CCF complements it, which means that if the carry flag was 1, it will become 0 and vice versa.
An example with the state of the carry flag after the operation as comment (1=set, 0=reset):
SCF ; 1
CCF ; 0
CCF ; 1
SCF ; 1
|
NOP and HALT
These are 2 instructions that don't do anything at all.
NOP stands for
No
OPeration and
HALT will halt the processor until the next interrupt comes around.
Both instructions can be used to create delays.
Suppose that you wrote a game that runs too fast. Then you could make a loop like this:
Delay:
LD B,10
Delay_Loop:
HALT
DJNZ Delay_Loop
ret
|
This example routine will execute 10 halt instructions and will cause a delay of 173 T-states (not included the call Delay instruction which takes another 17 T-states)
DI and EI
These instructions will enable or disable interrupts.
EI will enable interrupts and
DI will disable them.
In a later lesson, we will teach you how you can set up your own interrupt routine.
Previous lesson |
Contents |
Next lesson