Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work with Strings in ARM?

Tags:

c

assembly

arm

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.

like image 543
IAE Avatar asked Jun 20 '10 11:06

IAE


People also ask

How are strings stored in registers?

A string is stored as consecutive characters in memory. If it's ASCII (not UTF-8), each character is a single byte. So you can access them one at a time with byte loads/stores, like movzbl 2(%rsi), %eax to get the 3rd character, if rsi points to the start of the string.

What is ARM LDRB?

Load Register Byte (register) calculates an address from a base register value and an offset register value, loads a byte from memory, zero-extends it to form a 32-bit word, and writes it to a register. The offset register value can optionally be shifted.


1 Answers

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.

like image 109
Greg Hewgill Avatar answered Oct 22 '22 21:10

Greg Hewgill