Mov r1, r0, mcu 8051

Thread Starter

PG1995

Joined Apr 15, 2011
832
Hi

I have just tried to write my first assembly program for 8051 and sadly it's all errors! It seems one can't copy value from one Rx register to some other Rx register. Is this correct? The book I use didn't mention this. I wonder why.

When I learned basic C++ I was able to enclose multi-line comment within /* comment */. Is there something like this in the assembly too?

Please help me with the queries above. Thank you.


Rich (BB code):
;move some hex value, e.g. 89H to register A of CPU. Then from register A 
;move it to all registers of RAM, i.e. R0 - R7. 

ORG 0H        ;start at memory location 0H

    Mov A, #89H
    Mov R0, A
    Mov R1, R0
    Mov R2, R1
    Mov R3, R2
    Mov R4, R3
    Mov R5, R4
    Mov R6, R5
    Mov R7, R6

END        ;program ends here
Errors
Rich (BB code):
Compiling file: program1.asm
Initializing pre-processor ...
Syntax error at 9 in program1.asm: Invalid set of operands: mov R1,R0
Syntax error at 10 in program1.asm: Invalid set of operands: mov R2,R1
Syntax error at 11 in program1.asm: Invalid set of operands: mov R3,R2
Syntax error at 12 in program1.asm: Invalid set of operands: mov R4,R3
Syntax error at 13 in program1.asm: Invalid set of operands: mov R5,R4
Syntax error at 14 in program1.asm: Invalid set of operands: mov R6,R5
Syntax error at 15 in program1.asm: Invalid set of operands: mov R7,R6
Pre-processing FAILED !
Creating code listing file ...        -> "program1.lst"
7 errors, 0 warnings
 

Arm_n_Legs

Joined Mar 7, 2007
186
Refer to the 8051 instruction set. There isn't a MOV Rn,Rn instruction. However, since the registers can be addressed by its direct addresses, you can use MOV Direct,Direct which is a valid instruction.
 

Papabravo

Joined Feb 24, 2006
22,111
Refer to the 8051 instruction set. There isn't a MOV Rn,Rn instruction. However, since the registers can be addressed by its direct addresses, you can use MOV Direct,Direct which is a valid instruction.
Since there are four(4) register banks in the first 32 locations of internal SRAM you have to know which bank you are in for this to work properly. this information is in the PSW Special Function Register (SFR)
 
Last edited:

@android

Joined Dec 15, 2011
178
Hi

I have just tried to write my first assembly program for 8051 and sadly it's all errors! It seems one can't copy value from one Rx register to some other Rx register. Is this correct? The book I use didn't mention this. I wonder why.
Great! So you have started to get your hands dirty(I'd rather say your message window of compiler with ERRORS) :D Now you are there with the most important 'WHY'?? :confused:
Listen, you have started to get hands on & I appreciate it, I mean that's how we learn! We make mistake & we wonder what went wrong?? :rolleyes: That is the moment when you really become enthusiastic about any subject. You wonder & you wanna know 'WHY'?

Albeit above stuff is not solving your query, may be this will help to do so. Here it is, first of all don't worry about mugging up the instruction set, just get familiar with the 'ADDRESSING MODES' of 8051. Once you get comfortable with how to address internal registers & memory I bet you'll not be facing syntax errors for general programs. I guess you are using MAZIDI book. Look into the index you may find the titke relating to the addressing mechanisms of 8051. Jump onto that chapter if you are familiar with the internal architecture, registers & memory map of 8051; if not then first learn above things then get onto that chapter.

Overall the addressing modes are easy....most of the time you'll be using accumulator because many instructions demands use of it. And it is also easier if you work with ACC.

Anyways good luck with 8051! I like this uC(may be it is because its the first one I learned in detail).

Hope that helped. :)
 

Thread Starter

PG1995

Joined Apr 15, 2011
832
Refer to the 8051 instruction set. There isn't a MOV Rn,Rn instruction. However, since the registers can be addressed by its direct addresses, you can use MOV Direct,Direct which is a valid instruction.
Yes, there is no such instruction given in the instruction set appendix. I have noticed that the instruction set doesn't list "DB" anywhere. Isn't "DB" (define byte) also one of the instructions? If it is, then why doesn't the set mention it? Please let me know. Thank you.

