This is a C code of Caesar Cipher which takes a given text and an encryption key as its parameters. I have to convert this into ARM Inline Assembler.
void rotN(char *str, int n)
{
char *p;
for (p = str; *p != 0; p++)
{
int currChar = (int)*p;
if (currChar >= 97 && currChar <= 122)
{
currChar = currChar + n;
if (currChar > 122)
{
currChar = currChar - 26;
}
*p = (char)currChar;
}
if (currChar >= 65 && currChar <= 90)
{
currChar = currChar + n;
if (currChar > 90)
{
currChar = currChar - 26;
}
*p = (char)currChar;
}
}
}
Over here I've used 65, 90, 97, 122 and 0 as they're ASCII values of 'A', 'Z', 'a', 'z' and '\0'.
void rotN(char *str, int n){
asm volatile(
"str %[str], [%[str]]\n\t"
"mov r0, %[n]\n\t"
"mov r1, %[str]\n\t"
"mov r2, #0\n\t"
"1: ldrb r3, [r1, r2]\n\t"
"cmp r3, #0\n\t"
"beq 2f\n\t"
"cmp r3, #97\n\t"
"blo 2f\n\t"
"cmp r3, #122\n\t"
"bhi 2f\n\t"
"sub r3, r3, #26\n\t"
"b 1b\n\t"
"2: add r3, r3, r0\n\t"
"cmp r3, #122\n\t"
"bhi 2f\n\t"
"cmp r3, #97\n\t"
"blo 2f\n\t"
"sub r3, r3, #26\n\t"
"b 1b\n\t"
"2:\n\t"
"strb r3, [r1, r2]\n\t"
:
: [str] "r" (str), [n] "r" (n)
: "r0", "r1", "r2", "r3"
);
}
The code above is what I've done so far, but doesn't seem to work. What am i doing wrong?
Well, you could use the appropriate
gcccross compiler to generate the assembly code. I'm not sure if this satisfies your requirements though.Save the code into let's say
rot.cand run the following, replacinggccwith the appropriate cross compiler version for your ARM device:The above command also turns off optimizations (
-O0) which may be helpful.A
rot.swill be generated that has the entire assembly for the file. You won't need the whole file, but it may help you figure out where you went wrong in your manual porting effort.