450 likes | 976 Views
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. Instruction Summary - 2. Instruction Summary - 3. Instruction Summary - 4. Instruction Summary - 5.
E N D
Zilog Z80 CPU Assembler Syntax Microprocessor and Interface II Eko Henfri B
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.
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=8 add 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' ;or ld 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
SUB Now 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=11 ld 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/DEC inc r ;(where r=a,b,c,d,e,h,orl) inc ss ;(where ss=bc,de,hl, or sp) dec r dec 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=5 ld 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.
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 ;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 stackld 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
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 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.