SPARC Assembly - Parameter Passing

Tags: sparc, assembly lang

Introduction

The basic calling convention is same. The first six arguments of the caller are placed in the out registers %o0-%o5. The SPARC V9 ABI still uses a register window on a larger register file to make calling a function a cheap operation. Results are returned in %o0. Because all registers are now treated as 64-bit quantities, 64-bit values can now be passed in a single register, rather than a register pair.

Example Program - Passing 6 parameters to a procedure

We provide an example and provide analysis. Then, we provide compile and run instruction.

The program

Here is an example program demonstrating passing of 6 parameters to a procedure.

  .data
s0:     .asciz  "Input output passing example\n"
s1:     .asciz  "arg 1: %lld\narg 2: %lld\narg 3: %lld\narg 4: %lld\narg 5: %lld\n"
  .text
  .global main

main:
  save    %sp, -0xc0, %sp
  setx    s0, %l4, %o0
  call    printstring
  nop
  setx    s1, %l4, %o0
  or      %g0, 0x1, %o1
  or      %g0, 0x2, %o2
  or      %g0, 0x3, %o3
  or      %g0, 0x4, %o4
  or      %g0, 0x5, %o5
  call    printstring
  nop
  ret
  restore

printstring:
  save    %sp, -0xc0, %sp
  or      %g0, %i0, %o0
  or      %g0, %i1, %o1
  or      %g0, %i2, %o2
  or      %g0, %i3, %o3
  or      %g0, %i4, %o4
  or      %g0, %i5, %o5
  call    printf
  nop
  ret
  restore

In the program above, main procedure passes 6 parameters to printstring procedure. First parameter is the string containing the format specifier. Next 5 parameters are integer values. We use %lld as format specifier because we pass 64 bit values. All registers in 64 bit sparc is 64 bit quantitities as per SPARC V9 ABI.

SPARC is RISC architecture. We do not have native mov instruction which transfers value from register to register or loads immediate to register and so on. We have synthetic mov instructions though. Therefore, to do mov operation we use sparc native or instruction. Beccause the first oeprand to or we provide is %g0 which is hardwired zero the instruction is equivalent to a mov

Compile and Execution

We use studio compiler for example to compile our assembly program. Using gcc or other compiler is just fine.

$ cc -m64 -s  p04_Input_Output_parameter_passing.S -o p04_Input_Output_parameter_passing

Then we execute the binary,

$ ./p04_Input_Output_parameter_passing
Input output passing example
arg 1: 1
arg 2: 2
arg 3: 3
arg 4: 4
arg 5: 5

As for output analysis, the first string,

Input output passing example

is printed by the first call to printf and second part of the output that prints 5 arguments in seperate lines are done by secocnd call to printf

N.B: The example programs are not optimized for the hardware. They are simplified unoptimized versions for easy illustration.

Passing more than 6 parameters to a procedure

We use the stack to pass more than 6 parameters.

"reserved for the second program"

Returning value from a subroutine

Solaris 11.3 Manual - Functions Returning Values provides documentation on returning values from functions.

The program

Register %o0 is used to return value from a subroutine.

  .data
s0:     .asciz  "Return value: %lld\n"
  .text
  .global main

main:
  save    %sp, -0xc0, %sp
  call    printstring
  nop
  ret
  restore

printstring:
  save    %sp, -0xc0, %sp
  call    foo
  nop
  or      %g0, %o0, %o1
  setx    s0, %l4, %o0
  call    printf
  nop
  ret
  restore

foo:
  save    %sp, -0xc0, %sp
  or      %g0, 123, %i0
  ret
  restore

In the program above, subroutine foo returns value 123 using register %i0. The printstring subroutine retrieves this value using register %o0. This demonstrates just like the save instruction which renames all o registers to i registers restore instructions does the opposite: renames all i register to o registers.

Compile and Execution

We use studio compiler for example to compile our assembly program. Using gcc or other compiler is just fine.

$ suncc -m64 -s  p05_parameter_passing_return_value.S -o p05_parameter_passing_return_value

Following is what we get after executing the output binary,

$ ./p05_parameter_passing_return_value
Return value: 123

References

  1. Solaris 11.3 Manual - Parameter Passing
  2. SPARC V9 ABI
  3. SPARC Subroutine Calling Conventions
  4. Solaris 11.3 Manual - Functions Returning Values

No Comments

Add a Comment