Lecture 5. EEE3410 Microcontroller Applications Department of Electrical Engineering Assembly Language Programming (1)

Similar documents
Assembly Language programming (1)

Microcontroller Intel [Instruction Set]

ENE 334 Microprocessors

8051 Overview and Instruction Set

Microprocessors 1. The 8051 Instruction Set. Microprocessors 1 1. Msc. Ivan A. Escobar Broitman

8051 Microcontroller

8051 Microcontroller Assembly Programming

Assembly Language programming (2)

Programming of 8085 microprocessor and 8051 micro controller Study material

Memory organization Programming model - Program status word - register banks - Addressing modes - instruction set Programming examples.

Chapter Addressing Modes

Architecture & Instruction set of 8085 Microprocessor and 8051 Micro Controller

Microcontroller. Instruction set of 8051

8051 Microcontrollers

Module Contents of the Module Hours COs

ELEG3924 Microprocessor

SN8F5000 Family Instruction Set

CHAPTER ASSEMBLY LANGUAGE PROGRAMMING

ELEG3923 Microprocessor Ch.2 Assembly Language Programming

UNIT-III ASSEMBLY LANGUAGE PROGRAMMING. The CPU can access data in various ways, which are called addressing modes

UNIT 2 THE 8051 INSTRUCTION SET AND PROGRAMMING

Digital Blocks Semiconductor IP

Principle and Interface Techniques of Microcontroller


Assembly Language programming (3)

Q. Classify the instruction set of 8051 and list out the instructions in each type.

Digital Blocks Semiconductor IP

8051 Instruction Set

EEE3410 Microcontroller Applications Department of Electrical Engineering Lecture 4 The 8051 Architecture

Digital Blocks Semiconductor IP

Dodatak. Skup instrukcija

UNIT THE 8051 INSTRUCTION SET AND PROGRAMMING

The 8051 Microcontroller and Embedded Systems

C51 Family. Architectural Overview of the C51 Family. Summary

Instruction Set Of 8051

8051 Microcontroller

8051 Microcontrollers

8051 Addressing Modes

Figure Programming model

Instruction : A command to the microprocessor to perform a given task on specified data. Each instruction has two parts

DR bit RISC Microcontroller. Instructions set details ver 3.10

Chapter Family Microcontrollers Instruction Set

ET2640 Microprocessors

Application Brief D-005

ELEG3924 Microprocessor

Programming Book Microcontroller Kit. Rev 3.0 January, Wichit Sirichote

8051 Core Specification

Contents. Join the Technical Community Today!

~: Simple Programs in 8051 assembly language :~

Question Bank Microprocessor and Microcontroller

MASSEY UNIVERSITY PALMERSTON NORTH CAMPUS

ELEG3923 Microprocessor Ch.3 Jump, Loop, and Call

JUMP, LOOP AND CALL INSTRUCTIONS

TUTORIAL. Donal Heffernan University of Limerick May Tutorial D.Heffernan 2000,

Principle and Interface Techniques of Microcontroller

1. Write A Program to move a block of data within the internal RAM

MODULE-1. Short Answer Questions

UNIT MICROCONTROLLER AND ITS PROGRAMMING

EE6502- MICROPROCESSOR AND MICROCONTROLLER

Dragonchip. Instruction Set Manual

TUTORIAL Assembly Language programming (2)

Assembly Language Programming of 8085

What Registers are available? Programming in Assembler. Assembler Programming - like early Basic. Assembler Data Movement Instructions

MICROPROCESSOR LABORATORY MANUAL

INSTRUCCIONES ARITMETICAS ERROR! MARCADOR NO DEFINIDO.


Microcontroller and Applications

Control Transfer Instructions Jump, Loop, and Call. ECE473/573 Microprocessor System Design, Dr. Shiue

Introduction to uc & Embedded Systems

MODEL ANSWER SUMMER 17 EXAMINATION Subject Title: Microcontroller Subject Code:

Assembly Language Programming of 8085

Embedded Controller Programming

EE 5340/7340 Motorola 68HC11 Microcontroler Lecture 1. Carlos E. Davila, Electrical Engineering Dept. Southern Methodist University

ET355 Microprocessors Thursday 6:00 pm 10:20 pm

ORG ; TWO. Assembly Language Programming

The Microcontroller. Lecture Set 3. Major Microcontroller Families. Example Microcontroller Families Cont. Example Microcontroller Families

NAME as31 - An Intel 8031/8051 assembler. SYNOPSIS as31 [-h] [-l] [-s] [-v] [-Aarg] [-Ffmt] [-Ofile] infile.asm

EEM336 Microprocessors I. Data Movement Instructions

CS 320. Computer Architecture Core Architecture

