29
111/06/07 111/06/07 Microcomputers and Microproce Microcomputers and Microproce ssors ssors Suranaree University Suranaree University Of Technology Of Technology มมม มมม 2002 Anant Oonsivilai 2002 Anant Oonsivilai Chapter 2 Chapter 2 8051 Assembly 8051 Assembly Language Language Programming Programming

Chapter 2 8051 Assembly Language Programming

  • Upload
    mariko

  • View
    316

  • Download
    30

Embed Size (px)

DESCRIPTION

Chapter 2 8051 Assembly Language Programming. Outlines. 8051 registers Manipulate data using registers & MOVE instructions Code simple assembly language instructions Assemble and run a program Sequence events upon power-up Examine programs in ROM codes ROM memory map - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 2 8051 Assembly Language Programming

112/04/22112/04/22 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors

Suranaree UniversitySuranaree University

Of TechnologyOf Technologyมทสมทส

2002 Anant Oonsivilai2002 Anant Oonsivilai

Chapter 2Chapter 28051 Assembly 8051 Assembly

Language ProgrammingLanguage Programming

Page 2: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

OutlinesOutlines8051 registers8051 registersManipulate data using registers & MOVE Manipulate data using registers & MOVE instructionsinstructionsCode simple assembly language instructionsCode simple assembly language instructionsAssemble and run a programAssemble and run a programSequence events upon power-upSequence events upon power-upExamine programs in ROM codesExamine programs in ROM codesROM memory mapROM memory mapExecution of instructionsExecution of instructionsData typesData typesPSW register (Program Status Word)PSW register (Program Status Word)RAM memory spaceRAM memory spaceStackStackRegister banksRegister banks

Page 3: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

8051 Registers8051 Registers

D7D7 D6D6 D5D5 D4D4 D3D3 D2D2 D1D1 D0D0

DPTR

PC PC (Program counter)

DPH DPL

Figure2-1 (a): Some 8 bit Registers of the 8051

Figure2-1 (b): Some 8051 16 bit Registers

8 bit Registers

R6R6

R5R5

R4R4

R3R3

R2R2

R1R1

R0R0

BB

AA

R7R7

Page 4: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

MOV InstructionMOV Instruction

MOV destination, source ; copy source to MOV destination, source ; copy source to dest.dest.MOV A,#55HMOV A,#55H ;load value 55H into reg. A ;load value 55H into reg. AMOV R0,AMOV R0,A ;copy contents of A into R0 ;copy contents of A into R0

;(now A=R0=55H) ;(now A=R0=55H)MOV R1,AMOV R1,A ;copy contents of A into R1 ;copy contents of A into R1

;(now A=R0=R1=55H) ;(now A=R0=R1=55H)MOV R2,AMOV R2,A ;copy contents of A into R2 ;copy contents of A into R2

;(now A=R0=R1=R2=55H) ;(now A=R0=R1=R2=55H)MOV R3,#95HMOV R3,#95H ;load value 95H into R3 ;load value 95H into R3

;(now R3=95H) ;(now R3=95H)MOV A,R3MOV A,R3 ;copy contents of R3 into A ;copy contents of R3 into A

;now A=R3=95H ;now A=R3=95H

Page 5: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

Notes on ProgrammingNotes on Programming

