zilog z80 cpu assembler syntax microprocessor and interface ii eko henfri b

39
Zilog Z80 CPU Assembler Syntax Microprocessor and Interface II Eko Henfri B

Upload: alannah-fields

Post on 31-Dec-2015

235 views

Category:

Documents


1 download

TRANSCRIPT

Zilog Z80 CPU Assembler Syntax

Microprocessor and Interface II

Eko Henfri B

Mnemonic - 1ADC ADD WITH CARRY

ADD ADD

AND LOGICAL AND

BIT BIT TEST

CALL CALL SUB ROUTINE

CCF COMPLEMENT CARRY FLAG

CP COMPARE

CPD COMPARE AND DECREMENT

CPDR COMPARE DECREMENT AND REPEAT

CPI COMPARE AND INCREMENT

CPIR COMPARE INCREMENT AND REPEAT

CPL COMPLEMENT ACCUMULATOR

DAA DECIMAL ADJUST ACCUMULATOR

DEC DECREMENT

DI DISABLE INTERRUPTS

Mnemonic - 2DJNZ DEC JUMP NON-ZERO

EI ENABLE INTERRUPTS

EX EXCHANGE REGISTER PAIR

EXX EXCHANGE ALTERNATE REGISTERS

HALT HALT, WAIT FOR INTERRUPT OR RESET

IM INTERRUPT MODE 0 1 2

IN INPUT FROM PORT

INC INCREMENT

IND INPUT, DEC HL, DEC B

INDR INPUT, DEC HL, DEC B, REPEAT IF B>0

INI INPUT, INC HL, DEC B

INIR INPUT, INC HL, DEC B, REPEAT IF B>0

JP JUMP

JR JUMP RELATIVE

LDLOAD DATA TO/FROM REGISTERS/MEMORY

Mnemonic - 3LDD LOAD DECREMENT

LDDR LOAD DECREMENT AND REPEAT

LDI LOAD AND INCREMENT

LDIR LOAD INCREMENT AND REPEAT

NEGNEGATE ACCUMULATOR 2'S COMPLEMENT

NOP NO OPERATION

OR --

OTDR OUTPUT, DEC HL, DEC B, REPEAT IF B>0

OTIR OUTPUT, INC HL, DEC B, REPEAT IF B>0

OUT OUTPUT TO PORT

OUTD OUTPUT, DEC HL, DEC B

OUTI OUTPUT, INC HL, DEC B

POP POP FROM STACK

PUSH PUSH INTO STACK

RES RESET BIT

Mnemonic - 4RET RETURN FROM SUB ROUTINE

RETI RETURN FROM INTERRUPT

RETNRETURN FROM NON MASKABEL INTERRUPT

RL ROTATE LEFT register

RLA ROTATE LEFT ACUMULATOR

RLC ROTATE LEFT THROUGH CARRY register

RLCAROTATE LEFT THROUGH CARRY ACCUMULATUR

RLD ROTATE LEFT DIGIT

RR ROTATE RIGHT register

RRA ROTATE RIGHT ACCUMULATOR

RRC ROTATE RIGHT CIRCULAR register

RRCAROTATE RIGHT CIRCULAR ACCUMULATOR

RRD ROTATE RIGHT DIGIT

RST RESTART

SBC SUBTRACT WITH CARRY

Mnemonic - 5

SCF SET CARRY FLAG

SET SET BIT

SLA SHIFT LEFT ARITHMETIC register

SRA SHIFT RIGHT ARITHMETIC register

SRL SHIFT RIGHT LOGICAL register

SUB SUBTRACTION

XOR EXCLUSIVE OR

Instruction Summary - 1

Mnemonic SZHPNC Description Notes

ADC A,s ***V0* Add with Carry A=A+s+CY

ADC HL,ss **?V0* Add with Carry HL=HL+ss+CY

ADD A,s ***V0* Add A=A+s

ADD HL,ss --?-0* Add HL=HL+ss

ADD IX,pp --?-0* Add IX=IX+pp

ADD IY,rr --?-0* Add IY=IY+rr

AND s ***P00 Logical AND A=A&s

BIT b,m ?*1?0- Test Bit m&{2^b}

CALL cc,nn ------ Conditional Call If cc CALL

CALL nn ------ Unconditional Call -[SP]=PC,PC=nn

CCF --?-0* Complement Carry Flag CY=~CY

CP s ***V1* Compare A-s

CPD ****1- Compare and Decrement A-[HL],HL=HL-1,BC=BC-1

CPDR ****1- Compare, Dec., Repeat CPD till A=[HL]or BC=0

