diff --git a/simple_calc/maths.o b/simple_calc/maths.o new file mode 100644 index 00000000..24467ec6 Binary files /dev/null and b/simple_calc/maths.o differ diff --git a/simple_calc/maths.s b/simple_calc/maths.s new file mode 100644 index 00000000..7c803a5d --- /dev/null +++ b/simple_calc/maths.s @@ -0,0 +1,22 @@ +.section .text # This tells that the text below is code + + .global add # add is defined as global + #xmm registers are used for SIMD and floating points + add: + addss %xmm1, %xmm0 #xmm0=xmm0+xmm1 + ret + + .global sub + sub: + subss %xmm1, %xmm0 #xmm0=xmm0-xmm1 + ret + + .global mul + mul: + mulss %xmm1,%xmm0 #xmm0=xmm0*xmm1 + ret + + .global fdiv #we use fdiv so that we dont get issue of name conflict with stdlib.h in c + fdiv: + divss %xmm1, %xmm0 #xmm0=xmm0/xmm1 + ret#end of the code \ No newline at end of file diff --git a/simple_calc/simple_calc b/simple_calc/simple_calc new file mode 100755 index 00000000..a2f0178f Binary files /dev/null and b/simple_calc/simple_calc differ diff --git a/simple_calc/simple_calc.c b/simple_calc/simple_calc.c new file mode 100644 index 00000000..0f4b1439 --- /dev/null +++ b/simple_calc/simple_calc.c @@ -0,0 +1,57 @@ +#include + +// float functions from assembly +extern float add(float a, float b); +extern float sub(float a, float b); +extern float mul(float a, float b); +extern float fdiv(float a, float b); + +int main() { + float x, y, result; + int choice; + + while (1) { + printf("\nChoose operation:\n"); + printf("1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Exit\n"); + printf("Enter choice: "); + scanf("%d", &choice); + + if (choice == 5) { + printf("Exiting calculator. Goodbye!\n"); + break; + } + + printf("Enter x: "); + scanf("%f", &x); + printf("Enter y: "); + scanf("%f", &y); + + switch (choice) { + case 1: + result = add(x, y); + printf("Sum of %.2f & %.2f is %.2f\n", x, y, result); + break; + case 2: + result = sub(x, y); + printf("Difference of %.2f & %.2f is %.2f\n", x, y, result); + break; + case 3: + result = mul(x, y); + printf("Product of %.2f & %.2f is %.2f\n", x, y, result); + break; + case 4: + if (y == 0.0f) { + printf("Error: Division by zero!\n"); + break; + } + result = fdiv(x, y); + printf("Division of %.2f / %.2f is %.2f\n", x, y, result); + break; + default: + printf("Invalid choice! Please select 1-5.\n"); + break; + } + } + + return 0; +} \ No newline at end of file