EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410...

39
1 Week 5 © Vocational Training Council, Hong Kong. 8051 Assembly Language Programming (1) 8051 Assembly Language Programming (1) EEE3410 Microcontroller Applications Department of Electrical Engineering Lecture 5

Transcript of EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410...

Page 1: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

1Week 5© Vocational Training Council, Hong Kong.

8051 Assembly Language Programming (1)8051 Assembly Language Programming (1)

EEE3410 Microcontroller ApplicationsDepartment of Electrical Engineering

│ Lecture 5 │

Page 2: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

2Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

8051 programming modelAssembly language syntaxOperation codes and operandsMachine instructionsHow 8051 interprets binary dataExample of Assembly language program8051 Instruction SetInstruction time calculationData movement instructions

In this Lecture In this Lecture …………

Page 3: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

3Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

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

IntroductionIntroduction

Page 4: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

4Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

8051 Programming Model8051 Programming Model

7F

Page 5: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

5Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Syntax = format/rule

[label:] mnemonic [operands][;comment]

Items in square brackets are optional

Assembly Language SyntaxAssembly Language Syntax

Page 6: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

6Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

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

Mnemonic/Mnemonic/ Operation Code (Operation Code (OpcodeOpcode) ) ((操作碼操作碼))

Page 7: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

7Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

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

Operand Operand ((操作元操作元))

Page 8: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

8Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

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

MnemonicMnemonic / Machine InstructionsMachine Instructions

Page 9: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

9Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

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, #10hEquivalent Machine codes: 24h 10h (hexadecimal)

Binary nature of machine instruction Binary nature of machine instruction

Page 10: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

10Week 5© Vocational Training Council, Hong Kong.

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 Equivalent Machine codesNOP 00h (hexadecimal)ADD A, R0 28hINC A 04h

How 8051 Interprets Binary DataHow 8051 Interprets Binary Data

Page 11: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

11Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

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 Equivalent Machine codesADD A, #30h 24h 30h (hexadecimal)ADD A, 30h 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 codesLJMP 0130h 02h 01h 30h (hexadecimal)

How 8051 Interprets Binary DataHow 8051 Interprets Binary Data

Page 12: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

12Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Content of the List file of an assembly language programLOC OBJ LINE SOURCE0025 1 COUNT EQU 25H ;COUNT = 25H0000 2 ORG 0H ;start (origin) at location 00000 AD25 3 MOV R5, COUNT ;load 25H into R50002 7F34 4 MOV R7, #34H ;load 34H into R70004 7400 5 MOV A, #0 ;load 0 into A 0006 2D 6 ADD A, R5 ;add contents of R5 to A, now A = A + R50007 2F 7 ADD A, R7 ;add contents of R7 to A, now A = A + R70008 2412 8 ADD A, #12H ;add to A value 12H, now A = A + 12H000A 00 9 NOP ;no operation000B 80FE 10 HERE: SJMP HERE ;stay in this loop0020 11 ORG 20H0020 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 ATTRIBUTESCOUNT N NUMB 0025H ADATA1 C ADDR 0020H ADATA2 C ADDR 0021H AHERE C ADDR 000BH A

REGISTER BANK(S) USED: 0 ASSEMBLY COMPLETE. 0 WARNING(S), 0 ERROR(S)

Machine codes stored in memory

Source program in Assembly Language (Mnemonics)

Page 13: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

13Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

PseudoPseudo--instructions/Directives instructions/Directives

-- ORG (origin)ORG (origin)indicates the beginning of the address of the instructions. The number that comes after ORG can be either hex or decimal.

-- ENDENDindicates to the assembler the end of the source assembly instructions.

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 ofthe assembly language program into machine codes.

Page 14: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

14Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

PseudoPseudo--instructions/Directives instructions/Directives

-- EQU (equate)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.

Page 15: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

15Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

8051 Instruction Set8051 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

Page 16: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

16Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

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:

Instruction Time CalculationInstruction Time Calculation

