Rick Dearman
Rick Dearman

Reputation: 386

Reversing String in ARM GAS Assembler returns NULL

I know I'm doing something stupid, but I can't figure out what. The functions return a null string, so I'm not copying each character. Entry into my reverse_string_func has the input string in R0. The example I have used a syntax which gas doesn't seem to support, so I modified it, but it isn't functional. I want to move along the array of char one at a time. Any assistance is greatly appreciated, I've stared at it so many times I'm going code blind.

EDIT: The input string is in R0 when it is passed to the function. The reverse_string is an empty buffer filled with 0's. What I'm attempting to do is load the location of the input string into R1, move through it (while loading into r3) until I find the terminating 0, then move to the copy loop. There I want to load the current location of R3 (end of the string) into the first position of R2 (the reverse string) then go backwards pulling the characters from R3 and putting them back into R2 in the reverse order. I assumed I was using R2 and not ignoring it?

        ldr r2, [r0], r3 /* start at end of string */
        add r3, #-1

I believe I'm pushing the value numbered R3 in the array held in R0 into R2? Is that not what is happening?

reverse_string_func:
        PUSH {ip, lr}
        LDR r1, =input_string           @ pointer to first string.
        LDR r2, =reverse_string         @ pointer to second string
        mov r3, #0
mv2end:  /* try to find the end of the string */
        ldr r1, [r1], #1
        add r3, #1
        cmp r1, #0
        bne mv2end
        /* found end of string, move on */
string_copy:
        ldr r2, [r0], r3 /* start at end of string */
        add r3, #-1
        cmp r3, #0
        bne string_copy /* move back for next character */

        POP {ip, pc}

EDIT2:

OK, so I've made changes to attempt to str the values I'm finding. I have single-stepped through everything and it all seems to be in the registers, but it isn't being stored into the character buffer in memory? I still don't understand what I'm doing wrong.

reverse_string_func:
            PUSH {ip, lr}
        LDR r1, =input_string           @ pointer to first string.
        LDR r4, =reverse_string         @ pointer to second string
        mov r3, #0
mv2end:  /* try to find the end of the string */
        ldrb r2, [r1, r3]
        add r3, #1
        cmp r2, #0
        bne mv2end
        /* found end of string, move on */
        add r3, #-2 /* on terminator, move back past terminator, and line feed */
string_copy:
        add r3, #-1
        ldrb r2, [r1, r3]
        strb r4, [r1, r3] @ <--- I believe I'm going wrong here, but how?
        cmp r3, #0
        bne string_copy /* move back for next character */
        POP {ip, pc}

As it stands this just returns a NULL string into the main program.

Upvotes: 0

Views: 248

Answers (2)

Rick Dearman
Rick Dearman

Reputation: 386

The problem I was having with the second edit was with the STRB mnemonic and the problem was I that was misunderstanding the direction of storing. The correct way is:

strb r2, [r4, r5]

Because you're storing the contents of register R2 into the memory location held in R4, by the offset R5.

Hopefully this might help someone else who comes here later. There is a good tutorial here: https://azeria-labs.com/memory-instructions-load-and-store-part-4/

Upvotes: 1

domen
domen

Reputation: 1908

You load current character into r2 register (and then ignore it), while it appears what you wanted is to store that character into string pointed to by r2.

Upvotes: 0

Related Questions