Embedded software 1
1 / 33

Embedded Software 1 - PowerPoint PPT Presentation

  • Uploaded on

Embedded Software 1. General 8051 features (excluding I/O). CPU 8 bit microcontroller The basic registers include (more discussed later) The 8-bit A (accumulator) and B registers, 16-bit program counter (PC) 8-bit program status register (PSW) 8-bit stack pointer (SP) Memory

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' Embedded Software 1' - zelda

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

General 8051 features excluding i o
General 8051 features (excluding I/O)

  • CPU

    • 8 bit microcontroller

    • The basic registers include (more discussed later)

      • The 8-bit A (accumulator) and B registers,

      • 16-bit program counter (PC)

      • 8-bit program status register (PSW)

      • 8-bit stack pointer (SP)

  • Memory

    • Separate program and data memory

      • Internal ROM used to store the program memory.

      • Internal data memory consists of 256 bytes of internal RAM

8051 program memory
8051 Program Memory

  • Program memory is used to store the program code.

    • This is the machine code that is obtained from the assembly language or C program. The machine code represents instructions and constants.

  • When the 8051 is powered up, program execution always starts from the location 0X0000.

    • After the RESET operation, the program counter (PC) contains 0000h, causing the first instruction to be read from the program location 0000h.

  • The program memory addresses are 16-bits long.

    • Therefore the 8051 can directly address 216 = 64k locations.

  • Each program memory location stores a single byte.

  • The CPU can only read from program memory.

8051 data memory
8051 Data Memory

  • Can read/write from/to data memory.

    • Therefore the contents of data memory can change as the program executes.

  • Data memory is used to temporary data used by a program. Also used to store the stack.

  • The total size of the 8051’s internal RAM is small

    • 256 bytes in total !

  • Data memory map shows 256 locations

    • Bottom 32 locations are used

    • 224 locations available

    • Addresses are shown in hex

Simplified programming model for 8051
Simplified Programming Model for 8051

  • Note that there are more important registers to add to this model!!! (See these later)

  • The 8051 is an accumulator based CPU

    • Operations on data are performed on data in A and result is placed in A

    • E.g

      Add A,#23

  • Accumulator based machines are common for older 8-bit machines

