Maker Pro
Maker Pro

LED blinking assembly code

hello ,
i need help to understand how does each instruction fetch decode and execute in 8051 microcontroller
I made small project for LED blinking on proteus software
circuit
upload_2015-1-22_21-17-38.png

assembly code :
Code:
      ORG     0

MainLoop:
        setb    P1.0                    ; Turn LED ON
        Acall   Delay                   ; Wait a short time
        clr     P1.0                    ; Turn LED OFF
        Acall   Delay                   ; Wait a short time
          sjmp  MainLoop                ; Loop forever

Delay:
        mov     R1,#90                  ; Set up outer loop count
        mov     R2,#225                   ; Set up inner loop count
Delay1: djnz    R2,Delay1               ; Inner loop
        djnz    R1,Delay1               ; Outer loop
        ret

        end
I don't understand how does following instruction fetch , decode and execute ?
Code:
 MainLoop:
Acall  Delay
sjmp  MainLoop
I need information how does jump , branch , instruction fetch , decode and execute ?

I looked this link http://www.keil.com/support/man/docs/is51/is51_sjmp.htm
The SJMP instruction transfers execution to the specified address. The address is calculated by adding the signed relative offset in the second byte of the instructions to the address of the following instruction. The range of destination addresses is from 128 before thenext instruction to 127 bytes after the next instruction.

Operation
SJMP
PC = PC + 2
PC = PC + offset

I found this wikki page wikibooks.org/wiki/Microprocessor_Design/Program_Counter
there are diagram for jump and branch instruction. I don't understand why there are different multiplexer and ALU for jump and branch instructions. Please help me to understand that page on Program counter design.
how does PC increment or load with new address ? what is offset ?
 
Last edited:

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Are you a competent 8051 programmer yet?

Show me some code you've written, and explain to me how it operates.

If you can do that then it's reasonable to consider how the 8051 executes your code.
 
Are you a competent 8051 programmer yet?

Show me some code you've written, and explain to me how it operates.

If you can do that then it's reasonable to consider how the 8051 executes your code.

Ok here is some code
Data transfer Instruction
MOV A, # data
Bytes : 2
Cycles : 1
Function : load the immediate data into Accumulator
Code:
Address
0000  0001       74 05        MOV A,#5h             2 bytes instruction
0002  0003       74 12        MOV A,#12h          2 bytes instruction
0004  0005       74 14        MOV A,#14h          2 bytes instruction
0006  0007       74 52        MOV A,#52h          2 bytes instruction
0008  0009       74 25        MOV A,#25h          2 bytes instruction
000A  000B       74 35       MOV A,#35h           2 bytes instruction
000C  000D       74 45       MOV A,#45h           2 bytes instruction
000E  000F        74 54       MOV A,#54h           2 bytes instruction
0010  0011        74 55       MOV A,#55h           2 bytes instruction
Description : processor start to fetch the f opcode from location of the program ROM indicate by program counter. Processor read first byte 74 which is code for moving immediate data(05) to accumulator. Processor fetch data and places its into accumulator

Data transfer Instruction
MOV R0, # data
Bytes : 2
Cycles : 1
Function : load the immediate data into R0
Code:
Address
0000  0001       78 05        MOV R0,#5h             2 bytes instruction
0002  0003       78 12        MOV R0,#12h          2 bytes instruction
0004  0005       78 14        MOV R0,#14h          2 bytes instruction
0006  0007       78 52        MOV R0,#52h          2 bytes instruction
0008  0009       78 25        MOV R0,#25h          2 bytes instruction
000A  000B       78 35       MOV R0,#35h           2 bytes instruction
000C  000D       78 45       MOV R0,#45h           2 bytes instruction
000E  000F        78 54       MOV R0,#54h           2 bytes instruction
0010  0011        78 55       MOV R0,#55h           2 bytes instruction

Description : processor start to fetch the f opcode from location of the program ROM indicate by program counter. Processor read first byte 78 which is code for moving immediate data(05) to
Register R0. Processor fetch data and place it into register R0

Data transfer Instruction
MOV R1, # data
Bytes : 2
Cycles : 1
Function : load the immediate data into R1
Code:
Address
0000  0001       79 05        MOV R1,#5h             2 bytes instruction
0002  0003       79 12        MOV R1,#12h          2 bytes instruction
0004  0005       79 14        MOV R1,#14h          2 bytes instruction
0006  0007       79 52        MOV R1,#52h          2 bytes instruction
0008  0009       79 25        MOV R1,#25h          2 bytes instruction
000A  000B       79 35       MOV R1,#35h           2 bytes instruction
000C  000D       79 45       MOV R1,#45h           2 bytes instruction
000E  000F        79 54       MOV R1,#54h           2 bytes instruction
0010  0011        79 55       MOV R1,#55h           2 bytes instruction
Descriptions : processor start to fetch the f opcode from location of the program ROM indicate by program counter. Processor read first byte 79 which is code for moving immediate data(05) to register R1. Processor fetch data and places its into register R1

as similar I can write another Instructions
 
Last edited:

KrisBlueNZ

Sadly passed away in 2015
Hi vead.

Please post the listing file for your LED blinking program. This listing will include line numbers, addresses, and the opcode bytes that the assembler has generated for each line. We only need the main section that contains the actual code. It should look something like this.
Code:
01 0000       MainLoop:
02 0000 D2 90     setb    P1.0      ; Turn LED ON
03 0002 11 0A     acall   Delay     ; Wait a short time
04 0004 C2 90     clr     P1.0      ; Turn LED OFF
05 0006 11 0A     acall   Delay     ; Wait a short time
06 0008 80 F4     sjmp    MainLoop  ; Loop forever
07
08 000A       Delay:
09 000A 79 5A     mov     R1,#90    ; Set up outer loop count
10 000C 7A E1     mov     R2,#225   ; Set up inner loop count
11 000E       Delay1:
12 000E DA FE     djnz    R2,Delay1 ; Inner loop
13 0010 D9 FC     djnz    R1,Delay1 ; Outer loop
14 0012 22        ret

As Steve says, you need to understand (a) how those instructions create the blinking LED behaviour that you see, and (b) how those instruction lines relate to the binary instructions that control the 8051. Once you understand that, then you can start to learn about how the 8051 core actually processes them.

Please don't mention that subject again. Remember my racing car driver analogy. You need to learn to drive the car. Learning about how to design a car engine is not appropriate and asking about it now will just irritate us.
 
Top