460 likes | 707 Views
Microprocessor. Dr. Rabie A. Ramadan Al-Azhar University Lecture 7. Z80 Assembly Programming. High-level language (C, C++, Pascal). compiler. Assembly language (Z80). assembler. Object code. linker. Machine language. Programming Phases. Z80 Instruction Set.
E N D
Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7
High-level language (C, C++, Pascal) compiler Assembly language (Z80) assembler Object code linker Machine language Programming Phases
Z80 Instruction Set • First, You are required to look for Z80 assembler to try some of the programs. • Z80 Includes all 8080 instructions • Contains 158 instructions • Instruction Opcode + Operands
Instruction Format • Z80 instruction ranges from one byte to four bytes • Opcode varies from 1 to 2 bytes • Operands varies from 1 to 2 bytes • Operands could be memory locations , registers, I/O addresses , or memory addresses
Z80 instruction Set • Instructions can be classified to: • 1-Byte instructions • 2-Byte instructions • 3-Byte instructions • 4-Byte instructions
Z80 Instruction Set • 1-Byte Instruction • The opcode and operands are included in the same byte • Ex. • LD A,B load B into A 01 111 000 • LD 01 , A 1111 , and B 000 • For microprocessor internal usage only
Z80 Instruction Set • 2-Byte Instructions • Opcode First Byte • Operand Second Byte • Ex. LD B, 32H 0000 0110 (6H) byte 1 0011 0010 (32H) byte 2 Load the “32” value into register B • LD B is represented by 6H and the second byte includes “32”
Z80 Instruction Set • 3-Byte Instruction • One byte Opcode and two bytes Operand • Ex. LD BC, 2080H • 0000 0001 byte 1 LD BC (01H) • 1000 0000 byte 2 80H • 0010 0000 byte 3 20H • Loads the value “2080H” into the two registers B and C • Note: the load starts by the low order byte followed by the high order 80 then 20
Z80 Instruction Set • 4-Byte Instructions • Not compatible with 8080 instruction set • 2 bytes Opcode and 2 bytes Operand • Ex. LD IX , 2000H • Loads the contents of memory address 2000H into IX register first 2 bytes • 0000 0000 (00H) byte 3 • 0010 0000 (20H) byte 4
Z80 Instruction Set • Instruction Categories • Data Copy transfer or load operations • Arithmetic Operations • Logic Operations • Bit Manipulation • Branch Operation • Machine Control Operations
Data Copy Instructions • From register to register • LD A, B load B into A • Specific 8 bits data into register • LD B, 32H load “32” into B • Specific 16 bits data into register • LD HL , 2080H loads “2080” into HL • From memory location into register • LD A , (2010H) load the content of memory location (2010) into A
Data Copy Instructions • From input port into accumulator • IN A, (01H) loads data from Input port (01H) into A • From the accumulator into the o/p port • OUT (07H) , A • Copy the contents of register into stack memory • PUSH BC pushes the BC contents into the stack • Exchange data between registers • EXX BC, DE
Arithmetic Instructions • Addition • Any thing is only added to the contents of the accumulator. • No two registers such as B and C can be added directly. • ADD A, B add B to the accumulator contents • ADD A, 97H add the value “97H” to the accumulator content
Arithmetic Instructions • Subtraction • A register or memory location can be subtracted from the accumulator • SUB C subtract the contents of register C from the accumulator • SUB 47H subtract the “47H” from the accumulator • Note : the accumulator in implied in the instruction
Arithmetic Instructions • Increment/Decrement • Add / Sub1 to/from the contents of any register or memory location • INC B • DEC BC
Arithmetic Instructions • 1’s and 2’s Complement • Do 1’s or 2’s complement on the contents of the accumulator • CPL one’s complement changes the 1 to 0 and vice versa • NEG 2’s complement (subtract the accumulator from 0 ) or (Add 1 to the 1’s complement )
Group Activity • Write a simple program to load the values “53H” and “F5H” into registers A and B respectively. Then add the two registers? • LD A, 53H • LD B, F5H • ADD A, B
Logic Operations • Logic Functions • AND , OR , XOR with the accumulator contents • AND B • Shift and Rotate • RLC B rotate left the contents of B • Compare • Compare the contents of a register with the contents of a register or memory location O/p will be shown on the flag register • CP B
Bit Manipulation • Bit Test • Verify the value of a bit (0 or 1) • Z flag is the indicator • BIT 7, B check bit 7 in register B • Bit Set/Reset • SET 5, B • RES 2, B
Branching Operations • Jump • Change the program sequence • JP C, 2050H if C flag is set , jump to 2050H • Call/Return • Change the program sequence by calling or returning from a sub routine • CALL 2050H call subroutine located at 2050H • Restart • Memory are divided into pages • Page number 00 marked with 8 restart locations • RST 28H restart from the location 28H
Machine Control Operations • Control the Z80 operations • HALT Suspend execution of an instruction
How to write a program ? • Phases • Problem Statement • Analysis • Flowchart • Write the Assembly • Execute
Write the Assembly • Will try to get our hands dirty in the lab
Addressing Modes • A way of specifying the operand or pointing to a data location • Immediate • Immediate extended • Register • Implied • Register indirect • Extended • Relative • Indexed • Bit • Page Zero
Implied Memory Addressing • Registers H and L hold the address of the memory location being accessed • LD C, (HL) loads C register with the contents of the memory location pointed by HL registers
Addressing Modes • Immediate • A byte following the opcode is the operand • LD B, 97H 97h id the value • ImmediateExtended • Two bytes following the opcode are the operands • LD BC , 3040H 3040H are the 2 bytes value • Register • Operand is a register • LD A, B B is a register
Addressing Modes • Implied • The opcode imply one of the operands • AND B AND implies that the operation is done on the accumulator contents • RegisterIndirect • Register holds the memory location address • LD B, (HL) the memory location is stored in H and L registers • Extended • The two bytes following the opcode specify the jump location • JP 208H
Addressing Modes • Relative • The oprand indicates the placement of the next instruction to be executed relative to the current one • JR 14H from the next instruction , jump 20 locations • Indexed • Use one of the index registers to define the next instruction location • INC (IX+10H) if IX contains 2080H , then the content of the memory location (2080 +10) will be incremented
Addressing Modes • Bit • Used with bit operations • SET 7, B • Page zero • Reset operation • RST 28H
Reading Materials • Chapters 6 and 7 • Please find one of the free Z80 assemblers and play with it
Load Instruction • LD rd , rs copy data from rsto rd • LD r, 8-bit immediate addressing mode , loaf 8 bit number into register r • LD B, 32H • LD rp , 16- bit immediate extended addressing mode , load 16-bits into register pair • LD HL, 1840H • LD rx, 16-bit immediate extended addressing mode , loads 16 bits into specified index register • LD IX, 2050H
Load instructions Example • Write a program to do the following: • Load 97h into the accumulator • Loads 2050H into HL register • Loads 2070H into IX register • Copy the contents of the accumulator into register C • Copy the contents of register H into register B • End the instructions by HALT • Write all of these instructions at the memory locations started at 2000H • Show the register contents by the end of this program ?
Data Copy Between Registers and Memory • Memory Address stored in 16 bits • LD r, (HL) Indirect Addressing mode , loads the contents of a memory location whose address is stored in HL register pair • LD , B, (HL) • LD (HR), r Indirect Addressing mode , loads the contents of a register into a memory location whose address is stored in HL register pair • LD (HL) , C • LD (HL), 8-bit indirect and immediate , copy 8-bit into a memory location whose address is stored in HL register pair. • LD (HL), 97H
Data Copy Between Registers and Memory • LD A, (rp) indirect , copy the contents of a memory into A • LD (rp) , A indirect , copy the contents of A into a memory location • LD (BC), A • LD A, (16-bit) Extended , copy contents of memory into accumulator • LD (16-bit), A Extended , copy contents of the accumulator into memory • LD (2050H), A
Data Copy Between Registers and Memory Example • The memory location 2050H contains the data byte 37H , write instructions to copy a byte from the memory location into register B?
Answer • Method 1: • LD HL, 2050H • LD B, (HL) • Method 2: • LD DE, 2050H • LD A, (DE) • LD B, A • Method 3: • LD A, (2050H) • LD B,A
Data Copy Between Accumulator and I/O • IN A, (8-bit) read data from input port to the accumulator • OUT (8-bit) , A write data into the output port • See Example in Page 191
Group Activity • Write comments to explain the function of the following instructions • LD HL, 2065H • LD (HL), 00H • HALT
Group Activity • Specify the contents of the registers and memory locations if any after the execution of the following instructions: • A B C H L • 34 7F FF 01 00 • LD A, 00H • LD BC, 8058H • LD B, A • LD HL, 2040H • LD L,C • LD (HL), A • HALT
Useful Links are now available on the website • http://www.diylife.com/2008/02/15/program-a-pic-microcontroller/ • http://www.promeganet.com/?p=3 • http://www.promeganet.com/?page_id=233 • http://www.arabteam2000-forum.com/index.php?showtopic=76314