Go to main content

SPARC Assembly Language Reference Manual

Exit Print View

Updated: October 2017
 
 

6.1 Anatomy of a C Function

A good place to start is with a simple C function:

int add(int a, int b)
{
     return a + b;
}

Compiling with the C compiler's –S option generates the assembler code:

demo% cc -O add.c -S
demo% cat add.s

! --------BEGIN PROLOG ------------
        .section        ".text",#alloc,#execinstr,#progbits
        .file   "add.c"

        .section        ".bss",#alloc,#write,#nobits

Bbss.bss:

        .section        ".data",#alloc,#write,#progbits

Ddata.data:

        .section        ".rodata",#alloc,#progbits
!
! CONSTANT POOL
!

Drodata.rodata:

        .section        ".text",#alloc,#execinstr,#progbits
/* 000000          0 */         .align  4
/* 000000            */         .skip   16
/* 0x0010            */         .align  4
! FILE add.c

!    1                !int add(int a, int b)
!    2                !{

!
! SUBROUTINE add
!
! OFFSET    SOURCE LINE LABEL   INSTRUCTION

                        .global add

! ---END PROLOG --- BEGIN BODY -------

                        add:

!    3                !    return a + b;



                        .L900000105:
/* 000000          3 */         retl    ! Result =  %o0
/* 0x0004            */         add     %o0,%o1,%o0
! 
!  -------END BODY ------ BEGIN EPILOG -------
! 
/* 0x0008          0 */         .type   add,#function
/* 0x0008          0 */         .size   add,(.-add)


                        .L900000106:

        .section        ".text",#alloc,#execinstr,#progbits


                        .L900000107:

        .section        ".annotate",#progbits
/* 000000          0 */         .asciz  "anotate"
/* 0x0008          0 */         .half   6,0
/* 0x000c          0 */         .word   28
/* 0x0010          0 */         .half   0,8
/* 0x0014          0 */         .word   (.L900000107-0x18)
/* 0x0018          0 */         .word   24
/* 0x001c          0 */         .half   1,12
/* 0x0020          0 */         .word   .L900000105
/* 0x0024          0 */         .word   (.L900000106-.L900000105)
/* 0x0028          0 */         .word   1577472

! ----------------END EPILOG-------------

The purpose of the prolog is to put the body of the code into the correct context for the assembler to create an object file of executable code. The prolog creates the section where to code should go and defines the proper alignment. It also declares the entry point as global so that it can be called from another object.

The body of the function has a label for the function name and the instruction code for the function.

The epilog section declares the type and size of the function for compatibility with other tools.