Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
6460 | siemargl | 1 | /* examples for interoperability with assembler |
2 | |||
3 | 1. Calling assembler code from .c : see in libc\math any .asm file |
||
4 | 2. Using inline assembler: see \include\kos32sys1.h and libc\math\fmod.c |
||
5 | - https://gcc.gnu.org/onlinedocs/gcc-4.8.5/gcc/Extended-Asm.html |
||
6 | - not all constraints from gcc are supported, no "f" or "t" for example |
||
7 | - not supported clobberring st registers, must manual add "fstp %%st" at end or similar |
||
8 | - need full suffixes for opcodes, fstpl but not fstp |
||
9 | |||
10 | 3. Calling c functions from .asm: see \libc\start\start.asm:99 |
||
11 | Remember: |
||
12 | - small ints always passed as int32, floating point params as 64-bit |
||
13 | - returned structs passed on stack with additional hidden 1st param |
||
14 | - c functions can use EAX, ECX, EDX without warnings |
||
15 | - .c default is cdecl calling convention https://en.wikipedia.org/wiki/X86_calling_conventions |
||
16 | - dont use fastcall calling convention, tinycc realized it non-conformant way |
||
17 | - tinycc supports only ELF object files |
||
18 | |||
19 | tcc can be used as a linker |
||
20 | */ |
||
21 | |||
22 | |||
23 | |||
24 | |||
25 | #include |
||
26 | #include |
||
27 | |||
28 | |||
29 | main() |
||
30 | { |
||
31 | int i; |
||
32 | for (i = 0; i < 20; i++) |
||
33 | { |
||
34 | printf("------------------------------------------------------\n"); |
||
35 | printf ( "remainder of 5.3 / 2 is %f\n", remainder (5.3,2) ); |
||
36 | printf ( "remainder of 18.5 / 4.2 is %f\n", remainder (18.5,4.2) ); |
||
37 | //remainder of 5.3 / 2 is -0.700000 |
||
38 | //remainder of 18.5 / 4.2 is 1.700000 |
||
39 | |||
40 | printf ( "fmod of 5.3 / 2 is %f\n", fmod (5.3,2) ); |
||
41 | printf ( "fmod of 18.5 / 4.2 is %f\n", fmod (18.5,4.2) ); |
||
42 | // fmod of 5.3 / 2 is 1.300000 |
||
43 | // fmod of 18.5 / 4.2 is 1.700000 |
||
44 | |||
45 | double param, fractpart, intpart, result; |
||
46 | int n; |
||
47 | |||
48 | param = 3.14159265; |
||
49 | fractpart = modf (param , &intpart); |
||
50 | printf ("%f = %f + %f \n", param, intpart, fractpart); |
||
51 | //3.141593 = 3.000000 + 0.141593 |
||
52 | |||
53 | param = 0.95; |
||
54 | n = 4; |
||
55 | result = ldexp (param , n); |
||
56 | printf ("%f * 2^%d = %f\n", param, n, result); |
||
57 | //0.950000 * 2^4 = 15.200000 |
||
58 | |||
59 | param = 8.0; |
||
60 | result = frexp (param , &n); |
||
61 | printf ("%f = %f * 2^%d\n", param, result, n); |
||
62 | //8.000000 = 0.500000 * 2^4 |
||
63 | param = 50; |
||
64 | result = frexp (param , &n); |
||
65 | printf ("%f = %f * 2^%d\n", param, result, n); |
||
66 | |||
67 | } |
||
68 | }> |