Skip to content
Advertisement

Function arguments in x86-64 asm

So I have to do this project for school that consists of reading a file in brainfuck and interpret it as assembly. The code works if I save the file path as a String inside the .data section, but what I want to have it so it gets the file path as an argument when starting the code in the terminal.

I have tried popping 3 times(since the stack should be number of args/ addres of program/ first arg), and then setting %rdi to the value at the adress of the 3rd popped item, but it returns “./interpreter” instead of the file path

This is the code that works:

JavaScript

but what I want is:

JavaScript

and here’s how I have to compile and link(this part can’t be changed, that’s how my teacher wants me to do it):

JavaScript

Advertisement

Answer

The 64 bit calling convention passes first 6 function arguments in registers, so argc is in rdi and argv is in rsi. The first command line argument is argv[1] so to access that you need to load 8(%rsi):

JavaScript

JavaScript

Of course you should check argc to see if you received enough arguments.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement