Zilog Z80 CPU Assembler Syntax - PowerPoint PPT Presentation

About This Presentation
Title:

Zilog Z80 CPU Assembler Syntax

Description:

Zilog Z80 CPU Assembler Syntax Microprocessor and Interface II Eko Henfri B Mnemonic - 1 Mnemonic - 2 Mnemonic - 3 Mnemonic - 4 Mnemonic - 5 Instruction Summary - 1 ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 40
Provided by: myCo159
Category:
Tags: cpu | assembler | syntax | z80 | zilog

less

Transcript and Presenter's Notes

Title: Zilog Z80 CPU Assembler Syntax


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

2
Mnemonic - 1
ADC 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
3
Mnemonic - 2
DJNZ 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 Bgt0
INI INPUT, INC HL, DEC B
INIR INPUT, INC HL, DEC B, REPEAT IF Bgt0
JP JUMP
JR JUMP RELATIVE
LD LOAD DATA TO/FROM REGISTERS/MEMORY
4
Mnemonic - 3
LDD LOAD DECREMENT
LDDR LOAD DECREMENT AND REPEAT
LDI LOAD AND INCREMENT
LDIR LOAD INCREMENT AND REPEAT
NEG NEGATE ACCUMULATOR 2'S COMPLEMENT
NOP NO OPERATION
OR --
OTDR OUTPUT, DEC HL, DEC B, REPEAT IF Bgt0
OTIR OUTPUT, INC HL, DEC B, REPEAT IF Bgt0
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
5
Mnemonic - 4
RET RETURN FROM SUB ROUTINE
RETI RETURN FROM INTERRUPT
RETN RETURN FROM NON MASKABEL INTERRUPT
RL ROTATE LEFT register
RLA ROTATE LEFT ACUMULATOR
RLC ROTATE LEFT THROUGH CARRY register
RLCA ROTATE LEFT THROUGH CARRY ACCUMULATUR
RLD ROTATE LEFT DIGIT
RR ROTATE RIGHT register
RRA ROTATE RIGHT ACCUMULATOR
RRC ROTATE RIGHT CIRCULAR register
RRCA ROTATE RIGHT CIRCULAR ACCUMULATOR
RRD ROTATE RIGHT DIGIT
RST RESTART
SBC SUBTRACT WITH CARRY
6
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
7
Instruction Summary - 1
Mnemonic SZHPNC Description Notes
ADC A,s V0 Add with Carry AAsCY
ADC HL,ss ?V0 Add with Carry HLHLssCY
ADD A,s V0 Add AAs
ADD HL,ss --?-0 Add HLHLss
ADD IX,pp --?-0 Add IXIXpp
ADD IY,rr --?-0 Add IYIYrr
AND s P00 Logical AND AAs
BIT b,m ?1?0- Test Bit m2b
CALL cc,nn ------ Conditional Call If cc CALL
CALL nn ------ Unconditional Call -SPPC,PCnn
CCF --?-0 Complement Carry Flag CYCY
CP s V1 Compare A-s
CPD 1- Compare and Decrement A-HL,HLHL-1,BCBC-1
CPDR 1- Compare, Dec., Repeat CPD till AHLor BC0
CPI 1- Compare and Increment A-HL,HLHL1,BCBC-1
CPIR 1- Compare, Inc., Repeat CPI till AHLor BC0
CPL --1-1- Complement AA
8
Instruction Summary - 2
Mnemonic SZHPNC Description Notes
DAA P- Decimal Adjust Acc. ABCD format
DEC s V1- Decrement ss-1
DEC xx ------ Decrement xxxx-1
DEC ss ------ Decrement ssss-1
DI ------ Disable Interrupts
DJNZ e ------ Dec., Jump Non-Zero BB-1 till B0
EI ------ Enable Interrupts
EX SP,HL ------ Exchange SPlt-gtHL
EX SP,xx ------ Exchange SPlt-gtxx
EX AF,AF' ------ Exchange AFlt-gtAF'
EX DE,HL ------ Exchange DElt-gtHL
EXX ------ Exchange qqlt-gtqq' (except AF)
HALT ------ Halt
IM n ------ Interrupt Mode (n0,1,2)
IN A,n ------ Input An
IN r,C P0- Input rC
INC r V0- Increment rr1
9
Instruction Summary - 3
Mnemonic SZHPNC Description Notes
INC HL V0- Increment HLHL1
INC xx ------ Increment xxxx1
INC xxd V0- Increment xxdxxd1
INC ss ------ Increment ssss1
IND ???1- Input and Decrement HLC,HLHL-1,BB-1
INDR ?1??1- Input, Dec., Repeat IND till B0
INI ???1- Input and Increment HLC,HLHL1,BB-1
INIR ?1??1- Input, Inc., Repeat INI till B0
JP HL ------ Unconditional Jump PCHL
JP xx ------ Unconditional Jump PCxx
JP nn ------ Unconditional Jump PCnn
JP cc,nn ------ Conditional Jump If cc JP
JR e ------ Unconditional Jump PCPCe
JR cc,e ------ Conditional Jump If cc JR(ccC,NC,NZ,Z)
LD dst,src ------ Load dstsrc
LD A,i 00- Load Ai (iI,R)
LDD --00- Load and Decrement DEHL,HLHL-1,
10
Instruction Summary - 4
Mnemonic SZHPNC Description Notes
LDDR --000- Load, Dec., Repeat LDD till BC0
LDI --00- Load and Increment DEHL,HLHL1,
LDIR --000- Load, Inc., Repeat LDI till BC0
NEG V1 Negate A-A
NOP ------ No Operation
OR s P00 Logical inclusive OR AAvs
OTDR ?1??1- Output, Dec., Repeat OUTD till B0
OTIR ?1??1- Output, Inc., Repeat OUTI till B0
OUT C,r ------ Output Cr
OUT n,A ------ Output nA
OUTD ???1- Output and Decrement CHL,HLHL-1,BB-1
OUTI ???1- Output and Increment CHL,HLHL1,BB-1
POP xx ------ Pop xxSP
POP qq ------ Pop qqSP
PUSH xx ------ Push -SPxx
PUSH qq ------ Push -SPqq
RES b,m ------ Reset bit mm2b
11
Instruction Summary - 5
Mnemonic SZHPNC Description Notes
RET ------ Return PCSP
RET cc ------ Conditional Return If cc RET
RETI ------ Return from Interrupt PCSP
RETN ------ Return from NMI PCSP
RL m 0P0 Rotate Left mCY,mlt-
RLA --0-0 Rotate Left Acc. ACY,Alt-
RLC m 0P0 Rotate Left Circular mmlt-
RLCA --0-0 Rotate Left Circular AAlt-
RLD 0P0- Rotate Left 4 bits A,HLA,HLlt-
RR m 0P0 Rotate Right m-gtCY,m
RRA --0-0 Rotate Right Acc. A-gtCY,A
RRC m 0P0 Rotate Right Circular m-gtm
RRCA --0-0 Rotate Right Circular A-gtA
RRD 0P0- Rotate Right 4 bits A,HL-gtA,HL
RST p ------ Restart (p0H,8H,10H,...,38H)
SBC A,s V1 Subtract with Carry AA-s-CY
SBC HL,ss ?V1 Subtract with Carry HLHL-ss-CY
12
Instruction Summary - 6
Mnemonic SZHPNC Description Notes
SCF -1 Set Carry Flag CY1
SET b,m ------ Set bit mmv2b
SLA m 0P0 Shift Left Arithmetic mm2
SRA m 0P0 Shift Right Arith. mm/2
SRL m 0P0 Shift Right Logical m-gt0,m,CY
SUB s V1 Subtract AA-s
XOR s P00 Logical Exclusive OR AAxs
13
Note (Flag Bits)
F - 0 1 ? Flag unaffected Affected Reset Set unknown
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, Voverflow)
N N Add/Subtract flag (Bit 1)
CY C Carry flag (Bit 0)
14
Note (Data Adressing)
n Immediate addressing
nn Immediate extended addressing
e Relative addressing (PCPC2offset)
nn Extended addressing
xxd Indexed addressing
r Register addressing
rr Register indirect addressing
Implied addressing
b Bit addressing
p Modified page zero addressing (see RST)
15
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)
16
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)
17
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 xxd
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 xxd
src Source s, ss, BC, DE, HL, nn, nn
ss Register pair BC, DE, HL or SP
xx Index register IX or IY
18
Additional Note
- / Add/subtract/multiply/divide/exponent
v x Logical AND/NOT/inclusive OR/exclusive OR
lt- -gt Rotate left/right
Indirect addressing
- Indirect addressing auto-increment/decrement
Combination of operands
Also BCBC-1,DEDE-1
Only lower 4 bits of accumulator A used
19
LD Instruction
The 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.
20
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
21
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      a8 add a,10    aa10  a18ld
hl,FC00 hl FC00ld bc,00BB bc 00BBadd
hl,FCBB hlhlbc  hl fcbb
22
ADD
In order to add anything to the other registers,
you must do it indIrectly ld b,8      b8ld
a,b      abadd a,5     a'b5'ld b,a     
b'b5' or ld bc,46    bc46ld h,b      you
can't do'ld hl,bc'ld l,c      ld bc,52   
add hl,bc   hl bc52ld b,h      .ld
c,l      bc bc52
23
SUB
Now you know how to add, what about
subtracting? sub   aa- 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    a16sub
5      aa-5, a11 ld b,65    b65ld a,b    
a65sub 6      a65-6, a59ld b,a    
b59 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
24
INC/DEC
inc r   (where ra,b,c,d,e,h,orl) inc ss 
(where ssbc,de,hl, or sp) dec r dec ss These
are the ony cases we will use.  Here are some
examples ld a,5    a5inc a     aa1,
a6ld b,a    ba, b6dec b     bb-1, b5 ld
bc,568 bc568inc bc    bcbc1, bc569inc
bc    bcbc1, bc570
25
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. 
26
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)
27
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!
28
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     
a1MyLabel      LABEL  inc a       aa1 
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.
29
JP
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 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     a1MyLabel     
LABEL  inc a      aa1  jp MyLabel  jump to
LABEL
30
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     a5Loop       
MyLOOP Label  dec a      aa-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
(a5,4,3,2,1,0.
31
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.
32
DJNZ
DJNZ is a very helpful instruction.  It combines
the DEC -gt JP nz, label sequence used so often
in loops.   ld b,15  b15, the number of times
for the loop to executeLoop  all the cool
loop stuff inside of here  djnz Loop    bb-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   b4  add a,b  aab, aa4 
pop bc   gets our value of Bback!  djnz Loop
33
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))
34
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       b5  ld a,5       a5   cp
b         set flags based on (a-b)  jr
z,AequalsB if ab -gt a-b0,therefore            
   if the Zero flag is set JUMP to
