In previous post Running NASM inside C inside GDB. Part 1. Integers we created a simple
asm function and called it from
We had only 3 arguments in that example. But what happens if we have more?
More numeric calculations
Let’s create another
C program that will invoke external function with 8 arguments:
Okay, we have more than 3 parameters now. How are those passed to the external
gcc -S num_calc_more.c would give us
num_calc_more.s to see how the arguments were handled:
We can easily cross-check this with the Calling Conventions to see that
gcc placed all arguments into registers, but also pushed last two (
n8 to the stack).
Ok, so if we are sure we would only run on this architecture with this compiler, it’s fine, we can just use registers only and forget about the stack. But let’s not do this for now, and use stack.
8 here, it is equal to the size of our operand:
See some examples here: Calling Convention Examples.
In order to use arguments, passed via stack, we should initialize the stack pointer:
; stdcall save stack pointer
mov rbp, rsp
; do your stuff
; and restore base pointer
pop rbp ; restore stack pointer
Now we can access our values as follows:
Let’s do something even more useful here:
n1 + n2 - n3 + n4 - n5 + n6 - n7 + n8
Test if it is workign:
Great, seems like it is doing something useless, but doing it right ;)
Continue reading here: Running NASM inside C inside GDB. Part 3. Debugging
Source code on github: nasm-c-gdb