CPI ****1- Compare and Increment A-[HL],HL=HL+1,BC=BC-1

CPIR ****1- Compare, Inc., Repeat CPI till A=[HL]or BC=0

CPL --1-1- Complement A=~A

Instruction Summary - 2

Mnemonic SZHPNC Description Notes

DAA ***P-* Decimal Adjust Acc. A=BCD format

DEC s ***V1- Decrement s=s-1

DEC xx ------ Decrement xx=xx-1

DEC ss ------ Decrement ss=ss-1

DI ------ Disable Interrupts

DJNZ e ------ Dec., Jump Non-Zero B=B-1 till B=0

EI ------ Enable Interrupts

EX [SP],HL ------ Exchange [SP]<->HL

EX [SP],xx ------ Exchange [SP]<->xx

EX AF,AF' ------ Exchange AF<->AF'

EX DE,HL ------ Exchange DE<->HL

EXX ------ Exchange qq<->qq' (except AF)

HALT ------ Halt

IM n ------ Interrupt Mode (n=0,1,2)

IN A,[n] ------ Input A=[n]

IN r,[C] ***P0- Input r=[C]

INC r ***V0- Increment r=r+1

Instruction Summary - 3

Mnemonic SZHPNC Description Notes

INC [HL] ***V0- Increment [HL]=[HL]+1

INC xx ------ Increment xx=xx+1

INC [xx+d] ***V0- Increment [xx+d]=[xx+d]+1

INC ss ------ Increment ss=ss+1

IND ?*??1- Input and Decrement [HL]=[C],HL=HL-1,B=B-1

INDR ?1??1- Input, Dec., Repeat IND till B=0

INI ?*??1- Input and Increment [HL]=[C],HL=HL+1,B=B-1

INIR ?1??1- Input, Inc., Repeat INI till B=0

JP [HL] ------ Unconditional Jump PC=[HL]

JP [xx] ------ Unconditional Jump PC=[xx]

JP nn ------ Unconditional Jump PC=nn

JP cc,nn ------ Conditional Jump If cc JP

JR e ------ Unconditional Jump PC=PC+e

JR cc,e ------ Conditional Jump If cc JR(cc=C,NC,NZ,Z)

LD dst,src ------ Load dst=src

LD A,i **0*0- Load A=i (i=I,R)

LDD --0*0- Load and Decrement [DE]=[HL],HL=HL-1,#

Instruction Summary - 4

Mnemonic SZHPNC Description Notes

LDDR --000- Load, Dec., Repeat LDD till BC=0

LDI --0*0- Load and Increment [DE]=[HL],HL=HL+1,#

LDIR --000- Load, Inc., Repeat LDI till BC=0

NEG ***V1* Negate A=-A

NOP ------ No Operation

OR s ***P00 Logical inclusive OR A=Avs

OTDR ?1??1- Output, Dec., Repeat OUTD till B=0

OTIR ?1??1- Output, Inc., Repeat OUTI till B=0

OUT [C],r ------ Output [C]=r

OUT [n],A ------ Output [n]=A

OUTD ?*??1- Output and Decrement [C]=[HL],HL=HL-1,B=B-1

OUTI ?*??1- Output and Increment [C]=[HL],HL=HL+1,B=B-1

POP xx ------ Pop xx=[SP]+

POP qq ------ Pop qq=[SP]+

PUSH xx ------ Push -[SP]=xx

PUSH qq ------ Push -[SP]=qq

RES b,m ------ Reset bit m=m&{~2^b}

Instruction Summary - 5

Mnemonic SZHPNC Description Notes

RET ------ Return PC=[SP]+

RET cc ------ Conditional Return If cc RET

RETI ------ Return from Interrupt PC=[SP]+

RETN ------ Return from NMI PC=[SP]+

RL m **0P0* Rotate Left m={CY,m}<-

RLA --0-0* Rotate Left Acc. A={CY,A}<-

RLC m **0P0* Rotate Left Circular m=m<-

RLCA --0-0* Rotate Left Circular A=A<-

RLD **0P0- Rotate Left 4 bits {A,[HL]}={A,[HL]}<- ##

RR m **0P0* Rotate Right m=->{CY,m}

RRA --0-0* Rotate Right Acc. A=->{CY,A}

RRC m **0P0* Rotate Right Circular m=->m

RRCA --0-0* Rotate Right Circular A=->A

RRD **0P0- Rotate Right 4 bits {A,[HL]}=->{A,[HL]} ##

RST p ------ Restart (p=0H,8H,10H,...,38H)

