Solutions to Assignment 2

advertisement
CSC 3210: Assignment 2
Due date: Feb. 17, 2015 (Before Midnight)
Please use your first name and last name to name your file. For example, if my name is Tom Cruise,
then the file should have the name, Tom_Cruise.docx. Only use underline to connect first name and
last name.
1. Convert the following C expressions into SPARC assembly instructions, minimize nop instructions. (20
points)
[The value of variable sum should be stored in the register %l0 at the end of the program. And create a
label ‘result’ for the instruction ‘mov 1, %g1’]
[Hint: first convert the for loop into a while loop]
sum = 0;
for( i = 1; i <= 10; i++)
sum += (i*i);
Solution:
The final value of sum is 385.
Version 1 with nop instructions
define(sum, l0)
define(i_r, l1)
.global main
main:
save %sp, -96, %sp
mov 0, %sum
mov 1, %i_r
ba
test
nop
loop:
mov %i_r, %o0
mov %i_r, %o1
call
.mul
nop
add
%o0, %sum, %sum
add
%i_r, 1, %i_r
test:
cmp
ble
nop
%i_r, 10
loop
mov
ta
1, %g1
0
result:
Version 2 without nop instruction
define(sum, l0)
define(i_r, l1)
.global main
main:
save %sp, -96, %sp
mov 0, %sum
ba
test
mov 1, %i_r
loop:
call
.mul
mov %i_r, %o1
add
%o0, %sum, %sum
add
%i_r, 1, %i_r
test:
cmp
%i_r, 10
ble
loop
mov %i_r, %o0
result:
mov 1, %g1
ta
0
2. Exercise 2-3 from text book. Minimize nop instructions (20 points + 5 bonus)
Write a program to find the maximum of
x3 – 14x2 + 56x – 64
in the range -2 <= x <= 8, by stepping one by one through the range.
[The maximum value should be stored in the register %l0 at the end of the program. And create a label
‘result’ for the instruction ‘mov
1, %g1’]
Solution:
The maximum value should be 5 when x = 3.
Version 1 with nop instructions
define(max, l0)
define(x_r, l1)
define(r_r, l2)
.global main
main:
save %sp, -96, %sp
mov 0, %max
mov -2, %x_r
ba
test
nop
loop:
mov %x_r, %o0
mov %x_r, %o1
call
.mul
nop
sub
%x_r, 14, %o1
call
.mul
nop
sub
%o0, 64, %r_r
mov %x_r, %o0
mov 56, %o1
call
.mul
nop
add
%o0, %r_r, %r_r
cmp
%r_r, %max
ble
set
nop
mov %r_r, %max
set:
add
%x_r, 1, %x_r
test:
cmp
%x_r, 8
ble
loop
nop
result:
mov 1, %g1
ta
0
Version 2 without nop instruction
define(max, l0)
define(x_r, l1)
define(r_r, l2)
.global main
main:
save
mov
ba
mov
%sp, -96, %sp
0, %max
test
-2, %x_r
call
mov
call
sub
sub
mov
call
mov
add
cmp
ble
add
mov
.mul
%x_r, %o1
.mul
%x_r, 14, %o1
%o0, 64, %r_r
%x_r, %o0
.mul
56, %o1
%o0, %r_r, %r_r
%r_r, %max
set
%x_r, 1, %x_r
%r_r, %max
cmp
ble
mov
%x_r, 8
loop !or ble,a
%x_r, %o0
mov
ta
1, %g1
0
loop:
set:
test:
result:
Requirements:
1. Choose one question to program. For the second program, it has 5 points as bonus.
2. nano lastname2.m will create the buffer for you to enter your code. (All the commands for the
operation, save, cut, paste and so on, are available at the bottom of the SSH window. The SSH client,
PuTTY, can be downloaded via link:
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html ) Any SSH software is fine as long
as it can connect to solar sever.
3. Name your source file ‘lastname2.m’ and store it in your class account. [It is a .m file rather than a .s
file since you will have macro definitions.]
4. To expand the macros and to create the .s assembly code file, run:
m4 lastname2.m > lastname2.s
6. To compile the code and create an executable, use:
gcc –o lastname2 lastname2.s
7. To run the program, type:
./lastname2
8. Be sure to include the following information as comments in the header of your source file:
/* doe2.m
Author: John Doe
Account: jdoe1
Class: CSC 3210
Program #2
Due date: Feb 17, 2014
Description: (Give a brief description of what your program does.)
*/
9. Follow program standards as presented in your book. Pay special attention to comments, identifier
style, and consistent (tab) indentation.
10. Document your code. In assembly language, good documentation is very important. For most
instructions following main:, add a comment to the end of the line.
11. Submit your program (lastname2.m and lastname2._ex) by uploading to D2L (rename lastname2 to
lastname2._ex to make it allowed to be uploaded to D2L)
Download