Skip to content

Commit 2b748f9

Browse files
committed
ch6 finished?
1 parent 0230f39 commit 2b748f9

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

ch6.adoc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,54 @@ was_less:
185185
blah:
186186
----
187187

188+
==== Functions
189+
190+
Lastly, lets do a simple example of writing a function that takes a float and
191+
returns a float. I'm not going to bother doing one for doubles because it'd
192+
be effectively the same, or doing one that requires the stack, because the only
193+
difference from normal is a new set of registers and knowing which ones to save
194+
or not from the table above.
195+
196+
So, how about a function to convert a fahrenheit temperature to celsius:
197+
198+
[source,mips,linenums]
199+
----
200+
.data
201+
202+
# 5/9 = 0.5 with 5 repeating
203+
fahrenheit2celsius: .float 0.5555555
204+
205+
.text
206+
# float convert_F2C(float degrees_f)
207+
convert_F2C:
208+
la $t0, fahrenheit2celsius
209+
lwc1 $f0, 0($t0) # get conversion factor
210+
211+
# C = (F - 32) * 5/9
212+
li $t0, 32
213+
mtc1 $t0, $f1 # move int 32 to f1
214+
cvt.s.w $f1, $f1 # convert to 32.0
215+
216+
217+
sub.s $f12, $f12, $f1 # f12 = degrees - 32
218+
219+
mul.s $f0, $f0, $f12 # f0 = 0.555555 * f12
220+
221+
jr $ra
222+
----
223+
224+
You can see we follow the convention with the argument coming in f12 and the
225+
result being returned in f0. In this function we use both methods for getting
226+
a value into float registers; one we load from memory and the other, being
227+
an integer, we move and convert.
188228

189229

190230
== Conclusion
191231

232+
As I said before, it is rare for courses to even bother covering floating point
233+
instructions or assign any homework or projects that use it, but hopefully this
234+
brief overview, combined with the knowledge of previous ones is sufficient.
235+
236+
There are also 2 example programs conversions.s and calc_pi.s for you to study.
237+
238+

0 commit comments

Comments
 (0)