I'd like to create a variadic function that sums its inputs. I've managed to write inline assembly for capturing arguments 2-6, but I'm struggling to retrieve the other arguments from the stack frame.
I'm trying to solve a coding exercise that teaches you to understand the stack frame and how arguments are accessed in assembly, which is why I'm not using va
.
I've been following the Linux x86-64 calling convention with information from this site. This has led to my current attempt:
int n_sum(int n, ...)
{
int sum = 0;
if (n > 0)
__asm__("add %%rsi, %0": "+m"(sum));
if (n > 1)
__asm__("add %%rdx, %0": "+m"(sum));
if (n > 2)
__asm__("add %%rcx, %0": "+m"(sum));
if (n > 3)
__asm__("add %%r8, %0": "+m"(sum));
if (n > 4)
__asm__("add %%r9, %0": "+m"(sum));
printf("%d\n", sum);
if (n > 5)
{
void *extra_args;
__asm__("mov %%rbp, %0" : "=r"(extra_args));
printf("%p\n", extra_args);
extra_args += 16;
printf("%d\n", extra_args);
}
return sum;
}
int main()
{
n_sum(10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
return 0;
}
My expectation is that the command: __asm__("mov %%rbp, %0" : "=r"(extra_args));
has moved the %rbp
's value to my extra_args
pointer. Therefore adding 16 to this value should move me to the 7th argument, stored in the stack frame. But I get the incorrect value when I try to print this as an integer.
I presume I'm not accessing the stack arguments correctly, but I don't know why! How can I fix this?