8051 Instruction set

Thread Starter

vead

Joined Nov 24, 2011
629
Hello experts

8051 Instruction set

5 bit opcode + 3 bit register specification
7 bit opcode + 1 register specification
8 bit opcode



Many ALU can directly access program memory . so they use registers

· Accumulator register
· Rn register (R0 to R7)
· Ram memory
· Program memory
· Instruction decoder
· Program counter
· ALU (arithmetic and Logic unit)
· PSW register

5 bit opcode that allow 32 operations
7 bit opcode that allow 128 operations
8 bit opcode that allow 256 operations

5 bit opcode + 3 bit register specification
Example

MOV A, Rn
11101 n n n
Source Rn registers
Destination Accumulator
Example Mov A,R1
11101 0 0 0 MOV A, R0 Mov the content of R0 into A
11101 0 0 1 MOV A, R1 Mov the content of R1 into A
11101 0 1 0 MOV A, R2 Mov the content of R2 into A
11101 0 1 1 MOV A, R6 Mov the content of R3 into A
11101 1 0 0 MOV A, R6 Mov the content of R4 into A
11101 1 0 1 MOV A, R6 Mov the content of R5 into A
11101 1 0 1 MOV A, R6 Mov the content of R6 into A


7 bit opcode + 1 register specification

MOV @Ri, A
1111011
Source Accumulator
Destination register R0 or R1

11110110 MOV@R0,A
11110111 MOV @R1, A

Example: Mov@R1,A
Mov the content of A to the address in register R1

8 bit opcode

MOV A, direct
11100101 direct
Source address
Destination Accumulator

Example :Mov A, 80h
Mov the data from direct address 80h to register A


5 bit opcode + 3 bit register specification
7 bit opcode + 1 register specification
8 bit opcode



Q I don’t understand whats the use of three different Instruction formmet ?

Q2 first two formmet, I think they don’t use Instruction decoder ,I think they store data direcly to the respective registers . Last formmet , I think it use Instruction decoder ?

Q3 If possble anyone please explain how the diifferent instruction execute in different formmet
 

alfacliff

Joined Dec 13, 2013
2,458
the 8051 was designed to be a very universal processor, there are several commands duplicated, but with slight differences, to accomodate this.
 

Brevor

Joined Apr 9, 2011
297
The 8051 has 255 opcodes, the A5 opcode is not used and listed as "reserved". Over the years there has been much speculation about what the "secret" A5 opcode does.
 

Thread Starter

vead

Joined Nov 24, 2011
629
We write assembly code on assembler like this

Assembly code

MOV A, # 4A ; load data 4A into A register

MOV R1,# 5E ; load the data 5E into R1

ADD A,R1 ; add the content of R1 into A register

INC R1 ; increase R1 with 1

INC A ; increase A with 1

MOV A,R1 ; move the value of R1 into A register


Look following way how this assembly code convert in binary form and tell me Is it correct to learn Instruction set of 8051

Assembly code
MOV A, # 4A
Mcahine code
0000 74 4A
BINARY CODE
0000 0000 0000 0000 0111 0100
0100 1010

Example
Memory address Hex code assembly language comments
0000 74 4A MOV A,#4A ; Load the immediate data into A
Where 0000 is memory address. 74 is mov A opcode and 4A is operand

Explationation :
Processor read Byte from Program memory at the address indicate by program counter
Example: processor read Byte (0111 0100) which is opcode MOV A
First processor(cpu) need to read first byte of instruction

Example 0111 0100 74 first byte
0100 1010 4A second Byte

So processor read Byte form program memory at the address 0000 indicate by program counter and Store Data 4A into Accumulator registor

So program counter increament and its ready for next bytes in program counter

Ok If next memory address is 0002
Assembly code
MOV R1,# 5E
Mcahine code
0002 79 5E
Binary code
0000 0000 0010 01111 001
0101 1110

processor read Byte (0111 0100) which is opcode MOV R1

So processor read Byte form program memory at the address 0002 indicate by program counter and Store Data 4A into Accumulator registor
Again program counter increament and its ready for next bytes in program counter

Ok If next memory address is 0004

