I have to convert the following code into assembly language:
void rotN1(char *str, int n)
{ // NB: str will be modified in-place
char *p;
for (p = str; *p != '\0'; p++)
{
int currChar = (int)*p;
if (currChar >= 'a' && currChar <= 'z')
{
currChar = currChar + n;
if (currChar > 'z')
{
currChar = currChar - 26;
}
*p = (char)currChar;
}
}
}
It's basically using a Caesar cipher on a given string (str), and n is the encryption key.
.data
array: .string %[str]
.equ len.array,.-array
.align
.text
.global main
main:
nop
ldr r2,=array // pointer
MOV r0, #0 // initialise loop index to 0
MOV r1, #len.array // number of elements
MOV r4, %[n]
Loop:
ldrb r3, [r2, r0]
mov r6, #
B check_A
B check_Z
ADD r3, r3, r4
ADD r0, r0, #1 //increment loop index
CMP r0, r1
BLE Loop
_exit:
mov r7, #1
svc 0
check_A:
CMP r3, #97
BLT _exit
check_Z:
CMP r3, #122
BGT _exit
I haven't finished the code but while traversing the array I need to get the ASCII value of that character in each iteration. Is there a way to do that? I have to do this as inline ARM in C.
"...I need to get the ASCII value of that character in each iteration."
You already have them.
ASCII characters each have a value as shown in the link table. Referring to them with their decimal value, characters AZ
range from 65
- 90
. az
range from 97
- 122
. So, in an array such as:
char msg[] = "Hello";
msg[0]
has a value of 72
,
msg[4]
has a value of 111
,
msg[5]
has a value of 0
.
Note, the last conversion is because msg
is initialized with a string literal, and string literals in C
are defined as a char
array terminated with a NULL
terminator. Note that not all char
arrays are required be NULL
terminated, in which case that array would be a simple char
array, not a C
string.
Also, as you display in your post, an ASCII char surrounded by single quotes represents the decimal value of that character. eg in the expression
char aitch = 'H';
aitch
now has the value 72.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.