Page 1 of 1
Argument Passing in C
Posted: Sat Nov 08, 2008 11:09 am
by samoz
Hey guys, I've got a question.
I have a function in C, matmulss, that passes three float[][] as arguments, like so:
float[][] A, B, C
matmulss(A,B,C)
matmulss is a function I'm writing in x86-64 assembly, using GAS.
My question is, how are these arguments passed? Is it passing the A[0][0], B[0][0], C[0][0], to my assembly or is it passing memory addresses?
I don't see why it would be memory addresses since it's not a pointer though...
Any help?
Thanks!
Re: Argument Passing in C
Posted: Sat Nov 08, 2008 11:22 am
by DeletedAccount
Obviously its the address
.
Re: Argument Passing in C
Posted: Sat Nov 08, 2008 12:20 pm
by CodeCat
The C standard says that if an array name is used in an expression without an index following it, its value becomes to a pointer to the first element in the array. That's why you can pass strings to a char* easily. It also lets you do stuff like this:
Code: Select all
const char* a = "Hi!"; // Creates a pointer that points to the 'H'
Re: Argument Passing in C
Posted: Sat Nov 08, 2008 12:40 pm
by samoz
Alright, thanks!
Let me ask you another question then.
I'm trying to do some multiplication using the xmm registers.
However, when I code:
movss (%rdi), %xmm0
I get a segmentation fault...
I'm trying to load the value of A[0][0] (which I'm assuming is being given to me as an address by the argument) into the xmm0 register.
Any idea how to fix this?
Re: Argument Passing in C
Posted: Sat Nov 08, 2008 2:24 pm
by ru2aqare
samoz wrote:
I'm trying to load the value of A[0][0] (which I'm assuming is being given to me as an address by the argument) into the xmm0 register.
The variable A is a pointer which points to an array of pointers. You need
Code: Select all
mov rax, A ; could be mov rax, [rsp + 16] or mov rax, rcx or whatever the calling convention is
; rax now points to an array of pointers
mov rax, [rax +0] ; load pointer to first row
mov xmm0, [rax +0] ; load first element in array
to access the first element in the array. To access the third element in the second row, you should write
Code: Select all
mov rax, A ; could be mov rax, [rsp + 16] or mov rax, rcx or whatever the calling convention is
; rax now points to an array of pointers
mov rax, [rax +8] ; load pointer to second
mov xmm0, [rax +8*2] ; load third element in array
Edit: I don't know what calling convention is used by GCC, but msvc uses register-based calling convention. The first four arguments are passed in rcx, rdx, r8 and r9, and other arguments are passed on the stack. However, so called "home locations" are reserved for the first four arguments as well, allowing them to be written back to the stack.
Re: Argument Passing in C
Posted: Mon Nov 10, 2008 3:25 am
by DeletedAccount
C uses row - major addressing
.
Regards
Sandeep