0

I'm currently studying ARM assembly with R pi I'm doing "calling convention" between C and ARM. In "main.s", the program takes 3 user inputs and passes them to a function in function.c, and this function will return char * output. Finally, the program will print the output.

The question is, in this case, what the type of char * output looks like in ARM. In my understand, in ARM, the function will return the output into 'r0'. However, when I print out this 'r0', it shows strange characters...

In my main.s,

ldr r0, =string
ldr r1, =sindex
ldr r2, =eindex
bl sub_string

ldr r1, =s_string
str r0, [r1]

@print substring
ldr r0, =result @"The substring of the given string is '%s'
ldr r1, =s_string
bl printf

pop {ip, pc}

and the result is

The substring of the given string is 'Strange character'
  • please provide an example, with results and what you expected it to do. – old_timer Jul 16 '18 at 01:27
  • A `char *` is a *pointer*. You should be printing out what's in the address stored in `r0`. – Daniel H Jul 16 '18 at 01:39
  • @user202729 i used printf – Whiskey Mental Disturb Jul 16 '18 at 01:43
  • @DanielH i jot down some parts of my code. any wrong with my code? – Whiskey Mental Disturb Jul 16 '18 at 01:43
  • I don't know ARM assembly, but I suspect that because the return value `r0` of `sub_string` is already a `char*`, you should pass it to `printf %s` instead of `s_string`. – user202729 Jul 16 '18 at 01:47
  • (also do you need to free the memory later?) – user202729 Jul 16 '18 at 01:47
  • Possible duplicate of [How to use "printf" in raspberry pi assembly language](https://stackoverflow.com/questions/40442133/how-to-use-printf-in-raspberry-pi-assembly-language) – paulsm4 Jul 16 '18 at 01:47
  • I don't know ARM assembly and am just looking things up and making possibly-wrong guesses based on x86 assembly, so take this with a grain of salt, but it *looks* like you're storing the value of `r0` (which, remember, is a pointer) at the memory location indicated by `r1` (which is also a pointer), and then trying to print the string pointed to by `r1`. Because of the store, the first four bytes of the string will not be readable characters but instead the address which was in `r0`. – Daniel H Jul 16 '18 at 02:10

0 Answers0