I'm learning assembly and, while trying to create a string uppercase conversion function, that receives a string and returns a UPPERCASEd string, I'm stuck on the following code:
asm_func3:
mov rbp, [rax] ; stores argument received in rbp
push rcx ; prepare rcx for use
xor rcx,rcx ; zero counter
_toupper:
cmp byte [rbp+rcx], 61h ; is lowercase ascii?
jl _toupper_next ; no, go to next char
cmp byte [rbp+rcx], 7Ah ; is lowercase ascii?
jg _toupper_next ; no, go to next char
sub byte [rbp+rcx], 20h ; subtract 32 to convert lowercase to uppercase
_toupper_next:
inc rcx ; next char
cmp byte [rbp+rcx], 0h ; is end of string?
jne _toupper ; start again
_toupper_end:
mov rax, rbp ; copy the result to return register
pop rcx ; release rcx
ret
I'm getting a SIGBUS on the line:
cmp byte [rbp+rcx], 61h ; is lowercase ascii?
I'm calling this function from a C prog as follows:
printf("%s", asm_func3("abcdef\n"));
I suspect I'm not handling RBP correctly but I exhausted all my information sources. Can someone help me?
Thank you all for the comments. After reviewing the documentation I managed to have the code below working. Any additional comments?
Right now I just need to figure out how to clear RSI, to remove any "garbage" from previous calls.