Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added simple_calc/maths.o
Binary file not shown.
22 changes: 22 additions & 0 deletions simple_calc/maths.s
Original file line number Diff line number Diff line change
@@ -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
Binary file added simple_calc/simple_calc
Binary file not shown.
57 changes: 57 additions & 0 deletions simple_calc/simple_calc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <stdio.h>

// 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;
}
Loading