SBC A,s ***V1* Subtract with Carry A=A-s-CY

SBC HL,ss **?V1* Subtract with Carry HL=HL-ss-CY

Instruction Summary - 6

Mnemonic SZHPNC Description Notes

SCF -1 Set Carry Flag CY=1

SET b,m ------ Set bit m=mv{2^b}

SLA m **0P0* Shift Left Arithmetic m=m*2

SRA m **0P0* Shift Right Arith. m=m/2

SRL m **0P0* Shift Right Logical m=->{0,m,CY}

SUB s ***V1* Subtract A=A-s

XOR s ***P00 Logical Exclusive OR A=Axs

Note (Flag Bits)

F

-*01?

Flag unaffectedAffectedResetSetunknown

S S Sign flag (Bit 7)

Z Z Zero flag (Bit 6)

HC H Half Carry flag (Bit 4)

P/V P Parity/Overflow flag (Bit 2,

V=overflow)

N N Add/Subtract flag (Bit 1)

CY CCarry flag (Bit 0)

Note (Data Adressing)

n Immediate addressing

nn Immediate extended addressing

e Relative addressing (PC=PC+2+offset)

[nn] Extended addressing

[xx+d] Indexed addressing

r Register addressing

[rr] Register indirect addressing

Implied addressing

b Bit addressing

p Modified page zero addressing (see RST)

Note (Data Definition)

DEFB n(,...) Define Byte(s)

DEFB 'str'(,...) Define Byte ASCII string(s)

DEFS nn Define Storage Block

DEFW nn(,...) Define Word(s)

Note (Registers)

A B C D E Registers (8-bit)

AF BC DE HL Register pairs (16-bit)

F Flag register (8-bit)

I Interrupt page address register (8-bit)

IX IY Index registers (16-bit)

PC Program Counter register (16-bit)

R Memory Refresh register

SP Stack Pointer register (16-bit)

Note (Data and Registers) b One bit (0 to 7)

cc Condition (C,M,NC,NZ,P,PE,PO,Z)

d One-byte expression (-128 to +127)

dst Destination s, ss, [BC], [DE], [HL], [nn]

e One-byte expression (-126 to +129)

m Any register r, [HL] or [xx+d]

n One-byte expression (0 to 255)

nn Two-byte expression (0 to 65535)

pp Register pair BC, DE, IX or SP

qq Register pair AF, BC, DE or HL

qq' Alternative register pair AF, BC, DE or HL

r Register A, B, C, D, E, H or L

rr Register pair BC, DE, IY or SP

s Any register r, value n, [HL] or [xx+d]

src Source s, ss, [BC], [DE], [HL], nn, [nn]

ss Register pair BC, DE, HL or SP

xx Index register IX or IY

Additional Note

+ - * / ^ Add/subtract/multiply/divide/exponent

& ~ v x Logical AND/NOT/inclusive OR/exclusive OR

<- -> Rotate left/right

[ ] Indirect addressing

[ ]+ -[ ] Indirect addressing auto-increment/decrement

{ } Combination of operands

# Also BC=BC-1,DE=DE-1

## Only lower 4 bits of accumulator A used

LD InstructionThe first thing you will want to do is to load a value into a register.This is done with the LD instruction.  Here are some examples:

ld a,0    ; loads the value0 into register ald b,2    ; loads the value 2 into register bld de,257 ; loads the value 257 into register de          ;(same as loading 1 into d and 1 into e)ld d,$0A  ; NOTE $8A represents a HEX number,          ; %00100100 represents a BIN number,          ; 52 just a decimal number.          ; this loads $0A into d, $0A is the same as 10ld a,d    ; loads the current value of d into a (in this

case 10) NOTE:An 8-bit regiser can only hold the values from 0-255 (%00000000-%11111111),but a 16 bit register can hold the values 0-65535.

The register HL, is primarily used for ADDRESSING, This means it usually points to another memory location.  The video Memory is located at $FC00, so to have hl "point" to the video memory you use the command:

ld hl,$FC00 ;loads the value $FC00 into register hl

Now, to copy a value into the memory location that HL is pointing to, we do something called indirect addressing. 

ld a,%10101010   ;loads thevalue %10101010 into reg. ald (hl),a        ;loads the value %10101010 into the                 ;memory location that hl "points" to                 ;the value of HL is $fc00 therefore                 ;the value %10101010 is loaded into                 ;memory location $fc00, which happens                 ;to be the video memory :)                 ;IT DOES NOT CHANGE THE VALUE OF HL!!ld a,(hl)        ;similiarly this loads the value at                 ;mem location $fc00 into the reg. a