MCS -51 Programmer s Guide and Instruction Set

MODEL ANSWER WINTER 17 EXAMINATION Subject Title: Microcontroller and applications

Practical Malware Analysis

Subroutines & Software Delay Routines

UNIT MICROCONTROLLER

SUMMER 13 EXAMINATION

Unit-I. 1. INTRODUCTION TO MICROCONTROLLERS. Micro controller, types, selection of a microcontroller and applications

ENE 334 Microprocessors

WINTER 14 EXAMINATION Subject Code: Model Answer Page No: 1/ 26

INSTITUTE OF AERONAUTICAL ENGINEERING Dundigal, Hyderabad ELECTRONICS AND COMMUNICATION ENGINEERING QUESTION BANK

CPEG300 Embedded System Design. Lecture 6 Interrupt System

LAB FILE of EE1531: MICROCONTROLLER & EMBEDDED SYSTEMS LAB

80C51 family programmer s guide and instruction set. 80C51 Family. PROGRAMMER S GUIDE AND INSTRUCTION SET Memory Organization. Philips Semiconductors

MICROPROCESSORS AND MICROCONTROLLERS MATERIAL. Features of 8051:

8051 Instruction Opcodes In Hexadecimal Order

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

2. ADDRESSING METHODS

SOLUTION MANUAL FOR THE 8051 MICROCONTROLLER 4TH EDITION BY MACKENZIE AND PHAN

1. INTRODUCTION TO MICROPROCESSOR AND MICROCOMPUTER ARCHITECTURE:

Mark II Aiken Relay Calculator

reply db y prompt db Enter your favourite colour:, 0 colour db 80 dup(?) i db 20 k db? num dw 4000 large dd 50000

Transcription:

Department of Electrical Engineering Lecture 5 8051 Assembly Language Programming (1) 1

In this Lecture 8051 programming model Assembly language syntax Operation codes and operands Machine instructions How 8051 interprets binary data Example of Assembly language program 8051 Instruction Set Instruction time calculation Data movement instructions 2

Introduction You are now learning to be a programmer (a person who writes programs) You should know the internal structure of the 8051 to do good programming Programming model how programmers see the 8051 In the model, programmers can access the registers and accumulators with programs 3

8051 Programming Model 7F 4

Assembly Language Syntax Syntax = format/rule [label:] mnemonic [operands][;comment] Items in square brackets are optional 5

Mnemonic/ Operation Code (Opcode( Opcode) ( 操作碼 ) Program ( 電腦程式 ) = a set of instructions ( 指令 ) All computers work according to the program All instructions contain a verb ( 動詞 ), which tells the computer what to do This verb is called mnemonic/operation code e.g. MOV R0, #12h MOV is the opcode 6

Operand ( 操作元 ) Apart from Opcode, an instruction also includes object) to act on. The object is called an operand. Operand is optional. Instructions can have one, two or no operands. e.g. MOV R0, #12h --- R0 and #12h are two operands INC R1 --- R1 is the only one operand NOP --- no operand follows 7

Mnemonic / Machine Instructions Primitive ( 基本的 ) operations of the 8051 e.g. ADD A, #34 Each microprocessor has its unique instruction set Machine instructions = opcode + operand(s) Unary operand: 1 operand, e.g. CLR A Binary operand: 2 operands, e.g. ADD A, #10 8

Binary nature of machine instruction Unlike human, computers do not know verbal instructions; they only know 0s and 1s Binary data: program should be in a stream of 0s and 1s It is also called Machine Language For convenient purpose, machine instructions are usually expressed in hexadecimal (i.e. base-16) format, called machine codes. e.g. Mnemonic : ADD A, #10h Equivalent Machine codes: 24h 10h (hexadecimal) 9

How 8051 Interprets Binary Data EEE3410 Microcontroller Applications Machine instructions can be 3 bytes (24 bits), 2 bytes (16 bits) or 1 byte (8 bits) long The 1st byte (8 bits) is the operation code (opcode) The remaining byte(s) is/are the supplement data for the operation code 1-byte instruction: Contain the opcode only. Actions do not need supplement data. e.g. Mnemonic NOP ADD A, R0 INC A Equivalent Machine codes 00h (hexadecimal) 28h 04h 10

How 8051 Interprets Binary Data 2-byte instruction: The 1st byte is the opcode. The 2nd byte may be either an immediate data (a number) or the low-order byte of an address e.g. Mnemonic ADD A, #30h ADD A, 30h Equivalent Machine codes 24h 30h (hexadecimal) 25h 30h 3-byte instruction: The 1st byte is the opcode. The 2nd and the 3rd byte are the high-order byte and the low-order byte of an 16-bit memory address e.g. Mnemonic Equivalent Machine codes LJMP 0130h 02h 01h 30h (hexadecimal) 11

