Argument Passing in C

Programming, for all ages and all languages.
Post Reply
User avatar
samoz
Member
Member
Posts: 59
Joined: Sun Jun 01, 2008 1:16 pm

Argument Passing in C

Post 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!
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: Argument Passing in C

Post by DeletedAccount »

Obviously its the address #-o .
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: Argument Passing in C

Post 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'
User avatar
samoz
Member
Member
Posts: 59
Joined: Sun Jun 01, 2008 1:16 pm

Re: Argument Passing in C

Post 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?
Hexciting: An open source hex editor for the command line.
https://sourceforge.net/projects/hexciting/
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Argument Passing in C

Post 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.
Last edited by ru2aqare on Mon Nov 10, 2008 6:08 am, edited 1 time in total.
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: Argument Passing in C

Post by DeletedAccount »

C uses row - major addressing :idea: .

Regards
Sandeep
Post Reply