ADD/SUB

The next thing to learn, is how to add and subtract from a register.  To do this we use the instructions ADD and SUB.

add a,##add hl,ss  (where ss = bc,de,hl, or sp)add ix,pp (where pp = bc,de,ix, or sp)add iy,rr  (where rr = bc,de,iy, or sp)

These are the only ways that ADD can be used.  ex:

ld a,8      ;a=8add a,10    ;a=a+10  a=18ld hl,$FC00 ;hl = $FC00ld bc,$00BB ;bc = $00BBadd hl,FCBB ;hl=hl+bc  hl = $fcbb

ADD

In order to add anything to the other registers, you must do it indIrectly:]

ld b,8      ;b=8ld a,b      ;a=badd a,5     ;a='b+5'ld b,a      ;b='b+5';orld bc,46    ;bc=46ld h,b      ;you can't do'ld hl,bc'ld l,c      ;ld bc,52    ;add hl,bc   ;hl = bc+52ld b,h      ;.ld c,l      ;bc =bc+52

SUBNow you know how to add, what about subtracting?

sub ##  ; a=a-##

That's it!  you can only SUB from a (the accumulator), therefore all other subtractions must be made indirectly.  Here are some examples:

ld a,16    ;a=16sub 5      ;a=a-5, a=11ld b,65    ;b=65ld a,b     ;a=65sub 6      ;a=65-6, a=59ld b,a     ;b=59

ADD and SUB let you add or subtract any number, however if you only want to add or subtract the value 1 then you can use INC/DEC

INC/DECinc r   ;(where r=a,b,c,d,e,h,orl)inc ss  ;(where ss=bc,de,hl, or sp)dec rdec ss

These are the ony cases we will use.  Here are some examples:

ld a,5    ;a=5inc a     ;a=a+1, a=6ld b,a    ;b=a, b=6dec b     ;b=b-1, b=5ld bc,568 ;bc=568inc bc    ;bc=bc+1, bc=569inc bc    ;bc=bc+1, bc=570

PUSH

To add an item to the stack we use the PUSH instruction.  You can only PUSH the following registers:

AF, BC, DE, HL, IX, IY

Here are some examples:push af    ;adds the valuestored in AF to the stackpush bc    ;adds the value storedin BC to the stack  

NOTE: The register still contains the value it had before being PUSHEDIt is very important that you DON'T exit the program before POPPING, all PUSHED values. 

POP

To remove anitem from the stack we use the POP instruction.  You can only POP to the following registers:

AF, BC, DE, HL

Here are some examples:

pop af    ;stores the value on the top of the          ;stack in AF  (removes that value from the

stack)pop bc    ;stores the value on the top of the          ;stack in BC (removes that value from the stack)

LABELS

Labels area way to define a certain area in the program.  To define labels, you must first enter the name of the label then a ":".  Here are some examples:

loop:         ; this is a label  ld a,5  push af     ;just somestuff  push bc  inc aanotherlabel: ; this is another label  

NOTE: Labels cannotbe indented!

JR The ASM code is always processed sequently, one instruction after the next.  However, if you want to skip to a certain part of the code, or go back and repeat a previous portion you must jump there.  There are 2 different ways to JUMP to another part of code. JR, and JP.  There are a few differences that are discussed later, but they are VERY IMPORTANT!

To jump to a certain part of code you must enter JR LABELNAME.  Here are some examples:

  ld a,1      ;a=1MyLabel:      ;LABEL  inc a       ;a=a+1  jr MyLabel  ;jump to LABEL

NOTE:This code willcause an infinite loop.  The code between 'MyLabel' and 'jr MyLabel‘ will be repeated over and over.

JPThe ASM code is always processed sequently, one instruction after the next.  However, if you want to skip to a certain part of the code, or back to a previous portion you must jump there. There are 2 different ways to JUMP to another part of code.  JR, and JP. 

There are a few differences that are discussed later, but they're VERY IMPORTANT!

 To jump to a certain part of code you must enter JP LABELNAME. Here are some examples:

  ld a,1     ;a=1MyLabel:      ;LABEL  inc a      ;a=a+1  jp MyLabel  ;jump to LABEL

In order to make jumps ONLY IF certain requirements are met, we use the following conditionals:

C     (Carry)NC   (No Carry)Z      (Zero)NZ  (Not Zero)M     (Minus)P      (Positive)PE    (Parity Even)PO     (Parity Odd)

You can already see some differences between JR and JP.  JP allows the use of 4 more conditions! These conditions are based on how the F register is set.  Remember it is the FLAG register. Here're some examples:

