Microprocessor Dr Rabie A Ramadan AlAzhar University Lecture
Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7
Z 80 Assembly Programming 2
Programming Phases High-level language (C, C++, Pascal) compiler Assembly language (Z 80) assembler Object code linker Machine language 3
Z 80 Instruction Set l First, You are required to look for Z 80 assembler to try some of the programs. l Z 80 Includes all 8080 instructions l Contains 158 instructions l Instruction Opcode + Operands 4
Instruction Format l Z 80 instruction ranges from one byte to four bytes l Opcode varies from 1 to 2 bytes l Operands could be memory locations , registers, I/O addresses , or memory addresses 5
Z 80 instruction Set l Instructions can be classified to: • 1 -Byte instructions • 2 -Byte instructions • 3 -Byte instructions • 4 -Byte instructions 6
Z 80 Instruction Set l 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 7
Z 80 Instruction Set l 2 -Byte Instructions • Opcode First Byte • Operand Second Byte • Ex. LD B, 32 H 0000 0110 (6 H) byte 1 0011 0010 (32 H) byte 2 Load the “ 32” value into register B • LD B is represented by 6 H and the second byte includes “ 32” 8
Z 80 Instruction Set l 3 -Byte Instruction • One byte Opcode and two bytes Operand • Ex. LD BC, 2080 H • 0000 0001 byte 1 LD BC (01 H) • 1000 0000 byte 2 80 H • 0010 0000 byte 3 20 H • Loads the value “ 2080 H” into the two registers B and • C Note: the load starts by the low order byte followed by the high order 80 then 20 9
Z 80 Instruction Set l 4 -Byte Instructions • Not compatible with 8080 instruction set • 2 bytes Opcode and 2 bytes Operand • Ex. LD IX , 2000 H • Loads the contents of memory address 2000 H into IX register first 2 bytes • 0000 (00 H) byte 3 • 0010 0000 (20 H) byte 4 10
Z 80 Instruction Set l Instruction Categories • Data Copy transfer or load operations • Arithmetic Operations • Logic Operations • Bit Manipulation • Branch Operation • Machine Control Operations 11
Data Copy Instructions l l From register to register • LD A, B load B into A Specific 8 bits data into register • LD B, 32 H load “ 32” into B Specific 16 bits data into register • LD HL , 2080 H loads “ 2080” into HL From memory location into register • LD A , (2010 H) load the content of memory location (2010) into A 12
Data Copy Instructions l l From input port into accumulator • IN A, (01 H) loads data from Input port (01 H) into A From the accumulator into the o/p port • OUT (07 H) , 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 13
Arithmetic Instructions l 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, 97 H add the value “ 97 H” to the accumulator content 14
Arithmetic Instructions l Subtraction • A register or memory location can be subtracted from • • l the accumulator SUB C subtract the contents of register C from the accumulator SUB 47 H subtract the “ 47 H” from the accumulator Note : the accumulator in implied in the instruction 15
Arithmetic Instructions l Increment / Decrement • Add / Sub 1 to/from the contents of any register or memory location • INC B • DEC BC 16
Arithmetic Instructions l 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 ) 17
Group Activity l Write a simple program to load the values “ 53 H” and “F 5 H” into registers A and B respectively. Then add the two registers? l LD A, 53 H LD B, F 5 H ADD A, B l l 18
Logic Operations l l l 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 19
Bit Manipulation l Bit Test l Bit Set/Reset • Verify the value of a bit (0 or 1) • Z flag is the indicator • BIT 7, B check bit 7 in register B • SET 5, B • RES 2, B 20
Branching Operations l l l Jump • Change the program sequence • JP C, 2050 H if C flag is set , jump to 2050 H Call/Return • Change the program sequence by calling or returning from a sub routine • CALL 2050 H call subroutine located at 2050 H Restart • Memory are divided into pages • Page number 00 marked with 8 restart locations • RST 28 H restart from the location 28 H 21
Machine Control Operations l Control the Z 80 operations l HALT Suspend execution of an instruction 22
How to write a program ? l Phases • Problem Statement • Analysis • Flowchart • Write the Assembly • Execute 23
Flowchart Components 24
Write the Assembly l Will try to get our hands dirty in the lab 25
Addressing Modes l A way of specifying the operand or pointing to a data location • Immediate extended • Register • Implied • Register indirect • Extended • Relative • Indexed • Bit • Page Zero 26
Implied Memory Addressing l 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 27
Addressing Modes l Immediate Extended l Register • A byte following the opcode is the operand • LD B, 97 H 97 h id the value • Two bytes following the opcode are the operands • LD BC , 3040 H are the 2 bytes value • Operand is a register • LD A, B B is a register 28
Addressing Modes l l Implied • • The opcode imply one of the operands AND B AND implies that the operation is done on the accumulator contents Register Indirect • • Register holds the memory location address LD B, (HL) the memory location is stored in H and L registers l Extended • • The two bytes following the opcode specify the jump location JP 208 H 29
Addressing Modes l l Relative • The oprand indicates the placement of the next instruction to be executed relative to the current one • JR 14 H from the next instruction , jump 20 locations Indexed • Use one of the index registers to define the next instruction location • INC (IX+10 H) if IX contains 2080 H , then the content of the memory location (2080 +10) will be incremented 30
Addressing Modes l Bit l Page zero • Used with bit operations • SET 7, B • Reset operation • RST 28 H 31
Reading Materials l Chapters 6 and 7 l Please find one of the free Z 80 assemblers and play with it 32
Data Copy Operations 33
Load Instruction l l LD rd , rs copy data from rs to rd LD r, 8 -bit immediate addressing mode , loaf 8 bit number into register r • LD B, 32 H LD rp , 16 - bit immediate extended addressing mode , load 16 -bits into register pair • LD HL, 1840 H LD rx, 16 -bit immediate extended addressing mode , loads 16 bits into specified index register • LD IX, 2050 H 34
Load instructions Example l Write a program to do the following: • Load 97 h into the accumulator • Loads 2050 H into HL register • Loads 2070 H 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 2000 H • Show the register contents by the end of this program ? 35
Answer Mem. Address Hex code Operand 2000 3 E LD A, 97 H 2001 97 2002 21 LD HL, 2050 H 2003 50 2004 20 2005 DD LD IX, 2075 H 2006 21 2007 75 2008 20 2009 79 LD C, A 200 A 44 LD B, H 200 B 76 HALT 36
Data Copy Between Registers and Memory l l 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), 97 H 37
Data Copy Between Registers and Memory l l 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 (2050 H), A 38
Data Copy Between Registers and Memory Example l The memory location 2050 H contains the data byte 37 H , write instructions to copy a byte from the memory location into register B? 39
Answer l Method 1: • LD HL, 2050 H • LD B, (HL) l Method 2: • LD DE, 2050 H • LD A, (DE) • LD B, A l Method 3: • LD A, (2050 H) • LD B, A 40
Data Copy Between Accumulator and I/O l IN A, (8 -bit) read data from input port to the accumulator l OUT (8 -bit) , A write data into the output port l See Example in Page 191 41
Group Activity l Write comments to explain the function of the following instructions • LD HL, 2065 H • LD (HL), 00 H • HALT 42
Group Activity l l l l l Specify the contents of the registers and memory locations if any after the execution of the following instructions: A B C H L 34 7 F FF 01 00 LD A, 00 H LD BC, 8058 H LD B, A LD HL, 2040 H LD L, C LD (HL), A HALT 43
l l l l 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. arabteam 2000 -forum. com/index. php? showtopic=76314 44
- Slides: 44