Assembly code
ADD A,R1
Machine code
0004 29
Binary code
00000 00000 0100 00101001 (00101rrr)

Memory address Hex code assembly language comments
0004 74 ADD A,R1 ; Add the content of R1 into A

So the processor doesn't read any more bytes from program memory; it can start executing the instruction immediately. It knows that it needs to add the contents of the specified register into the accumulator, and it uses the ALU to do this.

8051 has 8 bit ALU
The instruction execution logic unit store the current data 4A of Accumulator into the Input of ALU
And instruction execution logic unit store the current data of R3 into the second input to the ALU
Then it tells the ALU to perform an addition operation
and the result appears at the output of the ALU.

ALU also use pse register toremember carry auxillary carry overflow …
Then the program counter is incremented to 0005.

Assembly code
INC R1
Machine code
0005 0B
Binary code
0000 0000 0101 00001 001

The instruction execution logic unit will Increment R1 register
Increment program counter to next Instruction 0006

Assembly code
INC A
Machine code
0006 04
Binary code
0000 0000 0110 00000100

The instruction execution logic unit will Increment A register

Increment program counter to next Instruction 0007

Assembly code
MOV A,R1

Machine code
0007 E9

Binary code
0000 0000 0000 0111 11101001

Now the code look like in assembly

Assembly code

MOV A, # 4A ; load data 4A into A register

MOV R1,# 5E ; load the data 5E into R1

ADD A,R1 ; add the content of R1 into A register

INC R1 ; increase R1 with 1

INC A ; increase A with 1

MOV A,R1 ; move the value of R1 into A register


Machine code

0000 74 4A

0002 79 5E

0004 29

0005 0B

0006 04

0007 E9


Binary code
Code:
0000 0000 0000 0000  0111 0100

                                               0100 1010
0000 0000 0000 0010  01111 001

                                           0101 1110
0000 0000 0000 0100  00101001
0000 0000 0000 0101  00001 001
0000 0000 0000 0110 00000100
0000 0000 0000 0111 11101001


Finally to understand someone


Assembly code

0000 74 4A MOV A, # 4A ; load data 4A into A register

0002 79 5E MOV R1,# 5E ; load the data 5E into R1

0004 29 ADD A, R1 ; add the content of R1 into A register

0005 0B INC R1 ; increase R1 with 1

0006 04 INC A ; increase A with 1

0007 E9 MOV A,R1 ; move the value of R1 into A register



Program memory will be following
Code:
Memory address opcode operand
0000  74 4A
0002  79 5E
0004  29
0005  0B
0006  04
0007  E9

Program counter will be following
Code:
0000 0000 0000 0000
0000 0000 0000 0010
0000 0000 0000 0100
0000 0000 0000 0101
0000 0000 0000 0110
 

alfacliff

Joined Dec 13, 2013
2,458
some people confuse machine code, assempler, and compler, the assembler and compiler are there just to make it easier for the programmer to write the program and to read a program. the machine language s what the comands actually are to the processer. there are also decompilers and disasemblers to turn machine code back into readable form, although they dont include the comments and labels, only addresses.
 

takao21203

Joined Apr 28, 2012
3,702
If you consider implementations of FAT filesystem, or JPEG decoders on controllers, your questions are fairly irrelevant. The problems here are on the C language level- how it translates into machine instructions is not interesting.

You'd get old if you dig into the disassembly.

You are either a genius for binary and internal works of a CPU, and can guess most things, or you arent, in which case I'd recommend just to treat the CPU as black box, and deal with C language problems instead.

It can be a challenge just to compile a ready-made source, adapt the IO configuration, and wire the SD card as well TFT, finally decode a JPEG. Try a BMP decoder first, perhaps.
 

Thread Starter

vead

Joined Nov 24, 2011
629
If you consider implementations of FAT filesystem, or JPEG decoders on controllers, your questions are fairly irrelevant. The problems here are on the C language level- how it translates into machine instructions is not interesting.

You'd get old if you dig into the disassembly.

You are either a genius for binary and internal works of a CPU, and can guess most things, or you arent, in which case I'd recommend just to treat the CPU as black box, and deal with C language problems instead.

It can be a challenge just to compile a ready-made source, adapt the IO configuration, and wire the SD card as well TFT, finally decode a JPEG. Try a BMP decoder first, perhaps.
I am not asking about software I am asking about hardware
Look at MOV instruction
this is two byte instruction
74= 0111 0100=MOV A 1 byte
4A= 0100 1010 1bite
Ok processor read the binary byte from program memory
To get something from memory called fetching
ok that means we are taking 0111 0100 byte from memory called fetching

Data 4A= 0100 1010 store in to A register call execution
but what is decode unit ?
 

takao21203

Joined Apr 28, 2012
3,702
I am not asking about software I am asking about hardware
Look at MOV instruction
this is two byte instruction
74= 0111 0100=MOV A 1 byte
4A= 0100 1010 1bite
Ok processor read the binary byte from program memory
To get something from memory called fetching
ok that means we are taking 0111 0100 byte from memory called fetching

Data 4A= 0100 1010 store in to A register call execution
but what is decode unit ?
Its like if you consider the traffic light circuit of New York, look at a broadway and ask "But what is travel permission".

One could tell you its individually wired and the roads plastered + signs put up + racks with computers + uplink to control centres.

It works according to a predefined rule network. Traffic is measured, computations are made, and individual lights are turned on and off.
 

takao21203

Joined Apr 28, 2012
3,702
Intel has multibyte instructions, simply put.

Certain opcodes put the CPU in a state where it will expect another byte.

Is that so difficult to find out yourself? If so, maybe its the wrong job you are trying to learn. It wont get better, it will get worse, as certainly, not all is explained in a way all is included and totally clear.

Looking for instance at touch button source codes for STM8, with your approach of researching, asking and reasoning, not even "good luck" could be expressed, rather "dont touch it at all".

I mean, you wont get anywhere if you cant put things together yourself. Not to speak, you wont be a brilliant hardware crackwhiz neither, inventing something new. Or probably you will, because you question things which by now, should be quite obvious to you already?

Maybe some day this will allow you to see them in a way we dont, make some clever deduction, and win a nobel prize.
 

takao21203

Joined Apr 28, 2012
3,702
To understand something complex we should have basic knowledge. so I started to learn basic I am trying to learn hardware of 8051 from the lot of days . If I learn about 8051 then I can start to learn about another controller so thats why asking here. Its batter for me If you can give any advice about how does Instruction fetch decode and execute. ?
Well sure. I studied the 68000.

It is indeed a 4bit controller, which runs a microcode, emulating a 16bit Instruction set.

The first 4 bits are the instruction group.

Inside the larger CPU, there is a smaller, simpler controller.

It cycles through a number of states, during it will bridge certain paths, latch certain data, and activate sub-circuits, then it will connected the accumulator for instance to the EA unit, and latch the result, or connect to the register bank, and latch the result.

Good explanation eh? Inside the controller, there is another, simpler controller, which you dont see, and which instructions set you dont know, so, you dont need to wonder about it.
 

cmartinez

Joined Jan 17, 2007
8,220
To understand something complex we should have basic knowledge. so I started to learn basic I am trying to learn hardware of 8051 from the lot of days . If I learn about 8051 then I can start to learn about another controller so thats why asking here. Its batter for me If you can give any advice about how does Instruction fetch decode and execute. ?
The 8051 "fetches" the code and then executes it in steps synchronous according to its internal clock. Think of the clock as the controller's heartbeat, in which something is executed every time it beats. Take a look at this video, and it will give you a glimpse on how an MCU's internal clock executes instructions. You can think of it as a clock cycle being executed each time a marble is added into the machine.
In a classic 8051 most instructions usually take 12 clock cycles, while others 24 and others even up to 36. In the most recent 8051 models (such as Atmel's 89LP4052) the cycles per instruction have been minimized to 1, 2 and 3, (maybe even up to 4 clock cycles) depending on the instruction being executed. This results in a far superior execution speed and less power drain.
 

Thread Starter

vead

Joined Nov 24, 2011
629
The 8051 "fetches" the code and then executes it in steps synchronous according to its internal clock. Think of the clock as the controller's heartbeat, in which something is executed every time it beats. Take a look at this video, and it will give you a glimpse on how an MCU's internal clock executes instructions. You can think of it as a clock cycle being executed each time a marble is added into the machine.
In a classic 8051 most instructions usually take 12 clock cycles, while others 24 and others even up to 36. In the most recent 8051 models (such as Atmel's 89LP4052) the cycles per instruction have been minimized to 1, 2 and 3, (maybe even up to 4 clock cycles) depending on the instruction being executed. This results in a far superior execution speed and less power drain.
sorry But due to some technical problem I can't see that video but can you help me to With mov instruction
this is sequence
0111 0100 opcode
0100 1010 operand
Mov A,#4A
processor read first byte from program memory (fetching )
data 4A will be store on A register executing
my question is how does register A know that it need to store data ? decoding
 

cmartinez

Joined Jan 17, 2007
8,220
sorry But due to some technical problem I can't see that video but can you help me to With mov instruction
this is sequence
0111 0100 opcode
0100 1010 operand
Mov A,#4A
processor read first byte from program memory (fetching )
data 4A will be store on A register executing
my question is how does register A know that it need to store data ? decoding
Not sure what you mean... but the "MOV A, #data" instruction has an encoding of "0111 0100" ... when the MCU finds that encoding during execution, it already understands what it's supposed to do... it's supposed to store the given data into the A register (which is at memory location E0H)... I've attached the 8051 Hardware Manual, which explains in detail all these technicalities, including the standard 8051 memory map, and the complete catalog of standard instructions ... hope that helps...
 

Attachments

takao21203

Joined Apr 28, 2012
3,702
sorry But due to some technical problem I can't see that video but can you help me to With mov instruction
this is sequence
0111 0100 opcode
0100 1010 operand
Mov A,#4A
processor read first byte from program memory (fetching )
data 4A will be store on A register executing
my question is how does register A know that it need to store data ? decoding
http://en.wikipedia.org/wiki/Finite-state_machine



Somehow with your posts you tell the forum you dont know anything about how a CPU works.

Read up State machine.
Read up turnstile for GODs sake: http://en.wikipedia.org/wiki/Turnstile

the turnstile doesnt "know" if you have a ticket, clear?

Read up http://en.wikipedia.org/wiki/Turing_machine

Think of a large 3 dimensional turnstile.

When it gets the MOV byte, it snaps into a position where it will look for a databyte. It doesnt know a thing, and the register has no clue either.

Its no matter if 8051 or what the binary codes are, thats just trivial, you can ditch it, if you understand it, you dont need the binary codes. And if you dont, they wont help you either.
 

cmartinez

Joined Jan 17, 2007
8,220
I think I'm beginning to understand what your question here is...

Think of an MCU as an extremely complex network of logic gates, with some that give a direct output, depending of their inputs, (such as AND's, NAND's, OR's, XOR's) and some others that depend on a clock (such as JK flip-flops, and SR latches) etc...
jk2.gif

depending on the encoding of the instruction, this instruction will ALWAYS be channeled through the same series of gates (which could be dozens in a row, or in parallel, and be even nested), that is why, even if you're using the same assembly mnemonic in two similar instructions, such as MOV A, #10D and MOV A, R1, they're actually two different instructions once they're compiled, since one stores a direct value into register A, and the other the contents of R1... although both instructions are similar, they're processed through an entirely different set of pre-assigned logic gates after they're fetched by the MCU
 

kubeek

Joined Sep 20, 2005
5,794
my question is how does register A know that it need to store data ? decoding
Register A know only that write into it is going to happen.
I will try to explain the stages of computer cycle:
Fetch - an instruction is read from memory into decoder
Decode - control lines are set according to the instructioin
Exectue - the operation is carried out, i.e. the data is written to reg A in your case.
These three allways take turn.

The steps for that instruction:
1) Fetch 0111 0100 opcode from memory
2) decode 0111 0100 and see that you need to read the next byte as well
3) execute - do nothing, you dont have anything to do yet
4)fetch 0100 1010 operand
5) decode - do nothing, its not an opcode, so you dont need to decode it
6) execute - write operand to reg A.

If the operation was INC A -0000 0100 it would go:
1) Fetch 0000 01000
2) decode it and set the control wires to ALU
3) execute - increment A
 

Thread Starter

