Sunday, December 25, 2016
Compute area of a circle using fldpi,fmul and fld instructions
1: //******************************************
2: //Note st0 refers to the top of
3: //the floating point stack.
4: //1. fldpi loads st0 with PI.
5: //2. flds or fldl loads st0 with the radius provided by the user
6: //flds is used for float and fldl is used if the radius is
7: //declared as double. This is required by the gnu assembler.
8: //3. (first fmul) multiply st0 with st0 this computes r*r
9: //4. (second fmul) multiply st0 with st1 this computes pi * r * r
10: //Then save the top of floating point stack(st0) into the variable area.
11: //In inline assembly, the "=t" means top of floating point stack and in
12: //this program it is mapped to area.
13: //******************************************
14: #include <stdio.h>
15: int main(int argc, char* argv[]){
16: double radius, area;
17: printf("Enter the radius\n");
18: scanf("%lf", &radius);
19: asm volatile(
20: "fldpi\n"
21: "fldl %1\n"
22: "fmul %%st(0),%%st(0)\n" //compute r ^ 2
23: "fmul %%st(1), %%st(0)\n" //compute pi * r^2
24: :"=t"(area)
25: :"m"(radius)
26: :
27: );
28: printf("The area is %lf\n",area);
29: return 0;
30: }
$ gcc -g -Fstabs area.c
$ ./a.out
Enter the radius
7.0
The area is 153.938040
Subscribe to Posts [Atom]
Post a Comment