diff --git a/asm2/example_c_asm_and_coproc/compile.sh b/asm2/example_c_asm_and_coproc/compile.sh index d64273a..6ab2d2f 100644 --- a/asm2/example_c_asm_and_coproc/compile.sh +++ b/asm2/example_c_asm_and_coproc/compile.sh @@ -1 +1 @@ -nasm sum.asm -f elf && gcc main.c sum.o -m32 +nasm utils.asm -f elf && gcc main.c utils.o -m32 diff --git a/asm2/example_c_asm_and_coproc/main.c b/asm2/example_c_asm_and_coproc/main.c index 8f3fd3a..33929ed 100644 --- a/asm2/example_c_asm_and_coproc/main.c +++ b/asm2/example_c_asm_and_coproc/main.c @@ -5,14 +5,14 @@ Copyright 2020 KoroLion (github.com/KoroLion) #include float sum(float a, float b); +float fx(float x); int main() { - float a, b; - scanf("%f %f", &a, &b); - - float c = sum(a, b); - + float c = sum(1.5, 2.1); printf("%f\n", c); + float y = fx(2); + printf("%f\n", y); + return 0; } diff --git a/asm2/example_c_asm_and_coproc/sum.asm b/asm2/example_c_asm_and_coproc/sum.asm deleted file mode 100644 index 007b43e..0000000 --- a/asm2/example_c_asm_and_coproc/sum.asm +++ /dev/null @@ -1,19 +0,0 @@ -; Copyright 2020 KoroLion (github.com/KoroLion) - -global sum - -section .data -section .bss - temp: resb 4 - -section .text - -; sum(x, y) -sum: - finit - fld dword [esp + 4] ; = x - fadd dword [esp + 8] ; += y - fst dword [temp] ; [temp] = st0 - - mov eax, [temp] - ret diff --git a/asm2/example_c_asm_and_coproc/utils.asm b/asm2/example_c_asm_and_coproc/utils.asm new file mode 100644 index 0000000..0bcefe0 --- /dev/null +++ b/asm2/example_c_asm_and_coproc/utils.asm @@ -0,0 +1,36 @@ +; Copyright 2020 KoroLion (github.com/KoroLion) + +global sum +global fx + +section .data +section .bss + temp: resb 4 + +section .text + +; float sum(float x, float y) +; return x + y +sum: + finit + + fld dword [esp + 4] ; st0 = x + fadd dword [esp + 8] ; st0 += y + + ret ; returns st0 to c + +; float fx(float x) +; return 1 / (x + 2) +fx: + ; st1 + fld dword [esp + 4] ; st1 = x + mov dword [temp], 2 + fiadd dword [temp] ; st1 += 2 + + ; st0 + mov dword [temp], 1 + fild dword [temp] ; st0 = 1 + + fdiv st0, st1 ; st0 /= st1 + + ret ; returns st0 to c