AequalsB    .AequalsB     .
35
You can also string them together like this
 ld b,6  .  ld c,5  .  ld a,5  Set the init
values   cp b    IF ab  jr z,AB THEN goto
AB  cp c    IF ac  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,
NOTEno conditia          listed.  Just a
straight 'jr NONE').AB.AC.NONE
36
GREATER THAN, and LESS THAN
Compare A and B for , lt and gtLD B, 7LD A, 5CP
B Flags status(A-B)JP Z,
A_Equal_To_B IF(a-b)0 THEN abJP NC,
A_Greater_Than_B IF(a-b)gt0 THEN agtbJP C,
A_Lower_Than_B IF(a-b)lt0 THEN
altb A_Greater_Than_B (...)JP
end_compare A_Lower_Than_B (...)JP
end_compare A_Equal_To_B (...)end_compare
RET
37
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 ccc,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
38
 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 ccc,nc,z,nz,
p,m,pe,or po)  If cc is true then the ret is
executed.  CC is OPTIONAL
39
FUNCTIONS
Putting 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 ba3, or 8  ld c,b    cb,
c8    . Add3  ld b,a     ba  inc b     
  inc b        inc b      ba3  ret       
returns control to next line after CALL was
made.
Write a Comment
User Comments (0)
About PowerShow.com