What do the sequence of instructions in 8051 assembler do?

313 views Asked by At

I have this code in 8051 assembler :

    MOV A,#04H
    RL A
    MOVC A,@A+PC
    SJMP Cont
Cont:   DB 10H, 20H, 30H, 40H, 50H, 60H, 70H, 80H

The question is that what is value of A after these instructions are executed; the answer was that A holds the value (A)=70H.

I have searched about MOV and RL instructions and I understood,but it is unclear to me,what do the rest of instructions and how did we get the value of register A?

1

There are 1 answers

0
Margaret Bloom On

MOVC A, @A+PC moves 8-bit of data from the program memory (MOVC stands for "MOVe Code" or similar) at the address given by A+PC and stores it into the accumulator.
The presence of the @ is eloquent, it is used to denote a register indirect addressing mode, the full expression @A+PC specify that it is actually an indexed addressing mode.
Note that:

  1. The PC has already been incremented by the time it is used to do the memory access.
    Since MOVC A, @A+PC is 1 byte long, its semantic is:

    PC = PC + 1
    A = (A+PC)
    
  2. In the symbolic expression @A+PC, the @ is meant to have less precedence than the +, so it should be understood as @(A+PC).

SJMP is a Short JuMP and it simply execution to the target specified as the operand.

MOV A, #04H moves the value 04h into A.
Here the symbol "#" denotes immediate addressing mode.

RL A simply rotate right A by one position.


Let's assume the code starts at X, then

Address   Data     Instruction        State before         State after             Description
X + 00  : 74 04  : MOV A, #04       : A = ?  PC = X        : A = 4  PC = X + 02    Set A to 4 
X + 02  : 23     : RL A             : A = 4  PC = X + 02   : A = 8  PC = X + 03    Rotate left 4 = 0000 0100 -> 0000 1000 = 8, now A is 8
X + 03  : 83     : MOVC A, @A+PC    : A = 8  PC = X + 03   : A = 70 PC = X + 04    Read from memory A+PC, A = MEM[A + PC after] = MEM[8 + X + 04] = MEM[X + 0C] = 70
X + 04  : 80 00  : SJMP X + 06      : A = 70 PC = X + 04   : A = 70 PC = X + 06    Goto X + 06, PC <- X + 06
X + 06  : 10
X + 07  : 20
X + 08  : 30
X + 09  : 40
X + 0A  : 50
X + 0B  : 60
X + 0C  : 70
X + 0D  : 80

Simply put, when MOVC A, @A+PC is executed, a byte is read from the address of the next instruction added by 8 (the value in A).
Since SJMP, the next instruction, is 1 byte long, it is like reading 7 bytes from Cont.
The seventh item of Cont is 70h.