An important value in calculus is the constant e. This constant can be calculate
ID: 3673879 • Letter: A
Question
An important value in calculus is the constant e. This constant can be calculated using iterative methods. One mathematical formula that can be used is as follows: e = sigma_n = 0^infinity (1/n!) = 1 + 1 + 1/2 + 1/6 + 1/24 +... It is not necessary to run for an infinite number of steps. In fact, only twelve iterations is sufficiently accurate for most applications. Your task is to write a MIPS32 program that calculates, then prints the constant e after 12 iterations of the algorithm shown above.Explanation / Answer
I have solved your problem by writing down a C code which is as follows:
#include<stdio.h>
float fact(int num)
{
if(num<=1)
num = 1;
else
{
num = num * fact(num-1);
}
return num;
}
int main()
{
float e=0;
int n=0;
for(n=0;n<12;n++)
{
e = e + 1/fact(n);
}
printf("The Value of e = %f ", e);
return 0;
}
Now, as per the question, this question can be easily modified by the programs to the MIPS program or you can use any compiler to covert it or even online. I have done it using the available compiler. [Code may produce errors when running in diff IDE. So focusing on the above code would be great and then convert it, in case of any error.]
.Ltext0:
.globl fact
fact:
.LFB0:
.cfi_startproc
0000 55 pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
0001 4889E5 movq %rsp, %rbp
.cfi_def_cfa_register 6
0004 4883EC10 subq $16, %rsp
0008 897DFC movl %edi, -4(%rbp)
000b 837DFC01 cmpl $1, -4(%rbp)
000f 7F09 jg .L2
0011 C745FC01 movl $1, -4(%rbp)
000000
0018 EB23 jmp .L3
.L2:
001a F30F2A4D cvtsi2ss -4(%rbp), %xmm1
FC
001f F30F114D movss %xmm1, -8(%rbp)
F8
0024 8B45FC movl -4(%rbp), %eax
0027 83E801 subl $1, %eax
002a 89C7 movl %eax, %edi
002c E8000000 call fact
00
0031 F30F5945 mulss -8(%rbp), %xmm0
F8
0036 F30F2CC0 cvttss2si %xmm0, %eax
003a 8945FC movl %eax, -4(%rbp)
.L3:
003d F30F2A45 cvtsi2ss -4(%rbp), %xmm0
FC
0042 F30F1145 movss %xmm0, -8(%rbp)
F8
0047 8B45F8 movl -8(%rbp), %eax
004a 8945F8 movl %eax, -8(%rbp)
004d F30F1045 movss -8(%rbp), %xmm0
F8
0052 C9 leave
.cfi_def_cfa 7, 8
0053 C3 ret
.cfi_endproc
.LFE0:
.section .rodata
.LC2:
0000 54686520 .string "The Value of e = %f "
56616C75
65206F66
20652020
3D202566
.text
.globl main
main:
.LFB1:
.cfi_startproc
0054 55 pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
0055 4889E5 movq %rsp, %rbp
.cfi_def_cfa_register 6
0058 4883EC10 subq $16, %rsp
005c 8B050000 movl .LC0(%rip), %eax
0000
0062 8945F8 movl %eax, -8(%rbp)
0065 C745FC00 movl $0, -4(%rbp)
000000
006c C745FC00 movl $0, -4(%rbp)
000000
0073 EB2B jmp .L6
.L7:
009c 8345FC01 addl $1, -4(%rbp)
0075 8B45FC movl -4(%rbp), %eax
0078 89C7 movl %eax, %edi
007a E8000000 call fact
00
007f F30F100D movss .LC1(%rip), %xmm1
00000000
0087 F30F5EC8 divss %xmm0, %xmm1
008b 0F28C1 movaps %xmm1, %xmm0
008e F30F104D movss -8(%rbp), %xmm1
F8
0093 F30F58C1 addss %xmm1, %xmm0
0097 F30F1145 movss %xmm0, -8(%rbp)
F8
.L6:
00a0 837DFC0B cmpl $11, -4(%rbp)
00a4 7ECF jle .L7
00a6 F30F1045 movss -8(%rbp), %xmm0
F8
00ab 0F5AC0 cvtps2pd %xmm0, %xmm0
00ae BF000000 movl $.LC2, %edi
00
00b3 B8010000 movl $1, %eax
00
00b8 E8000000 call printf
00
00bd B8000000 movl $0, %eax
00
00c2 C9 leave
.cfi_def_cfa 7, 8
00c3 C3 ret
.cfi_endproc
.LFE1:
.section .rodata
0016 0000 .align 4
.LC0:
0018 00000000 .long 0
.align 4
.LC1:
001c 0000803F .long 1065353216
.text
.Letext0:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.