views:

77

answers:

1

This is a homework question. Frankly, I'm not sure how a C program delivers a string parameter to the assembly level.

I have the function

StringSearchInString( text, searchString);

and the parameters

text = "Hallo Alles klar"

searchString = "ll"

I know ARM delivers the parameters into register R0, R1 respectively for text, searchString, but I'm not sure how this works with charactesr. If each character is 8 bits in length, then the register is mercilessly slaughtered by the incoming string.

I have since read that the ARM APCS converts the arguments as words, of which the first 4 bytes are stored in the register and the rest are loaded in reverse order on the stack.

Sooo... what? I'm not understanding this. The string text would be stored in R0, the first four bytes, "Hall" are stored in R0, and the rest in reverse order on the stack? Am I understanding that right? How do I call them?

TL;DR: How do I pass a string argument from a C-Program into assembly and how do I work/load/do stuff with it?

ANSWER:

In the remote case that anybody is looking for a solution to this as well, here it is:

As Greg Hewgill has said, strings are passed as a pointer to the string. Therefore, the value in R0 is an address to the string. You therefore use indirect addressing to access the value like so:

StringSearchInString( text, searchString ); // calls the ARM function...

//Going into the ARM function...

LDRB R4, [R0], #1 // Load the first value of R0 into R4 and skip 
                  // ahead one character(8 bits)
                  // Note the "B" in LDR. It indicates that you load ONLY 1 byte!
MOV R0, R4        // Move the value of R4 into R0. This destroys the pointer
                  // Stored in R0! Careful!

And success! If your string is "hallo Alles klar" like mine, you will have 0x68 loaded into register R0. This is the ASCII value of "h". From this you should be able to start working with strings.

+5  A: 

The short answer is that in C, strings are passed as a pointer to the character data somewhere else. For example, R0 might contain the value 0x01000078, which would be interpreted as a pointer to the "Hallo Alles klar" data in memory, followed by a null character (00 byte).. This is not unique to ARM.

Greg Hewgill
Figured that, but how do I work with it? I've tried loading the register, which doesn't make sense. I tried pointing to it with `ADR`. but the compiler complained. So I'm not sure what else to do.
SoulBeaver
@SoulBeaver: I'm afraid my ARM assembly is rusty because I worked with it only briefly a few years ago, but generally you would load the pointer into a register and then use an indirect addressing mode to read the actual characters out of memory (one at a time, usually).
Greg Hewgill
Thank you! With your little insight I was able to figure out what to do. Hopefully the rest should be a breeze! Updated my question and making you my suggested, and so far only, answer.
SoulBeaver