Question: Need help implementing an ARM assembly function to take a string and reverse it based off of the following C code: void StringReverse(uint8_t str[]) {
Need help implementing an ARM assembly function to take a string and reverse it based off of the following C code:
void StringReverse(uint8_t str[])
{
uint8_t i=0;
uint8_t j=stringlen(str)-1;
uint8_t temp;
while(i
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j--;
}
}
Here is the current ARM assembly code for the string reversal I have so far:
AREA |.text|, CODE IMPORT stringlen EXPORT StringReverse StringReverse ;LDR r3, [r0]; # Copy input string to r3 PUSH {r4}; # Pushes r4 to stack PUSH {r5}; # Pushes r5 to stack PUSH {r6}; # Pushes r6 to stack PUSH {r7}; # Pushes r7 to stack PUSH {r8}; # Pushes r8 to stack PUSH {r9}; # Pushes r9 to stack MOV r5, r0; # Moves string to r5 BL stringlen; # Gets length of string MOV r4, r0; # Moves char count to r4 AND r0, r0, #0; # Clears r0 register AND r1, r1, #0; # Clears r1 for iteration count AND r6, r6, #0; # Clears r7 for temp char holder SUB r6, r4, #1; # Sets end iteration count loop1 CMP r4, r1; # Checks if iterations match char count BEQ loopend; # Ends loop if char count matches iteration count ; # Stuck on how to implement this part of the process ADD r1, r1, #1; # Increments iteration count SUB r6, r6, #1; # Decrements end iteration count B loop1; # Jumps back to top of loop loopend POP {r9}; # Returns previous contents back to registers from stack POP {r8}; POP {r7}; POP {r6}; POP {r5}; POP {r4}; BX r14; END
And here's my completed code for the stringlen function in ARM assembly:
AREA |.text|, CODE EXPORT stringlen
stringlen MOV r3, r0; # Moves input string to r3 AND r0, r0, #0; # Clears r0 register loop1 LDR r2, [r3], #1; # Loads next char into r2 CMP r2, #0; # Checks if char exists BEQ loopend; # Ends loop if no additional char ADD r0, r0, #1; # Increments char count B loop1; # Jumps back to top of loop loopend BX r14; END
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
