Lab 3
Lab 3
Lab 3
Advanced instructions
Contents
1 Introduction 2
2 Exercises 2
2.1 Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.3 Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1 Introduction
• The main purpose of this session is to get familiar with floating-
point instructions and read/write file.
• Students must submit their answers to the BKeL system no later than
the last period of the lab section. Then, the instructor will evaluate all
students’ work during the lab section’s final period.
2 Exercises
2.1 Exercise 1
Write a MIPS program to find the solution to the equation:
ax2 + bx + c = 0 (1)
where a, b, and c are floating-points number inserted by the user. The result
should be in 1 of these 3 cases:
• There are 2 solutions to x: print those results to the screen after the
text: ”x1 = ” and ”x2 = ”.
• There is 1 solution to x: print the result after the text: ”There is one
solution, x = ”.
Note that the solutions printed must be in floating-point numbers. The user
will input the floating-points numbers after the prompts: ”Please insert a:
”, ”Please insert b: ”, and ”Please insert c: ”.
For example, if the user inserted a=1.2, b=9.6, c=1 then the result
should be: x1 = -7.8944 and x2 = -0.1056
In another example, if the user inserted a=2.4, b=-3.2, c=3.1 then the
result should be: There is no real solution
2.2 Exercise 2
Write a MIPS program to calculate the following integral:
Z u 6
ax + bx5 + cx
f (x) = (2)
v d4 + e 3
2.3 Exercise 3
To allocate memory, please refer to the following syscall:
1 l i $v0 , 9 # system c a l l code f o r dynamic a l l o c a t i o n
2 l i $a0 , 24 # $a0 c o n t a i n s number o f b y t e s t o a l l o c a t e
After the above system call, $v0 contains the first address in heap memory
that is allocated. Then, accessing the allocated memory can be done by
lw/sw, for example:
1 # Trying t o w r i t e t o a l l o c a t e d s p a c e
2 a d d i $t0 , $ z e r o , 2021
3 sw $t0 , 0 ( $v0 )
27 l i $v0 , 16 # system c a l l f o r c l o s e f i l e
28 move $a0 , $ s 6 # f i l e d e s c r i p t o r t o c l o s e
29 syscall # close f i l e
30 #######################################################
31 #######################################################
32 # Open ( f o r r e a d i n g ) a f i l e
33 l i $v0 , 13 # system c a l l f o r open f i l e
34 l a $a0 , f o u t # i n p u t f i l e name
35 l i $a1 , 0 # Open f o r r e a d i n g ( f l a g s a r e 0 : read , 1 : w r i t e )
36 l i $a2 , 0 # mode i s i g n o r e d
37 s y s c a l l # open a f i l e ( f i l e d e s c r i p t o r r e t u r n e d i n $v0 )
38 move $s6 , $v0 # s a v e t h e f i l e d e s c r i p t o r
39 #######################################################
40 # Read from f i l e
41 l i $v0 , 14 # system c a l l f o r r e a d
42 move $a0 , $ s 6 # f i l e d e s c r i p t o r
43 l a $a1 , b u f f e r r e a d # a d d r e s s o f b u f f e r r e a d
44 l i $a2 , 44 # hardcoded b u f f e r l e n g t h
45 s y s c a l l # read f i l e
4. Copy the line from the text file to the string in the memory.
The output in the terminal for the former example input should be:
—–Student personal information—–
Name: Davy Jones
ID: 2251234
Address: 168 Ly Thuong Kiet St District 10 Ward 14 HCMC
Age: 69
Religion: None