Write My Paper Button

WhatsApp Widget

File type: s, and asmyou will be tasked with performing theSecant Method

File type: s, and asmyou will be tasked with performing theSecant Method on the following function

(x)=x^3+2/x

To get started, you first should inspect the C code (Links to an external site.)for the Secant Method.

Approach:

Inspecting the C code, you will notice that the structure is fairly simple line by line, but the challenging part is the repetitive function calls that keeps occurring midway through the code. This will be the challenging part for you. There is a fixed number of register on the CPU and as a result you need to use the stack when representing various function calls. You can also notice that doubles are used extensively here, so we must utilize the FPU.

#include

#include

double f(double x)

{

return (x*x*x)+(2/(x));

}

double secant( double xA, double xB, double(*f)(double) )

{

double e = 1.0e-12;

double fA, fB;

double d;

int i;

int limit = 50;

fA=(*f)(xA);

for (i=0; i<limit; i++) {

fB=(*f)(xB);

d = (xB xA) / (fB fA) * fB;

if (fabs (Links to an external site.)(d) < e)

break;

xA = xB;

fA = fB;

xB -= d;

}

if (i==limit) {

printf (Links to an external site.)(“Function is not converging near (%7.4f,%7.4f).n, xA,xB);

return 99.0;

}

return xB;

}

int main(int argc, char *argv[])

{

double step = 1.0e-2;

double e = 1.0e-12;

double x = 1.032; // just so we use secant method

double xx, value;

int s = (f(x)> 0.0);

while (x < 3.0) {

value = f(x);

if (fabs (Links to an external site.)(value) < e) {

printf (Links to an external site.)(“Root found at x= .9fn, x);

s = (f(x+.0001)>0.0);

}

else if ((value > 0.0) != s) {

xx = secant(xstep, x,&f);

if (xx != 99.0) // -99 meaning secand method failed

printf (Links to an external site.)(“Root found at x= .9fn, xx);

else

printf (Links to an external site.)(“Root found near x= %7.4fn, x);

s = (f(x+.0001)>0.0);

}

x += step;

}

return 0;

}

You Have to simulate a function call. Your first step is to break it up into modular components, translate them into assembly instructions and then reconstruct them in MIPS.

Since a processor can only modify binary data stored in its registers, you will have to make a system call to get user inputs. you will also need to allocate some memory in the global region for your messages.

Do not use global memory for temp variables. If you need extra memory then use the stack.

MIPS

Given that you want to write your code for any given step size, you will be forced to make use of the FPU coprocessor. That is, a MIPS processor doesn’t have the capability to perform floating point arithmetics and therefore there will likely be a loss of precision whenever you perform a division operation. As a result, you will have to rely on coprocessor 1.

FP instructions available in MIPS:FP instructions available in MIPS:

add.s fd, fs, ft #FP add single

cvt.s.w fd, fs #Convert to single precision FP from integer

cvt.w.s fd, fs #Convert to integer from single precision FP

div.s fd, fs, ft #FP divide single

mfc1 rd, fs #move from coprocessor 1 (FP)

mov.s fd, fs #move FP single precision FP

mtc1 rs, fd #move to coprocessor 1 (FP)

mul.s fd, fs, ft #FP multiply single

sub.s fd, fs, ft #FP subtract single

The ‘s’ stands for single precision, so if you need to manipulate doubles you will have to replace the ‘s’ with a ‘d’ character

Keep in mind that the number of registers is limited, and there are registers that you should use for local variables

$f0 – $f3

Floating point return values

$f4 – $f10

Temporary registers, not preserved by subprograms

$f12 – $f14

First two arguments to subprograms, not preserved by subprograms

$f16 – $f18

More temporary registers, not preserved by subprograms

$f20 – $f31

Saved registers, preserved by subprograms

PROGRAMMING

Your code file should have the following structure

# Comment infromation about the name of program and description of function

# Type your C file here, to give some form of prespective for other people

# outline for numberical integration for x*x*2+x from 0 to 10

.data # variable declarations follow this line

#Introduces the data section of the program.

# (i.e. global variables)

#…

.text # instructions follow this line

main: # indicates start of code (first instruction to execute)

# Introduces the text section of the program.

# (i.e. code)

# …

#end of main

li $v0,10

syscall

# End of program, leave a blank line afterwards

first, declare your global variables

.data

from: .double 10.0

to: .double 100.0

n: .double 20.0

.text

main:

l.d $f0, from

Then break your different statements into instructions

and finally, wrap them inside of a loop

.data

start: .double 0.0

end: .double 10.0

step: .double 1.0

.text

ldc1 $f0, start

loop:

c.le.d $f0, end

bc1t exit

#loop body

add.d $f10, step

j loop

exit:

You will have to debug your code while programming, you can either use the registers, or you can have print statements in your code. You will need the OS to print to console, you can do so by using syscall. depending on the instruction argument (Links to an external site.) to #v0 it will print the string in a specific format

.data

here: .asciiz “heren”

.text

li $v0,4

la $a0, here

syscall

The post File type: s, and asmyou will be tasked with performing theSecant Method appeared first on PapersSpot.

Don`t copy text!
WeCreativez WhatsApp Support
Our customer support team is here to answer your questions. Ask us anything!
???? Hi, how can I help?