1

I have a C code that calls a function defined in ARM Assembly. Two Parameters have to be passed.

If the function call looks like this:

functionName(a, b)

the registers x0 and x1 hold these values in which order? Is it x0 holds a and x1 holds b or the other way round?

ThisaruG
  • 3,222
  • 7
  • 38
  • 60
user2524261
  • 109
  • 2
  • 10

1 Answers1

5

It took longer to ask the question than to just try it.

extern void bar ( unsigned int, unsigned int );

void foo ( void )
{
    bar(5,7);
}

compile then disassemble

traditional arm

00000000 <foo>:
   0:   e3a00005    mov r0, #5
   4:   e3a01007    mov r1, #7
   8:   eafffffe    b   0 <bar>

aarch64

0000000000000000 <foo>:
   0:   528000e1    mov w1, #0x7                    // #7
   4:   528000a0    mov w0, #0x5                    // #5
   8:   14000000    b   0 <bar>
   c:   d503201f    nop

msp430

00000000 <foo>:
   0:   3e 40 07 00     mov #7, r14 ;#0x0007
   4:   3f 40 05 00     mov #5, r15 ;#0x0005
   8:   b0 12 00 00     call    #0x0000 
   c:   30 41           ret         

pdp-11

00000000 <_foo>:
   0:   1166            mov r5, -(sp)
   2:   1185            mov sp, r5
   4:   15e6 0007       mov $7, -(sp)
   8:   15e6 0005       mov $5, -(sp)
   c:   09f7 fff0       jsr pc, 0 <_foo>
  10:   65c6 0004       add $4, sp
  14:   1585            mov (sp)+, r5
  16:   0087            rts pc
old_timer
  • 69,149
  • 8
  • 89
  • 168
  • Question looks like arm64? Nothing wrong with your answer though. – auselen Jan 08 '16 at 07:34
  • 3
    doesnt matter what processor, arm, aarch64, mips, x86, pdp-11. if you have a compiler for it and want to know the calling convention for two simple parameters. shouldnt take but around 40 to 50 seconds to find out. – old_timer Jan 08 '16 at 14:49
  • For future readers: https://godbolt.org/ has ARM, AArch64, MSP430, MIPS, and various other gcc and clang versions (including x86-64), and MSVC. – Peter Cordes Mar 10 '20 at 04:59
  • No. The specifications of calling conventions are described in the ABI for that platform. Writing a C program to have a look how it works can be a good way to check your understanding AFTER you read the ABI spec, or to complement it. There could be many cases where a simple test simply does not show them. Also, this website has a bigger function to be able to provide knowledge for future users, not just spoon feed the OP. Yours is not a bad answer, but why give an attitude... – Alessio Sangalli Jul 21 '23 at 20:01
  • @PeterCordes OK but any compiler can be configured to change the ABI even on the command line. Just because the default settings on godbolt show a specific way to pass a couple of parameters, it does not mean that in real life the calling convention will be exactly the same – Alessio Sangalli Jul 21 '23 at 20:25
  • @AlessioSangalli: If you're reverse-engineering, then it really is this simple to figure out which arg goes in which register; unless as you say you're dealing with ABI-changing args like x86 GCC's `-mregparm=3`. If you're writing your own functions in asm, then yeah you should read the ABI spec to see what other requirements exist like call preserved vs. call clobbered regs, stack alignment, some sequence of operations in the prologue that stack unwinding might depend on, etc. – Peter Cordes Jul 21 '23 at 20:36