x86-64-assembly icon indicating copy to clipboard operation
x86-64-assembly copied to clipboard

How do you take input in assembly?

Open de-engineer opened this issue 3 years ago • 1 comments

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

de-engineer avatar Feb 12 '22 10:02 de-engineer

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.

bergjohan avatar Feb 12 '22 13:02 bergjohan