Regards
PG
 

MrChips

Joined Oct 2, 2009
35,177
DB is not a microprocessor instruction.
It is called an assembler directive. It gives instructions to the assembler. It is not an instruction that will be executed by the microprocessor.

As ORG and END, it will be listed in the assembler documentation.
 

BillO

Joined Nov 24, 2008
1,001
There is the 0th time as well. Any given 8 bit location can be in 256 different states relating to the decimal numbers 0-255.

It is a common error for new students to ignore the state where all bits are logic 0, but it is just as valid as any other state.

As for the other query, I'm not sure I understand your question. The ORG statement causes the assembler to begin locating code to execute at the address given as the argument to the ORG statement. In this case 0000h. Once the processor reads the first instruction at 0000h, it will increment the the PC accordingly, depending on the instruction (1, 2 or 3 byte). So, in this case it sees that the first instruction is a two byte instruction, then it increments the PC to point at the 3rd byte (0002h), then it executes the rest of the first instruction, which is to load R0 with 00h.
 
Last edited:

Thread Starter

PG1995

Joined Apr 15, 2011
832
Thank you for the help.

BillO said:
There is the 0th time as well. Any given 8 bit location can be in 256 different states relating to the decimal numbers 0-255.

It is a common error for new students to ignore the state where all bits are logic 0, but it is just as valid as any other state.
I'm still confused about this. I still think that the loop runs only 255 times, not 256. For an 8-bit register we have 0-255 different states.

Rich (BB code):
MOV R2, 255D
MOV R0, #0
Repeat:
INC R0
ADD A, R0
DJNZ R2, Repeat
When the loop started, R2 has 255 stored and at the end of first cycle, the value has decreased to 254 and so on.

255 ---> 254 (1st cycle)
254 ---> 253 (2nd cycle)
253 ---> 252
252 ---> 251
251 ---> 250
...
0002 --> 001
001 ---> 000 (loop ends)

So, you see there are only 255 iterations. Do I make any sense, or am I still on the wrong path? Please let me know. Thank you.

Regards
PG
 

@android

Joined Dec 15, 2011
178
I'm still confused about this. I still think that the loop runs only 255 times, not 256. For an 8-bit register we have 0-255 different states.
Yes. The loop is repeated 256 times. 0-255 (both '0' & '255' included) comes out to be 256. You can simply write down numbers from '0' to '255' and count total numeric digits its 256 not 255. It seems that you ignoring the great '0'.
 

t06afre

Joined May 11, 2009
5,934
Then I was in school. I found out that simulators could be a great tool in understanding things both in the digital and analog world. So my question is. Do you use a 8051 simulator then working on your problems?
 

Thread Starter

PG1995

Joined Apr 15, 2011
832
Hi

@t06afre: Yes, I do use a simulator.

It seems I understand it now. Thank you.

Regards
PG
I'm sorry but I think I'm still confused. I remember that yesterday it did make sense to me that the loop will be repeated six times (assuming counter=5) but I have lost the track of that revelation.

Please see the attachment. Perhaps, you could tell me where the error lies in my understanding. Thank you.

Regards
PG
 

Attachments

BillO

Joined Nov 24, 2008
1,001
For DJNZ, yes, you are right. DJNZ tests for the zero condition immediately after the decrement, so this case will only run 5 times.

Consider these two code loops:

Rich (BB code):
REPEAT:
       [....]
       [loop processing code]
       [....]
       DJNZ I, REPEAT
And

Rich (BB code):
REPEAT:
       [....]
       [loop processing code]
       [....]
       JZ END
       DEC A
       AJMP REPEAT
END:
In the first case, if the start value of I=N, then [loop processing code] is executed N times.

In the second case, if the start value in the accumulator is N, then [loop processing code] is executed N+1 times.

So, to get the loop to run 256 times, you need to do the test for zero after the loop code, and the decrement after the test for zero.
 
Top