Instruction sets
Instruction Sets

  • The instruction set is the set of instructions that the CPU can decode and execute.

  • It defines the processor with each processor having it's own instruction set.

  • The general groupings of instructions for any uP are

    • Arithmetic/Logic

    • Data Movement

    • Transfer of Control

    • Test/Compare

    • Input/Output (only on some CPU's )

    • Others

  • An instruction stored in memory is represented by a certain number of byte(s).

    • Some CPUs have fixed length instructions, i.e. all the instructions in the instruction set are of equal size

Instruction encoding
Instruction Encoding

  • The instruction contains

    • what the operation is,

    • the operands for the instruction if there are any.

    • the destination for the result if there is one

  • The part of the instruction that determines the instruction is called the opcode

    • Examples are

      • ADD

      • JMP

  • An operand may be

    • an immediate value, memory address or a CPU register. The location for the result of an operation could be a memory address or a CPU register.

  • For an 8 bit machine such as the 8051, an instruction can be represented by 1, 2 or 3 bytes depending on the instruction

Instruction encoding1
Instruction Encoding

  • Details for the 8051 (8 bit machine) are in notes

    • The 8051 is an example of an accumulator based machine

      • Accumulator machines have a special purpose register that normally serves as an operand and destination for an instruction

      • E.g. ADD A,#27

        Encoding 00100100 00011011

        24h 1Bh

      • 2 byte instruction

Instruction encoding2
Instruction Encoding

  • Multiples of bytes

  • 8 bit opcode gives 28=256 possible opcodes

    • 255 actually used

      • 139 are 1 byte instructions

      • 92 are 2 byte instructions

      • 24 are 3 byte instructions

  • Most instructions are one byte, some two, a few three

    • See back of notes for complete details on instructions

  • Example of a 1 byte instruction is the following

    CLR C

  • which is represented by machine code C3H or the equivalent bit pattern 11000011

Instruction timing
Instruction Timing

  • The microcontroller takes a minimum of 12 clock cycles (machine cycle) to execute an instruction

    • Some instructions are slower to execute, taking 24 clock cycles

    • There are no instructions with 36 clock cycles, but there are 2 instructions that take 48 clock cycles (or 4 machine cycles)

      • MUL and DIV instructions

  • See back of lab notes for instruction cycle times for all instructions

  • Note that the instruction size does not directly imply how long the instruction cycle is!

8051 instruction set
8051 Instruction set

ACALL addr11 DIV AB LJMP addr16 RETI

ADD A,<src> DJNZ <byte>,<rel8> MOV <dest>,<src> RL A

ADDC A,<src> INC <byte> MOV DPTR,#data16 RLC A

AJMP addr11 INC DPTR MOV bit,bit RR A

ANL <dest,<src> JB bit,rel8 MOVC A,@A+<base> RRC A

ANL C,<bit> JBC bit,rel8 MOVX <dest>,<src> SETB bit

CJNE <dest>,<src>,rel8 JC rel8 MUL AB SJMP rel8


CLR bit JNB bit,rel8 ORL <dest>,<src> SWAP A

CPL A JNC rel8 ORL C,bit XCH A,<byte>

CPL bit JNZ rel8 POP direct XCHD A,@Ri

DA A JZ rel8 PUSH direct XRL <dest>,<src>

DEC <byte> LCALL addr16 RET

How many instructions??

Getting started with the instruction set
Getting started with the instruction set

  • A key requirement is the ability to move data

    • Between registers

    • Between memory and registers

    • Between memory

  • The MOV instruction can be used

    • Different modes of addressing available

Introducing the MOV instruction


Aside representing hexadecimal values
Aside: Representing hexadecimal values

  • On paper, a hexadecimal value is represented in one of the following ways



  • However, for an assember, a hexadecimal value starting with the digit A to F must be preceded by 0x.

    • This is so as to distinguish the number from an identifier.

    • Therefore, the representation 0xA4 must be used with an assembler.

  • Example

    MOV A,#12

  • Here the destination is the A register and the source is an immediate value

    • All immediate values are preceded by the hash symbol

  • Using an immediate value means that the source byte is constant

  • The value can be decimal or hex

Introducing the MOV instructionand immediate values


  • Register addressing is another method for accessing data

  • Example

    MOV R3,#32

    MOV A,R3

  • Firstly the decimal value 32 is copied into R3, while the second instruction copies the value in R3 into A.

  • Any one of the 8 registers R0-R7 can be used

    • Use Rn to indicate any register when describing register addressing

Introducing the MOV instruction and register addressing



Introducing the MOV instruction with register addressingand immediate values

Destination operand

Uses register addressing


MOV R3,#32


Source operand

Uses immediate value


Destination operand


Source operand

Uses register addressing


Introducing the MOV instruction showing instruction encoding

Aside, what is the benefit of short instructions?

MOV R3,#32


01111011 00100000

7B 20



  • To access data in lower RAM, direct addressing is used

  • Example

    MOV 0x20,A

  • Here, the destination is hex address 20 and the source value, which is in A, is copied into RAM

  • Direct addressing can only be used to access the lower 128 bytes and NOT the upper 128 bytes

Introducing the MOV instruction and direct addressing


Note the difference between immediate values and direct addressing be careful
Note the difference between immediate values and direct addressing: Be careful!!!

  • It is important to understand the difference between an immediate value and direct addressing

    MOV A,#0x64 MOV A,0x64

Here, the value 64h is stored in A

Here, the value at address 64h is stored in A.

Find out in lab the machine code for both of these?

  • To access data ìn any one of the 256 RAM locations, addressing: Be careful!!! indirect addressing can be used

  • Example

    MOV @R0,A

  • Indirect addressing can be specified using either @R0 or @R1

    • when discussing indirect addressing, it is common to refer to @Ri, where i could be 1 or 0.

  • How does it work??

    Read on!!!

Introducing the MOV instruction and indirect addressing



  • Example addressing: Be careful!!!

    MOV @R0,A

  • The contents of R0 are interpreted as an address in RAM. If R0 contains the value 20h, then the above instruction will copy the value in A into address 20h in RAM.

    • In effect, it is now possible to indirectly access a memory location

      • Have you seen this idea before, C programmers?

Introducing the MOV instruction and indirect addressing



Summary of addressing modes

Immediate value addressing: Be careful!!!


e.g. MOV A,#56h

Regsister addressing


e.g. MOV R6,R3

Direct Addressing


e.g. MOV R6,4Eh

Indirect addressing


e.g. MOV @R1,R6

Good news for us this semester

The first 3 are the addressing modes that will be mostly used and that indirect addressing will be rarely used

Summary of addressing modes

Full details for mov instructions
Full details for addressing: Be careful!!!MOV instructions

MOV A,source

MOV A,#data

MOV dest,source

MOV dest,#data

Where dest and source can be any of

Rn, direct or @Ri

Appendix addressing: Be careful!!!

8051 block diagram
8051 addressing: Be careful!!!Block diagram

Instruction encoding3

3 bits addressing: Be careful!!!

5 bits


0011 1

Instruction encoding

  • Another example is

    ADDC A,R1

  • This instruction adds the contents of A and the contents of R1 using the carry and stores the result in A

  • The machine code is 39H. In general for this instruction, it will be encoded as follows

Exercises addressing: Be careful!!!

  • Draw a block diagram of the 8051.

  • What is the purpose of the program memory on the 8051?

  • At what address does the 8051 start executing code from upon powerup?

  • How many memory locations in the 8051 program memory?

  • What register contains the address of the next program instruction?

    • How many bits in this register?

Exercises addressing: Be careful!!!

  • Can program memory be written to during program execution?

  • What is the purpose of the 8051 data memory?

  • What size is the data memory?

  • Draw a simplified programming model for the 8051, making sure to label all registers.

  • What does it mean to say that a processor is an accumulator based machine

  • What is an instruction set?

  • List the general groupings of instructions that make up an instruction set?

Exercises addressing: Be careful!!!

  • What information does an instruction contain?

  • On the 8051, what size are the instructions?

  • What is machine code for the following instruction and how many clock cycles does it take?

    ADD A,R5

  • Do we need to remember the machine code for each instruction?

  • How many 8051 instructions are there?

  • What is the purpose of the MOV instruction?

Exercises addressing: Be careful!!!

  • What is an immediate value?

  • How is it represented?

  • What instruction is used to move the immediate decimal value 12 into the accumulator?

  • What is register addressing?

  • Move the value in register R4 into the accumulator. What is the resulting machine code?

  • What is direct addressing? Give an example.

Exercises addressing: Be careful!!!

  • What is indirect addressing? Give an example

  • Write out the code to move the decimal values 0,1,15 and 8 into the regsiters R0 to R3 respectively?

  • Write out the code to mode the hexadecimal values 0,a5,15 and c into the registers R0 to R4 respectively.

  • Write out the code to move the initialise the memory locations 20h to 24h with zero.

  • Write out the code to swap the values in R0 and R1.