Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mixing C & Assembly.

Similar presentations


Presentation on theme: "Mixing C & Assembly."— Presentation transcript:

1 Mixing C & Assembly

2 Topic Calling assembly functions from C/C++ Calling C from assembly
Using Cstdlib from assembly Writing inline assembly

3 Name Mangling C++ tranforms function names on compiling
Support for overloaded functions

4 Name Mangling C does not support overloading
does not mangle names Can tell compiler to use C function naming extern "C"

5 Calling ASM from C/C++ Assembly function must obey standard calling conventions Preserve r4+ Take parameters in r0-r3 Return via r0 In .s: ASM function must be declared as .global In .cpp ASM function declared in c/c++ as extern “C”

6 Calling ASM from C/C++ ASM file:

7 Calling ASM from C/C++ C++ file:

8 Building Build asm and .cpp seperately
-c flag just compiles and does not link -masm makes compiler use 32 bit instructions Let g++ do the final linking as end product is a c++ program Build asm as symbolCount.s -g -o symbolCount.o Build c++ g++ useSymbolCount.cpp -g -c -marm -o useSymbolCount.o Link object files g++ useSymbolCount.o symbolCount.o -g -o useSymbolCount.exe

9 Debugging In gdb : new tricks
step is one instruction at current level C++ or assembly stepi always is at assembly instruction level next / nexti step but pass over function calls (step over) display/10i $pc – 20 at every step, display 10 instructions starting from current value of the PC - 20 bytes (5 instructions before PC)

10 Calling C/C++ from ASM Call C/C++ using standard convention In .cpp:
Function may destroy r0-r3 - save if needed Pass parameters in r0-r3 Get return value from r0 In .cpp: C++ function declared as extern "C" In .s: C++ function defined as .extern

11 Calling C/C++ from ASM C++ file:

12 Calling C/C++ from ASM ASM file:

13 Building Build cpp file and asm file Can link with ld as thi
Build c++ g++ simple.cpp -c -g -o simple.o Build asm as useSimple.s -g -o useSimple.o Link ld useSimple.o simple.o -g -o something.exe

14 Using CStdlib C stdlib defines lots of useful functions
Can call like other C functions Declare as .extern in asm Use standard calling conventions

15 Printf Printf prints formatted strings:
As many extra parameters as format codes

16 Printf Format Codes:

17 Scanf Scanf reads information into variables:

18 Reading Safely Prevent overflow
scanf("%s", myCString) potentially unsafe fgets(myCString, size, stdin) only reads in size -1 chars Prevent overflow

19 Using CStdlib Sample 1 : isEven printf(prompt) scanf("%i", &inputNum)

20 Using CStdlib Sample 2 : rollD20 r0 = time(0) srand(r0) rand()
printf("You rolled %i\n", r6)

21 Cstdlib Entry/Exit C stdlib functions require special setup
C stdlib must be the start/end point It defines _start Looks in your code for global symbol main End program by branching to exit

22 Using CStdlib main is starting point for program using cstdlib
End program by branching to exit

23 Building If using clibs, let gcc or g++ do linking:
Build as rollD20.s -g -o rollD20.o Link gcc rollD20.o -g -o rollD20.exe

24 Inline ASM Define chunk of assembly inside c++ Specify
Registers used to transfer information in and out Registers you clobber


Download ppt "Mixing C & Assembly."

Similar presentations


Ads by Google