ld a,5     ;a=5Loop:        ;MyLOOP Label  dec a      ;a=a-1  jp p,Loop  ;if A is positive then jumpto Loop.            ;(When the value of A changes F is ALWAYS updated)

This loop will execute 6 times (a=5,4,3,2,1,0.

DIFFERENCES BETWEEN JR/JP

The Main difference between the 2 types of jumps (jr, jp) is the JR is a RELETIVE jump. 

When translated into machine code it essential says 'jump this many bytes forward/backward', for this reason, it has a limited range. If you plan on jumping further then the range allowed by JR you  MUST use JP. 

JP is an ABSOLUTE jump.  When translated into machine code it basicallysays 'jump to this location'.  The advantage of JR over JP is bothsize and speed in the final compiled program.

Another difference, which we have already seenis that JP allows you to use more conditions.  JR does not support P, M, PO, or PE.

DJNZ DJNZ is a very helpful instruction.  It combines the [DEC -> JP nz, label] sequence used so often in loops.  ld b,15  ;b=15, the number of times for the loop to executeLoop:  ; all the cool loop stuff inside of here  djnz Loop    ;b=b-1, jump is b is NotZero 

NOTE: DJNZ always uses the register B.  It decrements B,  checks if B is nz, if so then jumps to the label. If the value of B is changed inside your loop code remember to use PUSH/POP so your values is not destroyed.

ld a,0  ld b,15Loop:  push bc  ;adds it to the stack  ld b,4   ;b=4  add a,b  ;a=a+b, a=a+4  pop bc   ;gets our value of Bback!  djnz Loop

CP

CP simply sets the FLAG register (F) based on the subtraction (A-s)

CP S  (where S = A,F,B,C,D,E,H,L,#,(hl),(iy+#),or (ix+#))

IF... THEN... ELSE To achieve an IF... THEN... ELSE statement(s) you must use a CP call followed by a JR, JP, or any other instruction that has conditionals (ret, call, ...) Here are some examples:

  ld b,5       ;b=5  ld a,5       ;a=5   cp b         ;set flags based on (a-b)  jr z,AequalsB ;if a=b -> a-b=0,therefore               ;if the Zero flag is set JUMP to AequalsB  :  .AequalsB: ;  :  .

You can also string them together like this:

 ld b,6  ;.  ld c,5  ;.  ld a,5  ;Set the init values   cp b    ;IF a=b  jr z,AB ;THEN goto AB  cp c    ;IF a=c  jr z,AC ;THEN goto AC  jr NONE ;ELSE goto NONE (if it didn'tjump one of the other         ; times then it must be an ELSE, NOTE:no conditia         ; listed.  Just a straight 'jr NONE'):.AB::.AC::.NONE:

GREATER THAN, and LESS THAN:

Compare A and B for =, < and >LD B, 7LD A, 5CP B ; Flags = status(A-B)JP Z, A_Equal_To_B ; IF(a-b)=0 THEN a=bJP NC, A_Greater_Than_B ; IF(a-b)>0 THEN a>bJP C, A_Lower_Than_B ; IF(a-b)<0 THEN a<b

A_Greater_Than_B: (...)JP end_compare

A_Lower_Than_B: (...)JP end_compare

A_Equal_To_B: (...)end_compare:

RET

CALL

Call allows you to jump to a different location in the ASM program, while saving where it was, so it can return to that location later.

CALL cc,LABEL   (where cc=c,nc,z,nz,p,m,pe,orpo)

 If the condition cc is true then it jumps to the label. CC is OPTIONAL.

call _clrLCD   ; calls the ROM function to clear the LCDcall z,_clrLCD ; only calls the function if the Zeroflag is set

 RET RET, returns power back to where the last CALL statement was made, or back the TI/OS-SHELL, if no CALLs where made.  It's actually similar to the stack, but it's easier to explain it this way.

RET cc  (where cc=c,nc,z,nz,p,m,pe,or po)

 If cc is true then the ret is executed.  CC is OPTIONAL

FUNCTIONSPutting the CALL and RET commands together you are given a powerful way to modulate your programs.  You can create functions that perform certain tasks and even accept input and produce output.  ld a,5

call Add3  ; calls our function Add3which adds 3 to the reg. A           ; and stores the new value in B           ; NOW b=a+3, or 8  ld c,b   ; c=b, c=8  :  .Add3:  ld b,a     ;b=a  inc b      ;  inc b      ;  inc b      ;b=a+3  ret        ;returns control to next line after CALL was made.