Mov r1, r0, mcu 8051

Discussion in 'Embedded Systems and Microcontrollers' started by PG1995, Mar 30, 2012.

  1. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    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.


    Code ( (Unknown Language)):
    1.  
    2. ;move some hex value, e.g. 89H to register A of CPU. Then from register A
    3. ;move it to all registers of RAM, i.e. R0 - R7.
    4.  
    5. ORG 0H        ;start at memory location 0H
    6.  
    7.     Mov A, #89H
    8.     Mov R0, A
    9.     Mov R1, R0
    10.     Mov R2, R1
    11.     Mov R3, R2
    12.     Mov R4, R3
    13.     Mov R5, R4
    14.     Mov R6, R5
    15.     Mov R7, R6
    16.  
    17. END        ;program ends here
    18.  
    Errors
    Code ( (Unknown Language)):
    1.  
    2. Compiling file: program1.asm
    3. Initializing pre-processor ...
    4. Syntax error at 9 in program1.asm: Invalid set of operands: mov R1,R0
    5. Syntax error at 10 in program1.asm: Invalid set of operands: mov R2,R1
    6. Syntax error at 11 in program1.asm: Invalid set of operands: mov R3,R2
    7. Syntax error at 12 in program1.asm: Invalid set of operands: mov R4,R3
    8. Syntax error at 13 in program1.asm: Invalid set of operands: mov R5,R4
    9. Syntax error at 14 in program1.asm: Invalid set of operands: mov R6,R5
    10. Syntax error at 15 in program1.asm: Invalid set of operands: mov R7,R6
    11. Pre-processing FAILED !
    12. Creating code listing file ...        -> "program1.lst"
    13. 7 errors, 0 warnings
    14.  
     
  2. jimkeith

    Active Member

    Oct 26, 2011
    539
    99
    Registers must be rotated through the accumulator
     
    PG1995 likes this.
  3. Arm_n_Legs

    Active Member

    Mar 7, 2007
    183
    10
    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.
     
    PG1995 likes this.
  4. Papabravo

    Expert

    Feb 24, 2006
    10,137
    1,786
    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: Apr 20, 2012
    PG1995 likes this.
  5. @android

    Member

    Dec 15, 2011
    178
    9
    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. :)
     
    PG1995 likes this.
  6. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    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
     
  7. MrChips

    Moderator

    Oct 2, 2009
    12,429
    3,360
    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.
     
    PG1995 likes this.
  8. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    Thanks a lot, MrChips.

    Could you please help me with the query given in the attachment? Thank you.

    Regards
    PG
     
  9. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    Sorry.

    I think I understand it now. FFH = 255D. Therefore, adding FFH+1=00000000 for an 8-bit register. Correct?

    Q1: Well, you can help me with this new query in the attachment.

    Q2: Please help me with the query.

    Regards
    PG
     
    Last edited: Apr 21, 2012
  10. BillO

    Well-Known Member

    Nov 24, 2008
    985
    136
    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: Apr 21, 2012
    PG1995 likes this.
  11. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    Thank you for the help.

    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.

    Code ( (Unknown Language)):
    1.  
    2. MOV R2, 255D
    3. MOV R0, #0
    4. Repeat:
    5. INC R0
    6. ADD A, R0
    7. DJNZ R2, Repeat
    8.  
    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
     
  12. @android

    Member

    Dec 15, 2011
    178
    9
    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'.
     
    PG1995 likes this.
  13. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    It seems I understand it now. Thank you.

    Regards
    PG
     
  14. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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?
     
  15. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    Hi

    @t06afre: Yes, I do use a simulator.

    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
     
    • IMG.jpg
      IMG.jpg
      File size:
      73.8 KB
      Views:
      23
  16. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    Would someone please comment on my previous post? Thank you.

    Regards
    PG
     
  17. @android

    Member

    Dec 15, 2011
    178
    9
    May be this will help. I found this in book. Yes your deduction is correct.
    djnz.JPG
     
    PG1995 likes this.
  18. BillO

    Well-Known Member

    Nov 24, 2008
    985
    136
    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:

    Code ( (Unknown Language)):
    1.  
    2.  
    3. REPEAT:
    4.        [....]
    5.        [loop processing code]
    6.        [....]
    7.        DJNZ I, REPEAT
    8.  
    9.  
    And

    Code ( (Unknown Language)):
    1.  
    2.  
    3. REPEAT:
    4.        [....]
    5.        [loop processing code]
    6.        [....]
    7.        JZ END
    8.        DEC A
    9.        AJMP REPEAT
    10. END:
    11.  
    12.  
    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.
     
    PG1995 likes this.
  19. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    Thank you, @android, Bill.

    I'm happy to know that I was correct. :)

    Regards
    PG
     
  20. Mark_T

    Member

    Feb 7, 2012
    47
    8
    Yes in second query the PC points to the opcodes not mneumonics. The opcodes are what the processor runs.
     
Loading...