s085.1)12.(100592.11

1T 6cyclemachine µ=×

=

Page 17: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

17Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Example 5Example 5--11For 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

Page 18: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

18Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Solution to Example 5Solution to Example 5--11

4 x 1.085µs = 4.34 µs4(g) MUL AB

1 x 1.085µs = 1.085 µs1(f) NOP

2 x 1.085µs = 2.17 µs2(e) SJMP

2 x 1.085µs = 2.17 µs2(d) LJMP

2 x 1.085µs = 2.17 µs2(c) DJNZ R2, target

1 x 1.085µs = 1.085 µs1(b) DEC R3

1 x 1.085µs = 1.085 µs1(a) MOV R3, #55

Time to executeTime to executeMachine Machine cyclescyclesInstructionInstruction

Page 19: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

19Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Example 5Example 5--22Find the execution time for the following program segment, assuming a crystal frequency of 11.0592MHz.

--END1ASWAP230h, AMOV1A, R1ADD102PUSH2A, BMOV101PUSH4ABMUL 2B, #2MOV2A, #100MOV--00hORG

Machine Cycle

Page 20: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

20Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Solution to Example 5Solution to Example 5--22

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 #

Page 21: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

21Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Moving data from a source to a destinationMOV

MOVX

MOVC

PUSH

POP

XCH

XCHD

Data Movement InstructionsData Movement Instructions

Page 22: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

22Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

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

The MOV InstructionThe MOV Instruction

Page 23: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

23Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

MOV A, 80h - Copy data from 80h (port 0) to register A

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)

Examples of MOV InstructionExamples of MOV Instruction

Page 24: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

24Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

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

The MOVX InstructionThe MOVX Instruction

Page 25: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

25Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

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

Examples of MOVX InstructionExamples of MOVX Instruction

Page 26: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

26Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

MOVC destination, source

“C” means the data movement is from the source address in code ROM to register AUsed with data transfer between internal/external ROM and register A, e.g. reading a table from the program memory

The MOVC InstructionThe MOVC Instruction

Page 27: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

27Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

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 Ato form the final address of the code byte

Examples of MOVC InstructionExamples of MOVC Instruction

Page 28: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

28Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

A register

R0 or R1

DPTR

DPTR + A

PC + A

Data

External RAM

Data

Internaland

ExternalROM

WriteRead ReadWriteRead

8051

MOVX @DPTR

MOVX @Ri

MOVC A,@A + DPTR

MOVC A,@A + PC

Examples of MOVX and MOVC InstructionsExamples of MOVX and MOVC Instructions

Page 29: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

29Week 5© Vocational Training Council, Hong Kong.

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)

The StackThe Stack

Page 30: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

30Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

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)

The PUSH InstructionThe PUSH Instruction

Page 31: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

31Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

POP destination

Copy data from the stack to the destination address

SP is decremented by 1 (−1) after data is copied from the stack

The POP InstructionThe POP Instruction

Page 32: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

32Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

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

Other RemarksOther Remarks

Page 33: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

33Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

SP + 2

SP + 1

SP

Push Y

Push X

X

Y Pop Y

Pop X

SP

SP - 2

SP - 1

Internal RAM

Increment BeforePUSHing

Decrement AfterPOPing

Summary of PUSH and POPSummary of PUSH and POP

Page 34: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

34Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

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

ExamplesExamples

Page 35: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

35Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

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

Data ExchangingData Exchanging

Page 36: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

36Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

XCH destination, source

XCHD destination, source

XCH: Data exchange between register A and the addressed byte

XCHD: Data exchange between the lower-nibble of A and the addressed byte

(Upper-nibble of A remains unchanged)

The XCH and XCHD InstructionsThe XCH and XCHD Instructions

Page 37: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

37Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

XCH A, R7Exchange bytes between registers A and R7

XCH A, 0F0hExchange bytes between registers A and B

XCH A, @R1Exchange bytes between register A and address in R1

XCHD A, @R1Exchange lower-nibble in register A and the address in R1

ExamplesExamples

Page 38: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

38Week 5© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

The 8051 Microcontroller and Embedded Systems -Using Assembly and C, Mazidi

Chapter 2 P.37 – P.63

Read referenceRead reference

Page 39: EEE3410 Microcontroller Applications Department … 8051 Assembly Language Programming (1...EEE3410 Microcontroller Applications Department of Electrical Engineering ... Assembly language

1Week 5© Vocational Training Council, Hong Kong.

8051 Assembly Language Programming (1)8051 Assembly Language Programming (1)

EEE3410 Microcontroller ApplicationsDepartment of Electrical Engineering

│ END of Lecture 5 │