Value (proceeded with #) can be loaded Value (proceeded with #) can be loaded directly to registers A, B, or R0 – R7directly to registers A, B, or R0 – R7 MOV R5, #0F9HMOV R5, #0F9H

If values 0 to F moved into an 8-bit If values 0 to F moved into an 8-bit register, the rest assumed all zerosregister, the rest assumed all zeros MOV A, #5MOV A, #5

A too large value causes an errorA too large value causes an error MOV A, #7F2HMOV A, #7F2H

Page 6: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

ADD InstructionADD Instruction

ADD A, source ADD A, source ;ADD the source operand;ADD the source operand

;to the accumulator;to the accumulator

MOV A, #25H MOV A, #25H ;load 25H into A;load 25H into AMOV R2,#34H MOV R2,#34H ;load 34H into R2;load 34H into R2ADD A,R2 ADD A,R2 ;add R2 to accumulator;add R2 to accumulator

;(A = A + R2) ;(A = A + R2)

Page 7: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

Structure of Assembly Structure of Assembly LanguageLanguage

ORG 0HORG 0H ;start (origin) at location 0 ;start (origin) at location 0MOV R5,#25HMOV R5,#25H ;load 25H into R5 ;load 25H into R5MOV R7,#34HMOV R7,#34H ;load 34H into R7 ;load 34H into R7MOV A,#0MOV A,#0 ;load 0 into A ;load 0 into AADD A,R5ADD A,R5 ;add contents of R5 to A ;add contents of R5 to A

;now A = A + R5 ;now A = A + R5ADD A,R7ADD A,R7 ;add contents of R7 to A ;add contents of R7 to A

;now A = A + R7 ;now A = A + R7ADD A,#12HADD A,#12H ;add to A value 12H ;add to A value 12H

;now A = A + 12H ;now A = A + 12HHERE: SJMP HEREHERE: SJMP HERE ;stay in this loop ;stay in this loop

ENDEND ;end of asm source file ;end of asm source file

Program 2-1:Sample of an Assembly Language Program

Page 8: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

Steps to Create a ProgramSteps to Create a Program

Page 9: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

8051 Program Counter & ROM 8051 Program Counter & ROM SpaceSpace

Page 10: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

8051 Program Counter & ROM 8051 Program Counter & ROM SpaceSpace

Page 11: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

8051 Program Counter & ROM 8051 Program Counter & ROM SpaceSpace

Page 12: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

Execute a Program Byte by Execute a Program Byte by ByteByte

1.1. PC=0000: opcode 7D fetched; 25 PC=0000: opcode 7D fetched; 25 fetched; fetched; R5←25; PC+2R5←25; PC+2

2.2. PC=0002: opcode 7F fetched; 34 PC=0002: opcode 7F fetched; 34 fetched; R7←34; PC+2fetched; R7←34; PC+2

3.3. PC=0004; opcode 74 fetched; 0 fetched; PC=0004; opcode 74 fetched; 0 fetched; A←0; PC+2A←0; PC+2

4.4. PC=0006; opcode 2D fetched; A←A+R5; PC=0006; opcode 2D fetched; A←A+R5; PC+1PC+1

5.5. (Similarly…)(Similarly…)

Page 13: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

8051 On-Chip ROM Address 8051 On-Chip ROM Address RangeRange

Page 14: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

Data Types & DirectivesData Types & Directives

ORG 500HORG 500H

DATA1: DB 28 DATA1: DB 28 ;DECIMAL (1C in Hex);DECIMAL (1C in Hex)

DATA2: DB 00110101B DATA2: DB 00110101B ;BINARY (35 in Hex);BINARY (35 in Hex)

DATA3: DB 39H ;DATA3: DB 39H ;HEXHEX

ORG 510HORG 510H

DATA4: DB “2591” DATA4: DB “2591” ; ASCII NUMBERS; ASCII NUMBERS

ORG 518HORG 518H

DATA6: DB “My name is Joe” DATA6: DB “My name is Joe” ;ASCII CHARACTERS;ASCII CHARACTERS

Page 15: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

PSW (Flag) RegisterPSW (Flag) Register

Page 16: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

Instructions Affecting Flag BitsInstructions Affecting Flag Bits

Page 17: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

ADD Instruction and PSWADD Instruction and PSW

Page 18: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

ADD Instruction and PSWADD Instruction and PSW

Page 19: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

ADD Instruction and PSWADD Instruction and PSW

Page 20: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

8051 RAM Allocation8051 RAM Allocation

Page 21: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

8051 Register Banks8051 Register Banks

Page 22: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

Access RAM Locations Using Register Access RAM Locations Using Register NamesNames

Page 23: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

Access RAM Locations Using Access RAM Locations Using AddressesAddresses

Page 24: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

Switch Register BanksSwitch Register Banks

Page 25: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

Switch Register BanksSwitch Register Banks

Page 26: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

Pushing onto StackPushing onto Stack

Page 27: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

Popping from StackPopping from Stack

Page 28: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

Stack & Bank 1 ConflictStack & Bank 1 Conflict

Page 29: Chapter 2 8051 Assembly Language Programming

2002 Anant Oonsivilai2002 Anant Oonsivilai

Stack & Bank 1 ConflictStack & Bank 1 Conflict