0% found this document useful (0 votes)
7 views

Lab 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Ho Chi Minh City University of Technology

Faculty of Computer Science and Engineering


Course: Computer Architecture Lab (CO2008)

Lab 3
Advanced instructions

Ho Chi Minh City, November 2024


Ho Chi Minh City University of Technology
Faculty of Computer Science and Engineering

Contents
1 Introduction 2

2 Exercises 2
2.1 Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.3 Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Exercise for Computer Architecture


Page 1/5
Academic year 2024 - 2025
Ho Chi Minh City University of Technology
Faculty of Computer Science and Engineering

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 = ”.

• There is no real solution: print ”There is no real solution”.

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

Exercise for Computer Architecture


Page 2/5
Academic year 2024 - 2025
Ho Chi Minh City University of Technology
Faculty of Computer Science and Engineering

where u, v, a, b, c, d, and e are floating-point numbers chosen by the user.


The user will input the floating-point numbers after the prompts: ”Please
insert a: ”, ”Please insert b: ”, etc. For example, user inserted u=1, v=2,
a=3, b=4, c=5, d=6, e=7 then the result should be: -0.0634

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 )

The followings are instructions used to access a file (open/close/read/write):


1 # Sample MIPS program t h a t w r i t e s t o a new f i l e .
2 # by Kenneth Vollmar and Pete Sanderson
3 . data
4 f o u t : . a s c i i z ” t e s t o u t . t x t ” # f i l e n a m e f o r output
5 msg1 : . a s c i i z ” B e f o r e r e a d : ”
6 msg2 : . a s c i i z ” A f t e r r e a d : ”
7 b u f f e r w r i t e : . a s c i i z ”The q u i c k brown f o x jumps o v e r t h e
l a z y dog . \ n”
8 b u f f e r r e a d : . a s c i i z ”−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n”
9 . text
10 #######################################################
11 # Open ( f o r w r i t i n g ) a f i l e t h a t d o e s not e x i s t
12 l i $v0 , 13 # system c a l l f o r open f i l e
13 l a $a0 , f o u t # output f i l e name
14 l i $a1 , 1 # Open f o r w r i t i n g ( f l a g s a r e 0 : read , 1 : w r i t e )
15 l i $a2 , 0 # mode i s i g n o r e d
16 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 )
17 move $s6 , $v0 # s a v e t h e f i l e d e s c r i p t o r
18 #######################################################
19 # Write t o f i l e j u s t opened
20 l i $v0 , 15 # system c a l l f o r w r i t e t o f i l e
21 move $a0 , $ s 6 # f i l e d e s c r i p t o r
22 l a $a1 , b u f f e r w r i t e # a d d r e s s o f b u f f e r from which t o w r i t e
23 l i $a2 , 44 # hardcoded b u f f e r l e n g t h
24 s y s c a l l # write to f i l e
25 #######################################################
26 # Close the f i l e

Exercise for Computer Architecture


Page 3/5
Academic year 2024 - 2025
Ho Chi Minh City University of Technology
Faculty of Computer Science and Engineering

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

Please do the followings:

1. Manually create a line of text in format <name>,<id>,<address>,<age>,<religion>in


a text file by using a text editor. The file must be named: raw input.txt.
An example text should be: Davy Jones,2251234,168 Ly Thuong
Kiet St District 10 Ward 14 HCMC,69,None

2. Open the file to read the text you inserted.

3. Declare a string in the heap memory with dynamically allocated mem-


ory. The size of the string must be large enough to store the text in
the text file.

4. Copy the line from the text file to the string in the memory.

5. Print the string to the terminal in the following format:

(a) —–Student personal information—–


(b) Name: <name>
(c) ID: <ID>
(d) Address: <address>
(e) Age: <age>
(f) Religion: <religion>

Exercise for Computer Architecture


Page 4/5
Academic year 2024 - 2025
Ho Chi Minh City University of Technology
Faculty of Computer Science and Engineering

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

6. The printed result should also be written in a file named format-


ted result.txt

Exercise for Computer Architecture


Page 5/5
Academic year 2024 - 2025

You might also like