assemblyarmstrlen

loop to count length of string Assembly ARM


I have a string, and I want to loop it to count the length of the string.

main:
    MOV R1, #0
    loop:
        CMP R1, #10 
        BGT loop
        ADD R1, #1
        B loop_end
    loop_end:

string: .asciz "Hello World\n" 

Solution

  • The code is comparing R1 to ten and branches back to loop if R1 is more than ten. Else, it increments R1 by 1 and branches (unconditionally) to the end of the loop. This is not right. Also, it is not clear why the code is comparing R1 to ten. If you need the code to work for any length of string, then it should check for the null character at the end of the string.

    Another thing, string is a keyword/directive name in ARM. So call the string something else like foo, for example.

    The logic should look like this: Get a character, if null, branch to end, else increment pointer (to get the next character) and loop for another iteration.

    Depending on the version of ARM you have, it could look something like this:

    foo: .asciz "Hello World\n"
    mov R3, #0 //setup a counter
    ldr R1, =[foo] //R1 has the address of the string
    loop:
    ldrb R2, [R1] //get a byte from that address
    cmp  R2, #0 //compare the byte value to the null 
    character
    beq  loop_end //branch to end of loop if true
    //otherwise
    add R1, #1 //increment the pointer
    add R3, #1 //increment counter
    b loop
    loop_end:
    //rest of the code
    

    Here is a link to a video that shows you how to count the length of a string in ARM: Counting characters in a string