This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

What does LDRB do here?

Hello.
I've been looking at a piece of code that I'm not entirely sure what it does.

    LDR r0, =str
    MOV r1, #0
loop
    LDRB r2, [r0], #1
    ADD r1, r1, #1
    CMP r2, #0
    BNE loop
    SUB r1, r1, #1
Data Segment Little Endian Format
    str DCB "mCQ5Gtq",0


From my understanding, DCB loads the string into a word of 32 bits, and each character has its own byte. Little Endian Format means the least significant byte is stored in the lower memory address. LDR makes r0 = the string. LDRB loads the least significant byte held in r0, into r2 offset by 1 bit.

1) Does the least significant byte start with m or q? i.e. would it be
<- 0000 0017 (ascii for q) or <- 0000 006D (ascii for m).

2) Does the LDRB operator load the least significant byte from r0 offset by 1 bit, into r2. If so, what would r2 be (just the first loop).

Thank you very much

Parents
  • Equivalent to

    char str[] = "mCQ5Gtq";
    uint8_t *r0 = str; // pointer is 32-bit wide
    uint32_t r1 = 0;
    
    do
    {
      r2 = *r0++; // LDRB r2, [r0], #1
      r1++;
    }while(r2); // while not NUL
    r1--;
    

    R0 is a 32-bit pointer, for a byte, it reads the byte into R2, and increments R0 by 1

Reply
  • Equivalent to

    char str[] = "mCQ5Gtq";
    uint8_t *r0 = str; // pointer is 32-bit wide
    uint32_t r1 = 0;
    
    do
    {
      r2 = *r0++; // LDRB r2, [r0], #1
      r1++;
    }while(r2); // while not NUL
    r1--;
    

    R0 is a 32-bit pointer, for a byte, it reads the byte into R2, and increments R0 by 1

Children
No data