问题
I´m working on a program in which the user enters his name, and the program should convert all lower case letters to upper case:
I am using the %s format to read the string:
.text
ldr r0,=msj
bl printf
ldr r0,=format
ldr r1,string
bl scanf
.data
.align 2
msj: .asciz "Enter you name: "
format: .asciz "%s"
string: .asciz ""
I have tried substracting 32 to each character but I think the strings are not in ascii numbers format.
Is there any way I can convert the entire word to Upper Case?
回答1:
This might work. I don't have any of my ARM material with me at the moment.
; call with address of string in 'R0'.
upperString:
1: ldrb r1,[r0],#1
tst r1 ; finished string with null terminator?
bxeq lr ; then done and return
cmp r1,#'a' ; less than a?
blo 1b ; then load next char.
cmp r1,#'z' ; greater than z?
bhi 1b ; then load next char.
; Value to upper case.
sub r1,r1,#('a' - 'A') ; subtract 32.
strb r1,[r0,#-1] ; put it back to memory.
b 1b ; next character.
At least it is a good starting point. This is like wallyk's code, except I have assumed a null-terminated string instead of a pascal type string. To call it,
ldr r0,=string
bl upperString
回答2:
This is the essential algorithm:
for (int idx = 0; idx < len; ++idx)
if (str [idx] >= 'A' && str [idx] <= 'Z')
str [idx] += 'a' - 'A';
It has several parts you don't have. Scanning the string character by character. Inspecting for an uppercase letter. Adding (not subtracting) the lowercase/uppercase offset.
Note that this won't work for Unicode in general.
来源:https://stackoverflow.com/questions/16599258/arm-assembly-rasperry-pi-converting-a-string-to-upper-case