Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access command line arguments (argv and argc) in AAarch64/Darwin assembly #22

Closed
jrosengarden opened this issue Aug 27, 2020 · 3 comments

Comments

@jrosengarden
Copy link

I'm hoping you can possibly answer this question???

I've been working thru the book and your code changes in order for everything to work on the Machine We Must Not Speak About.

I'm trying to extend one of the Chapter 4 exercises and access the command line argument(s) (argc & argv) within my assembly code. I've searched high and low and just can't find anything that applies (or works).

Could you possibly provide any clues/hints on how to access the command line arguments (argv and argc) within the assembly program.

This very simple program is working fine but if you read the TODO, in the attached code, what I'm trying to do should make sense.

case.txt

Thanks much!

@below
Copy link
Owner

below commented Aug 27, 2020

Gladly, it is quite easy. This is the entry function:

int main (int argc, const char * argv[])

As per the standard ARM64 ABI, the arguments are in the registers:
x0 holds argc
x1 holds argv

Let's invoke our executable with lldb HelloSilicon foo, and look at the register when our _start routine is called:

(lldb) re r sp x0 x1
      sp = 0x000000016fdff970
      x0 = 0x0000000000000002
      x1 = 0x000000016fdff9a0

As you can see, x0 contains 2, and x1 points to a position 48 bytes above the current SP (and we can find the value for argc at 40 bytes, too).

When we examine the memory pointed to at x1, we see the elements of argv, which are in turn pointers:

(lldb) m read -fx -c32 0x000000016fdff9a0
0x16fdff9a0: 0x6fdffb20 0x00000001 0x6fdffb53 0x00000001

The memory pointed to by the doubleword at x1 contains, as expected, the full path to the executable:

(lldb) m read -fc -c51 0x000000016fdffb20
0x16fdffb20: /Users/below/dev/HelloSilicon/Ch
0x16fdffb40: apter 1/HelloWorld\0

And finally, the doubleword at [x1, #8] points to our argument, foo:

(lldb) m read -fc -c4 0x000000016fdffb53
0x16fdffb53: foo\0

With this information, it should be easy to adopt your code! Let me know if it works!

@below below closed this as completed Aug 27, 2020
@jrosengarden
Copy link
Author

Thank-you VERY, VERY, VERY much. That worked perfectly and I've modified my program to now grab the command line argument(s) entered and act on them accordingly.

Again....thanks tons. Much appreciated!!!!!!

@below
Copy link
Owner

below commented Aug 28, 2020

Please see my adaption of your code in Chapter 4/case.s and feel free to add or edit it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants