Andes Workshop

Mix C code and assembly code
Page 1 of 1

Author:  cindy [ Wed Oct 30, 2013 5:50 pm ]
Post subject:  Mix C code and assembly code

First, you could read Chapter 8 “Application Binary Interface (ABI)” in programming guide to get related information.

A C source code can be compiled to an assembly by gcc.
For example, there is a simple C code “simple.c”.
In cygwin console, I use gcc command:
$ nds32le-elf-gcc -O2 -S simple.c
Then it generate a simple.s file.
You can use gcc to view the assembly code.
(see attached

a. Assembly call an C function (see attached

Arguments will be passed in general-purpose registers (r0-r5).
For 4-byte primitive type, the return value is returned in r0.

In C code, we write:
int a=2,b=3,c=4, sum,number;

In Assembly, we write:
movi $r2, 4 ! Third argument:c
movi $r1, 3 ! Second argument: b
movi $r0, 2 ! First argument: a
bal simple ! bal is equal to jal, call function simple
addi $r1, $r0, 1 ! return value sum is put on $r0

b. C call a assembly function (see attached
In C code, we write:
int simple(int x, int y, int z)
int result;
return result;

In Assembly, we write:
add $r0, $r1, $r0
add $r0, $r0, $r2

These three arguments are passing in $r0, $1, $r2.
Return value is passing in $r0.

Download example codes:
Attachment: [24.44 KiB]
Downloaded 308 times

Attachment: [33.52 KiB]
Downloaded 324 times

The following files (main.c and simple.c) in these example are excluded from build.
5.gif [ 5.67 KiB | Viewed 4067 times ]

These two files (main.c and simple.c) are just for reference.

Here are the method to exclude some files:

Page 1 of 1 All times are UTC + 8 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group