Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Following is a x86 program to estimate e using: Modify this code to estimate pi

ID: 3870423 • Letter: F

Question

Following is a x86 program to estimate e using:


Modify this code to estimate pi using:


This series converges much slower than the series for e. Use 1000000 iterations to estimate pi.

TITLE MASM Template                        (main.asm)

; Description:
;
; Revision date:

INCLUDE Irvine32.inc
.data
e real4 0.0       ; how to declare 32-bit floats
num dword 1       ; how to declare 32-bit ints
denom dword 1
nfact dword 1

.code
main PROC
    mov ecx,20   ; only need a few interations for e
L1:
    fild num     ; load numerator on stack as int
    fild denom   ; load denominator on stack as int
    fdiv         ; fp divide
    fld e        ; load accumulator for e estimate
    fadd         ; fp add
    fstp e       ; store fp value and pop stack
    mov eax,denom
    mov ebx, nfact
    mul ebx
    mov denom,eax
    inc nfact
loopd L1
    fld e
    call WriteFloat
    call crlf

    exit
main ENDP

END main

0! 1! 2!

Explanation / Answer

TITLE MASM Template (main.asm)

; Description:
;
; Revision date:

INCLUDE Irvine32.inc
.data
e real4 0.0 ; how to declare 32-bit floats
num dword 4 ; how to declare 32-bit ints
denom dword 1

.code
main PROC
mov ecx,500000 ; Take half the number of iterations you need.
L1:
fild num ; load numerator on stack as int
fild denom ; load denominator on stack as int
fdiv ; fp divide
fld e ; load accumulator for e estimate
fadd ; fp add
fstp e ; store fp value and pop stack
inc denom; incremented the denominator
fild num ; load numerator on stack as int
fild denom ; load denominator on stack as int
fdiv ; fp divide
fld e ; load accumulator for e estimate
fsub ; fp subtract
fstp e ; store fp value and pop stack
Inc denom; incremented the den and loops back
loopd L1
fld e
call WriteFloat
call crlf

exit
main ENDP

END main