Fill This Form To Receive Instant Help
Homework answers / question archive / 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
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.
<span class="co2">#include <math.h></span> <span class="co2">#include <stdio.h></span> <span class="kw4">double</span> f<span class="br0">(</span><span class="kw4">double</span> x<span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> (x<span class="sy0">*</span>x<span class="sy0">*</span>x)+(2/(x))<span class="sy0">;</span> <span class="br0">}</span> <span class="kw4">double</span> secant<span class="br0">(</span> <span class="kw4">double</span> xA<span class="sy0">,</span> <span class="kw4">double</span> xB<span class="sy0">,</span> <span class="kw4">double</span><span class="br0">(</span><span class="sy0">*</span>f<span class="br0">)</span><span class="br0">(</span><span class="kw4">double</span><span class="br0">)</span> <span class="br0">)</span> <span class="br0">{</span> <span class="kw4">double</span> e <span class="sy0">=</span> <span class="nu19">1.0e-12</span><span class="sy0">;</span> <span class="kw4">double</span> fA<span class="sy0">,</span> fB<span class="sy0">;</span> <span class="kw4">double</span> d<span class="sy0">;</span> <span class="kw4">int</span> i<span class="sy0">;</span> <span class="kw4">int</span> limit <span class="sy0">=</span> <span class="nu0">50</span><span class="sy0">;</span> fA<span class="sy0">=</span><span class="br0">(</span><span class="sy0">*</span>f<span class="br0">)</span><span class="br0">(</span>xA<span class="br0">)</span><span class="sy0">;</span> <span class="kw1">for</span> <span class="br0">(</span>i<span class="sy0">=</span><span class="nu0">0</span><span class="sy0">;</span> i<span class="sy0"><</span>limit<span class="sy0">;</span> i<span class="sy0">++</span><span class="br0">)</span> <span class="br0">{</span> fB<span class="sy0">=</span><span class="br0">(</span><span class="sy0">*</span>f<span class="br0">)</span><span class="br0">(</span>xB<span class="br0">)</span><span class="sy0">;</span> d <span class="sy0">=</span> <span class="br0">(</span>xB <span class="sy0">-</span> xA<span class="br0">)</span> <span class="sy0">/</span> <span class="br0">(</span>fB <span class="sy0">-</span> fA<span class="br0">)</span> <span class="sy0">*</span> fB<span class="sy0">;</span> <span class="kw1">if</span> <span class="br0">(</span><a href="https://www.opengroup.org/onlinepubs/009695399/functions/fabs.html" class="external" target="_blank" rel="noreferrer noopener"><span class="kw3">fabs</span><span class="screenreader-only"> (Links to an external site.)</span></a><span class="br0">(</span>d<span class="br0">)</span> <span class="sy0"><</span> e<span class="br0">)</span> <span class="kw2">break</span><span class="sy0">;</span> xA <span class="sy0">=</span> xB<span class="sy0">;</span> fA <span class="sy0">=</span> fB<span class="sy0">;</span> xB <span class="sy0">-=</span> d<span class="sy0">;</span> <span class="br0">}</span> <span class="kw1">if</span> <span class="br0">(</span>i<span class="sy0">==</span>limit<span class="br0">)</span> <span class="br0">{</span> <a href="https://www.opengroup.org/onlinepubs/009695399/functions/printf.html" class="external" target="_blank" rel="noreferrer noopener"><span class="kw3">printf</span><span class="screenreader-only"> (Links to an external site.)</span></a><span class="br0">(</span><span class="st0">"Function is not converging near (%7.4f,%7.4f).<span class="es1">\n</span>"</span><span class="sy0">,</span> xA<span class="sy0">,</span>xB<span class="br0">)</span><span class="sy0">;</span> <span class="kw1">return</span> <span class="sy0">-</span><span class="nu16">99.0</span><span class="sy0">;</span> <span class="br0">}</span> <span class="kw1">return</span> xB<span class="sy0">;</span> <span class="br0">}</span> <span class="kw4">int</span> main<span class="br0">(</span><span class="kw4">int</span> argc<span class="sy0">,</span> <span class="kw4">char</span> <span class="sy0">*</span>argv<span class="br0">[</span><span class="br0">]</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw4">double</span> step <span class="sy0">=</span> <span class="nu19">1.0e-2</span><span class="sy0">;</span> <span class="kw4">double</span> e <span class="sy0">=</span> <span class="nu19">1.0e-12</span><span class="sy0">;</span> <span class="kw4">double</span> x <span class="sy0">=</span> <span class="sy0">-</span><span class="nu16">1.032</span><span class="sy0">;</span> <span class="co1">// just so we use secant method</span> <span class="kw4">double</span> xx<span class="sy0">,</span> value<span class="sy0">;</span> <span class="kw4">int</span> s <span class="sy0">=</span> <span class="br0">(</span>f<span class="br0">(</span>x<span class="br0">)</span><span class="sy0">></span> <span class="nu16">0.0</span><span class="br0">)</span><span class="sy0">;</span> <span class="kw1">while</span> <span class="br0">(</span>x <span class="sy0"><</span> <span class="nu16">3.0</span><span class="br0">)</span> <span class="br0">{</span> value <span class="sy0">=</span> f<span class="br0">(</span>x<span class="br0">)</span><span class="sy0">;</span> <span class="kw1">if</span> <span class="br0">(</span><a href="https://www.opengroup.org/onlinepubs/009695399/functions/fabs.html" class="external" target="_blank" rel="noreferrer noopener"><span class="kw3">fabs</span><span class="screenreader-only"> (Links to an external site.)</span></a><span class="br0">(</span>value<span class="br0">)</span> <span class="sy0"><</span> e<span class="br0">)</span> <span class="br0">{</span> <a href="https://www.opengroup.org/onlinepubs/009695399/functions/printf.html" class="external" target="_blank" rel="noreferrer noopener"><span class="kw3">printf</span><span class="screenreader-only"> (Links to an external site.)</span></a><span class="br0">(</span><span class="st0">"Root found at x= %12.9f<span class="es1">\n</span>"</span><span class="sy0">,</span> x<span class="br0">)</span><span class="sy0">;</span> s <span class="sy0">=</span> <span class="br0">(</span>f<span class="br0">(</span>x<span class="sy0">+</span><span class="nu18">.0001</span><span class="br0">)</span><span class="sy0">></span><span class="nu16">0.0</span><span class="br0">)</span><span class="sy0">;</span> <span class="br0">}</span> <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">(</span><span class="br0">(</span>value <span class="sy0">></span> <span class="nu16">0.0</span><span class="br0">)</span> <span class="sy0">!=</span> s<span class="br0">)</span> <span class="br0">{</span> xx <span class="sy0">=</span> secant<span class="br0">(</span>x<span class="sy0">-</span>step<span class="sy0">,</span> x<span class="sy0">,&</span>f<span class="br0">)</span><span class="sy0">;</span> <span class="kw1">if</span> <span class="br0">(</span>xx <span class="sy0">!=</span> <span class="sy0">-</span><span class="nu16">99.0</span><span class="br0">)</span> <span class="co1">// -99 meaning secand method failed</span> <a href="https://www.opengroup.org/onlinepubs/009695399/functions/printf.html" class="external" target="_blank" rel="noreferrer noopener"><span class="kw3">printf</span><span class="screenreader-only"> (Links to an external site.)</span></a><span class="br0">(</span><span class="st0">"Root found at x= %12.9f<span class="es1">\n</span>"</span><span class="sy0">,</span> xx<span class="br0">)</span><span class="sy0">;</span> <span class="kw1">else</span> <a href="https://www.opengroup.org/onlinepubs/009695399/functions/printf.html" class="external" target="_blank" rel="noreferrer noopener"><span class="kw3">printf</span><span class="screenreader-only"> (Links to an external site.)</span></a><span class="br0">(</span><span class="st0">"Root found near x= %7.4f<span class="es1">\n</span>"</span><span class="sy0">,</span> x<span class="br0">)</span><span class="sy0">;</span> s <span class="sy0">=</span> <span class="br0">(</span>f<span class="br0">(</span>x<span class="sy0">+</span><span class="nu18">.0001</span><span class="br0">)</span><span class="sy0">></span><span class="nu16">0.0</span><span class="br0">)</span><span class="sy0">;</span> <span class="br0">}</span> x <span class="sy0">+=</span> step<span class="sy0">;</span> <span class="br0">}</span> <span class="kw1">return</span> <span class="nu0">0</span><span class="sy0">;</span> <span class="br0">}</span>
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.
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 |
Your code file should have the following structure
<i># Comment infromation about the name of program and description of function</i> <i># Type your C file here, to give some form of prespective for other people</i> <i># outline for numberical integration for x*x*2+x from 0 to 10 </i> .data <i># variable declarations follow this line</i> <i> </i><i>#Introduces the data section of the program.</i> <i> # (i.e. global variables)</i> <i> </i><i>#...</i> .text <i># instructions follow this line </i> main: <i># indicates start of code (first instruction to execute)</i> <i> </i><i># </i> Introduces the text section of the program. <i> </i><i># </i>(i.e. code) <i> </i><i># …</i> #end of main li $v0,10 syscall <i># End of program, leave a blank line afterwards </i>
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 "here\n" .text li $v0,4 la $a0, here syscall