44
Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Embed Size (px)

Citation preview

Page 1: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Microprocessor

Dr. Rabie A. RamadanAl-Azhar University

Lecture 7

Page 2: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Z80 Assembly Programming

2

Page 3: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Programming Phases

3

High-level language (C, C++, Pascal)

Assembly language (Z80)

Machine language

assembler

compiler

linker

Object code

Page 4: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

4

Page 5: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

5

Page 6: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Z80 instruction Set

Instructions can be classified to:

• 1-Byte instructions

• 2-Byte instructions

• 3-Byte instructions

• 4-Byte instructions 6

Page 7: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

7

Page 8: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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”

8

Page 9: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

9

Page 10: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

10

Page 11: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Z80 Instruction Set

Instruction Categories • Data Copy transfer or load operations

• Arithmetic Operations

• Logic Operations

• Bit Manipulation

• Branch Operation

• Machine Control Operations

11

Page 12: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

12

Page 13: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

13

Page 14: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

14

Page 15: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

15

Page 16: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Arithmetic Instructions

Increment / Decrement

• Add / Sub1 to/from the contents of any register or memory location

• INC B

• DEC BC

16

Page 17: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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 )

17

Page 18: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

18

Page 19: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

19

Page 20: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

20

Page 21: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

21

Page 22: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Machine Control Operations

Control the Z80 operations

HALT Suspend execution of an instruction

22

Page 23: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

How to write a program ?

Phases • Problem Statement

• Analysis

• Flowchart

• Write the Assembly

• Execute

23

Page 24: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Flowchart Components

24

Page 25: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Write the Assembly

Will try to get our hands dirty in the lab

25

Page 26: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

26

Page 27: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

27

Page 28: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Addressing Modes

Immediate • A byte following the opcode is the operand

• LD B, 97H 97h id the value

Immediate Extended • 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

28

Page 29: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Addressing Modes

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

Extended • The two bytes following the opcode specify the jump location

• JP 208H

29

Page 30: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

30

Page 31: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Addressing Modes

Bit • Used with bit operations

• SET 7, B

Page zero • Reset operation

• RST 28H

31

Page 32: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Reading Materials

Chapters 6 and 7

Please find one of the free Z80 assemblers and play with it

32

Page 33: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Data Copy Operations

33

Page 34: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Load Instruction

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, 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

34

Page 35: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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 ?

35

Page 36: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Answer Mem. Address Hex code Opcode Operand

2000 3E LD A,97H

2001 97

2002 21 LD HL, 2050H

2003 50

2004 20

2005 DD LD IX, 2075H

2006 21

2007 75

2008 20

2009 79 LD C,A

200A 44 LD B,H

200B 76 HALT36

Page 37: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

37

Page 38: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

38

Page 39: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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?

39

Page 40: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

40

Page 41: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

41

Page 42: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

Group Activity

Write comments to explain the function of the following instructions• LD HL, 2065H

• LD (HL), 00H

• HALT

42

Page 43: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

43

Page 44: Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7

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

44