Content of the List file of an assembly language program LOC OBJ LINE SOURCE 0025 1 COUNT EQU 25H ;COUNT = 25H 0000 2 ORG 0H ;start (origin) at location 0 0000 AD25 3 MOV R5, COUNT ;load 25H into R5 0002 7F34 4 MOV R7, #34H ;load 34H into R7 0004 7400 5 MOV A, #0 ;load 0 into A 0006 2D 6 ADD A, R5 ;add contents of R5 to A, now A = A + R5 0007 2F 7 ADD A, R7 ;add contents of R7 to A, now A = A + R7 0008 2412 8 ADD A, #12H ;add to A value 12H, now A = A + 12H 000A 00 9 NOP ;no operation 000B 80FE 10 HERE: SJMP HERE ;stay in this loop 0020 11 ORG 20H 0020 39 12 DATA1: DB 39H ; 0021 416D6572 13 DATA2: DB "America ; 0025 696361 14 END ;end of assembly source file SYMBOL TABLE LISTING ------ ----- ------- N A M E T Y P E V A L U E ATTRIBUTES COUNT N NUMB 0025H A DATA1 C ADDR 0020H A DATA2 C ADDR 0021H A HERE C ADDR 000BH A REGISTER BANK(S) USED: 0 ASSEMBLY COMPLETE. 0 WARNING(S), 0 ERROR(S) Source program in Assembly Language (Mnemonics) Machine codes stored in memory 12

Pseudo-instructions/Directives Beside mnemonics, directives are used to define variables and memory locations where the machine codes are stored. These directives are interpreted by assembler during the conversion of the assembly language program into machine codes. - ORG (origin) indicates the beginning of the address of the instructions. The number that comes after ORG can be either hex or decimal. - END indicates to the assembler the end of the source assembly instructions. 13

Pseudo-instructions/Directives - EQU (equate) used to define a constant without occupying a memory location. It does not set aside storage for a data item but associates a constant value with a data label so that when the label appears in the program. Its constant value will be substituted for the label. - DB (define byte) DB (define byte) used to define 8-bit data and store them in assigned memory locations. Define data can be in decimal, binary, hex, or ASCII formats. 14

8051 Instruction Set There are roughly 241 instructions in 8051 instruction set. They can be grouped into 6 groups. - Data movement instructions - Arithmetic operation instructions - Logic and byte operation instructions - Bit operation instructions - Program branching instructions - Special instruction Refer to the 8051 instruction set summary for details 15

Instruction Time Calculation Depends on the clock frequency of oscillator 1 machine cycle = 12 oscillator cycle For a 11.0592MHz oscillator, the time for 1 machine cycle is: 1 machine cycle =.(12) = 1.085 µ s 11.0592 10 T 6 16

Example 5-15 For an 8051 system of 11.0592MHz, find how long it takes to execute each of the following instructions. (a) MOV R3, #55 (b) DEC R3 (c) DJNZ R2, target (d) LJMP (e) SJMP (f) NOP (g) MUL AB 17

Solution to Example 5-15 Instruction (a) MOV R3, #55 (b) DEC R3 (c) DJNZ R2, target (d) LJMP (e) SJMP (f) NOP (g) MUL AB Machine cycles 1 1 2 2 2 1 4 Time to execute 1 x 1.085µs = 1.085 µs 1 x 1.085µs = 1.085 µs 2 x 1.085µs = 2.17 µs 2 x 1.085µs = 2.17 µs 2 x 1.085µs = 2.17 µs 1 x 1.085µs = 1.085 µs 4 x 1.085µs = 4.34 µs 18

Example 5-25 EEE3410 Microcontroller Applications Find the execution time for the following program segment, assuming a crystal frequency of 11.0592MHz. ORG MOV MOV MUL PUSH MOV PUSH ADD MOV SWAP END 00h A, #100 B, #2 AB 01 A, B 02 A, R1 30h, A A Machine Cycle -- 2 2 4 1 2 1 1 2 1 -- 19

Solution to Example 5-25 Total machine cycles required, [2 + 2 + 4 + 1 + 2 + 1 + 1 + 2 + 1 ] = 16 machine cycles The execution time of the program segment is : Execution time = 16 x 1.085µs = 17.36µs # 20

Data Movement Instructions Moving data from a source to a destination MOV MOVX MOVC PUSH POP XCH XCHD 21

The MOV Instruction MOV destination, source Used for data movement inside the 8051 No change in original (source) data: a copy of it is made and the copy is then moved to the destination Source can be data/register/memory locations Destination can be register/memory locations Note that the destination cannot be an immediate data 22