vead

Joined Nov 24, 2011
629
Register A know only that write into it is going to happen.
I will try to explain the stages of computer cycle:
Fetch - an instruction is read from memory into decoder
Decode - control lines are set according to the instructioin
Exectue - the operation is carried out, i.e. the data is written to reg A in your case.
These three allways take turn.

The steps for that instruction:
1) Fetch 0111 0100 opcode from memory
2) decode 0111 0100 and see that you need to read the next byte as well
3) execute - do nothing, you dont have anything to do yet
4)fetch 0100 1010 operand
5) decode - do nothing, its not an opcode, so you dont need to decode it
6) execute - write operand to reg A.

If the operation was INC A -0000 0100 it would go:
1) Fetch 0000 01000
2) decode it and set the control wires to ALU
3) execute - increment A
Decode - control lines are set according to the instructioin
that's ok to execute instruction we need to set control lines or wires
11101 rrr its 1 byte instruction

11101 0 0 1 MOV A, R1 Mov the content of R1 into A
Last three number 001 is control signal that set to register R1
When last three digit is 001 ,register R1 will be set

11101 0 1 0 MOV A, R2 Mov the content of R2 into A
Last three number 010 is control signal that set to register R2
When last three digit is 001 ,register R2 will be set

11101 0 1 1 MOV A, 3 Mov the content of R3 into A
Last three number 011 is control signal that set to register R3
When last three digit is 011 ,register R3 will be set

11101 1 0 0 MOV A, R4 Mov the content of R4 into A
Last three number 100 is control signal that set to register R4
When last three digit is 100 ,register R4 will be set

11101 1 0 1 MOV A, R5 Mov the content of R5 into A
Last three number 101 is control signal that set to register R5
When last three digit is 101 ,register R5 will be set

11101 1 0 1 MOV A, R6 Mov the content of R6 into A
Last three number 101 is control signal that set to register R6
When last three digit is 001 ,register R2 will be set

11101 1 0 1 MOV A, R7 mov the content of R7 into A
Last three number 010 is control signal that set to register R7
When last three digit is 101 ,register R7 will be set

example
11101 0 0 0 MOV A, R0 Mov the content of R0 into A

Last three number 000 is control signal that set to register R0
When that three input will be low , register R0 will be set

In this example three 000 input wire need to set R0
Now we have 5 number or wires 11101

I don’t understanding which number specify the which type of control signal ?
To set the accumulator it can take 1 ,or 2 or 3 or 5 number or wire

How to know that which specific number or wire is used by control unit to set accumulator ?

Ok this instruction contain 8 bit means 8 wire. we have already taken 3 wires to specify the register ,now I have only 5 wires but we have to tell other component don’t do anything during this instruction

Don’t use alu – need 1 wire to do activate or deactivate
Don’t increament program counter- need 1 wire to do activate or deactivate
Don’t use PSW register - need 1 wire to do activate or deactivate
Don’t use immediate data - need 1 wire to do activate or deactivate
I think I need more then 8 wire to execute this instruction

But in this instruction I have only 8 signal so how to set that I need and how to tell other that I don’t need you ?

Control signal are required inside processor inorder to perform each type of opeartions
Control unit generate right signal so that the processor can perform requested operation

how to findout which control signal is required for different operations ?
 

kubeek

Joined Sep 20, 2005
5,794
how to findout which control signal is required for different operations ?
You need to have the full diagram of the processor. Each part, like different registers and temporary buffers, incrementers etc. have their own control signals. Lets say you have 45 control wires going to different places in the procesor
(just a wild guess). The decoder could then be described as a huge table that has 8 inputs and 45 outputs, and 256 rows. For each instruction you activate the wires that are needed to perform it.
There dont need to be any rules such as "last three bits set the address", but designers usually do it in such way to make it more user friendly.
 

ScottWang

Joined Aug 23, 2012
7,397
Using 74HC181 to be the ALU and adding some logic gates, some 74ls89 or SRAM, eeprom, 74LS244(74HC244), 74HC574 to build a small uC has 16 commands, then you will learn a lots of hardware about uC.

Another way is to using 8051 to simulating the functions of 74HC181 and above functions.
 
Top