user668074
user668074

Reputation: 1131

How can I read command line arguments using Raspberry Pi (ARM) Assembly?

I'm using the Raspberry Pi to learn ARM assembly. I'm still beginning but I would like to know how I can read the values of argv.

I figured out that the value of argc is held in register 0 (r0) at the beginning of a program, but I can't figure out where the argv value is stored. I assume it's somewhere in the stack, but I'm not able to find it.

Can someone help with this?


Edit: So the answer I marked as correct allowed me to find the values of argv. To summarize, the value of argc is found in register r0 when the program launches. r1 contains an address to an array of addresses. These addresses point to the relevant values of argv.

Upvotes: 1

Views: 1327

Answers (1)

Dmitry Grigoryev
Dmitry Grigoryev

Reputation: 3204

According to ARM ABI, the second primitive argument to a function should be stored in R1. Bear in mind that argv is a char**, i.e. an address pointing to a table of argc pointers. The first pointer in this table points to a NULL-terminated string containing the executable name, and following pointers point to individual arguments, also represented as NULL-terminated strings.

In case I'm mistaken, just build a simple program accessing argv and see where it takes it from:

int main (int argc, char *argv[])
{
  return argv[0][0];
}

Note that while you're using assembler, gcc will still include startup code from standard library in your program, defining the _start symbol for you and making C-style main function the entry point, so the above still applies to you. However, since you're learning assembler, you may be interested in getting rid of standard library completely, which is done with gcc -nostdlib. Note that you will need to provide your own code for _exit() function to be able to terminate your program properly. See this tutorial for a detailed walk-through.

Upvotes: 3

Related Questions