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

How do you take input in assembly? #149

Open
de-engineer opened this issue Feb 12, 2022 · 1 comment
Open

How do you take input in assembly? #149

de-engineer opened this issue Feb 12, 2022 · 1 comment

Comments

@de-engineer
Copy link

i don't seem to find anyway to get the input from the tests that are taken.

@bergjohan
Copy link
Contributor

bergjohan commented Feb 12, 2022

If you have a look at the [exercise]_test.c file, you'll see a function declaration at the top of the file. This is the function that you'll have to implement in assembly.

For the hello-world exercise it looks like the following (https://github.com/exercism/x86-64-assembly/blob/main/exercises/practice/hello-world/hello_world_test.c#L5):

extern const char *hello(void);

In this case it takes no parameters (void), and returns a const char *.

In assembly, function arguments are passed in registers. Which registers depend on the ABI. For this track, we use the System V ABI. The register usage can be found on page 21 in https://uclibc.org/docs/psABI-x86_64.pdf.

So, the arguments are passed in the following order: rdi, rsi, rdx, rcx, r8, and r9. And the return value is passed in the rax register.

Let's use a simple example, a function taking two integers and returning an integer:
int fn(int a, int b);

If the test calls this function with fn(1, 2);, you'll find the integer 1 in the rdi register, and the integer 2 in the rsi register. To return the integer 5 you would move it into the rax register, e.g., mov rax, 5.

I hope this answers your question, let me know if anything is unclear.

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