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
Assembly code
MOV A, # 4A
Mcahine code
BINARY CODE
Code:
0000 0000 0000 0000 0111 0100
0100 1010
Example
Code:
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
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
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
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
Code:
0000 0000 0010 01111 001
0101 1110
Code:
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
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
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
Code:
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