Examples of MOV Instruction MOV A, 80h - Copy data from 80h (port 0) to register A EEE3410 Microcontroller Applications MOV 80h, A - Copy data from register A to RAM address 80h (port 0) MOV 3Ah, #3Ah - Copy immediate data 3Ah to RAM location 3Ah MOV R0, 12h - Copy data from RAM location 12h to R0 MOV 5Ch, A - Copy data from register A to RAM location 5Ch MOV 08Ah, 77h - Copy data from RAM address 77h to 08Ah (IE register) 23

The MOVX Instruction MOVX destination, source X means the data movement is external to the 8051 data movement is between the external RAM and the (internal) register A All MOVX instructions must involve register A All MOVX instructions must use indirect addressing mode Operation is similar to the MOV instruction 24

Examples of MOVX Instruction MOVX @DPTR, A - Copy data from A to the 16-bit address in DPTR MOVX @R0, A - Copy data from A to the 8-bit address in R0 MOVX A, @R1 - Copy data from the 8-bit address in R1 to A MOVX A, @DPTR - Copy data from the 16-bit address in DPTR to A 25

The MOVC Instruction MOVC destination, source C means the data movement is from the source address in code ROM to register A Used with data transfer between internal/external ROM and register A, e.g. reading a table from the program memory 26

Examples of MOVC Instruction MOVC A, @A+DPTR (Copy the code byte to A) - This code byte is found at the ROM address formed by adding A and the DPTR MOVC A, @A+PC (Copy the code byte to A) - This code byte is found at the ROM address formed by adding A and the PC Note that the PC is incremented by 1 before added to A to form the final address of the code byte 27

Examples of MOVX and MOVC Instructions 8051 Read Write Read Write Read A register Data Data R0 or R1 DPTR MOVX @Ri MOVX @DPTR External RAM Internal and External ROM DPTR + A PC + A MOVC A,@A + DPTR MOVC A,@A + PC 28

The Stack EEE3410 Microcontroller Applications Stack: an area of internal RAM for fast data storage and retrieval Stack operation follows the first-in-last-out (FILO), or equivalently, last-in-first-out (LIFO) logic (i.e. the stack grows up as data is stored) Stack pointer (SP): a register that stores the address of the stack s top item Adding data to the stack: SP increments (+1) and then data is stored on the stack Getting data from the stack: Data is read from the stack and then SP decrements ( 1) 29

The PUSH Instruction PUSH source Copy data from the source address to the stack SP is incremented by 1 before source data is copied to the stack Too many PUSH operations may overflow the stack, (i.e. stack runs out of memory) 30

The POP Instruction POP destination Copy data from the stack to the destination address SP is decremented by 1 ( 1) after data is copied from the stack 31

Other Remarks SP register is set to 07h when the 8051 is reset To prevent the stack from running out of memory, programmer needs to initialize the SP to a value above the highest address likely to be used by the program This value is usually above the register banks The first PUSH operation writes data to R0 in bank 1(08h) SP rolls over to 00h after it reaches FFh PUSH > 7Fh will result in error as the RAM ends at address 7Fh 32

Summary of PUSH and POP SP + 2 Push Y Y Pop Y SP SP + 1 Push X X Pop X SP - 1 SP SP - 2 Increment Before PUSHing Internal RAM Decrement After POPing 33

Examples MOV 81h, #30h ; Copy the immediate data 30h to SP MOV R0, #0ACh ; Copy the immediate data ACh to R0 ; (i.e. 00h) PUSH 00h ; SP=31h, address 31h contains the ; number ACh PUSH 00h ; SP=32h, address 32h contains the ; number ACh POP 01h ; SP=31h, address R1 (i.e. 01h) ; contains the number ACh POP 80h ; SP=30h, port 0 latch (i.e. 80h) ; contains the number ACh 34

Data Exchanging XCH and XCHD are data exchange instructions data movement is bi-directional (i.e. source destination) Data exchanging operations are internal to the 8051 All data exchanging operations must use register A 35

The XCH and XCHD Instructions XCH XCHD destination, source destination, source XCH: Data exchange between register A and the addressed byte XCHD: Data exchange between the lowernibble of A and the addressed byte (Upper-nibble of A remains unchanged) 36

Examples XCH A, R7 Exchange bytes between registers A and R7 XCH A, 0F0h Exchange bytes between registers A and B XCH A, @R1 Exchange bytes between register A and address in R1 XCHD A, @R1 Exchange lower-nibble in register A and the address in R1 37

Read reference The 8051 Microcontroller and Embedded Systems - Using Assembly and C, Mazidi Chapter 2 P.37 P.63 38

Department of Electrical Engineering END of Lecture 5 8051 Assembly Language Programming (1) 1