Difference between revisions of "CSC231 Homework 5 2017"

From dftwiki3
Jump to: navigation, search
 
(9 intermediate revisions by the same user not shown)
Line 9: Line 9:
 
=Problem #1=
 
=Problem #1=
 
<br />
 
<br />
Write an assembly language program called '''hw5_1.asm''' that prompts the user for an integer, and prints out a pattern of stars representing a triangle, a square, and another triangle.
+
Write an assembly language program called '''hw5_1.asm''' that prompts the user for an integer, and prints out a pattern of stars representing a triangle, a square, and another triangle.  The height and width of the patterns are defined by an integer that the user inputs to the program.  The "&gt;" sign is the prompt used by the program.
 
<br />
 
<br />
Here is an example where the user enters 5 when prompted by the program:
+
Here is an example where the user enters '''5''' when prompted by the program:
 
<br />
 
<br />
 
[[Image:hw5_1.png|400px|center]]
 
[[Image:hw5_1.png|400px|center]]
Line 93: Line 93:
 
==Hints==
 
==Hints==
 
<br />
 
<br />
 +
<font color="magenta">Note (added 3/31/17): You can expect that the number of stars selected by the user will never be more than 30.</font>
 +
<br />
 +
 
You will find out that you will need a for loop to control how many lines you need to print.  The for-loop will require ecx to control the number of iterations.  However, printing a string requires the use of ecx, so your ecx register will be modified inside the loop by the print section...  So you need to save the ecx register somewhere before you print, and restore it after you've finished printing.  You can use another register, or you can use a memory variable for storage...
 
You will find out that you will need a for loop to control how many lines you need to print.  The for-loop will require ecx to control the number of iterations.  However, printing a string requires the use of ecx, so your ecx register will be modified inside the loop by the print section...  So you need to save the ecx register somewhere before you print, and restore it after you've finished printing.  You can use another register, or you can use a memory variable for storage...
 
<br />
 
<br />
 
==Submission==
 
==Submission==
 
<br />
 
<br />
Submit your asm file (not the executable) to Moodle.  You won't be able to run or evaluate it, so make sure your program works exactly as the solution program!!!
+
Submit your asm file (not the executable) to Moodle.  Document it well. You won't be able to run or evaluate it, so make sure your program works exactly as the solution program!!!
 +
<br />
 +
 
 +
=Problem #2=
 +
<br />
 +
Write a '''bash script''' called '''hw5_2.sh''' that works the same way the hw5_1.asm program works, except it will get the integer input on the command line, rather than prompting the user.
 +
 
 +
Here is an example of how your bash script should work:
 +
 
 +
231b@aurora  $ ./hw5_2.sh 3
 +
*
 +
**
 +
***
 +
***
 +
***
 +
***
 +
***
 +
**
 +
*
 +
231b@aurora  $ ./hw5_2.sh 1
 +
*
 +
*
 +
*
 +
231b@aurora  $ ./hw5_2.sh 2
 +
*
 +
**
 +
**
 +
**
 +
**
 +
*
 +
 +
<br />
 +
==Testing==
 +
<br />
 +
I cannot make available the solution bash-script, since it would be impossible to hide its code from you, but I can provide you with a python program that behaves exactly the same way your bash script should.
 +
 
 +
* Get a copy of it as follows:
 +
 +
getcopy hw5_2sol.pyc
 +
 +
: and run it as follows:
 +
 +
 
 +
231b@aurora $ '''python hw5_2sol.pyc 5'''
 +
*
 +
**
 +
***
 +
****
 +
*****
 +
*****
 +
*****
 +
*****
 +
*****
 +
*****
 +
*****
 +
****
 +
***
 +
**
 +
*
 +
 +
* The example above shows the output for '''5'''.  It contains 3x5 = 15 lines, 5 lines for the upper triangle, 5 lines for the square, and 5 lines for the bottom triangle.
 +
<br />
 +
==Submission==
 +
<br />
 +
* Submit your '''hw5_2.sh''' program on Moodle.  Again, you will not be able to evaluate your code on Moodle, so make sure it works well before you submit!  Document your code according to the examples of good documentation provided in the lab on bash scripts.
 +
<br />
 +
<br />
 +
 
 +
=Problem 3=
 +
<br />
 +
* Assume that you have a program that uses a loop to compute Fibonacci numbers. 
 +
* The Fibonacci terms get stored in an array called Fib as they are computed.  The array is an array of '''dwords'''.
 +
* In the loop, the program computes
 +
<br />
 +
:::<source lang="java">
 +
          Fib[ i ] = Fib[ i-1 ] + Fib[ i-2 ];
 +
</source>
 +
<br />
 +
* The program uses some form of addressing mode to store each '''Fib[i]''' term in the array.
 +
* If we are not worried about possible overflow of the arithmetic, i.e. we are not worried that the computation might become invalid at some point, and if we assume that the array can be as large as we want (we have a lot of RAM in our computer), '''what is a good approximation to the number of Fibonacci terms an assembly program can compute in 1 second, if our computer has a 2.5 GHz Pentium processor?'''
 +
* Answer the multiple-choice question on Moodle.
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 
<br />
 
<br />
 +
[[Category:CSC231]][[Category:Nasm]][[Category:Assembly]][[Category:Bash]]

Latest revision as of 11:15, 3 April 2017

--D. Thiebaut (talk) 08:12, 25 March 2017 (EDT)



This homework is due on 4/3/17, at 11:55 p.m.


Problem #1


Write an assembly language program called hw5_1.asm that prompts the user for an integer, and prints out a pattern of stars representing a triangle, a square, and another triangle. The height and width of the patterns are defined by an integer that the user inputs to the program. The ">" sign is the prompt used by the program.
Here is an example where the user enters 5 when prompted by the program:

Hw5 1.png


Note that the output corresponds to 3 geometric patterns: a triangle of 5 lines, a square of 5 lines, and another triangle of 5 lines. The width of the square and triangles is 5 star (max length of a line).


Hw5 1top.png Hw5 1middle.png Hw5 1bot.png



Solution Executable


The executable version of the solution program is available for you to play with. Get a copy of it as follows:

getcopy hw5_1sol


and run it a few times to see how it works:

./hw5_1sol

You can compare the output of your program to the solution program by running these linux commands (we assume that we want to test the two programs with an input of 6):


231b@aurora hw5 $ cat > input.txt
6
^D          (type Control-D then ENTER to indicate end of input)
231b@aurora hw5 $ ./hw5_1 < input.txt > output.txt
231b@aurora hw5 $ ./hw5_1sol < input.txt > outputsol.txt
231b@aurora hw5 $ diff -y output.txt outputsol.txt  
> *                                                                > *
**                                                                 **
***                                                                ***
****                                                               ****
*****                                                              *****
******                                                             ******
******                                                             ******
******                                                             ******
******                                                             ******
******                                                             ******
******                                                             ******
******                                                             ******
******                                                             ******
*****                                                              *****
****                                                               ****
***                                                                ***
**                                                                 **
*                                                                  *

If your output does not match the solution output, you will see a special character

> *                                                                > *
**                                                                 **
***                                                                ***
****                                                               ****
*****                                                              *****
******                                                             ******
******                                                             ******
******                                                             ******
******                                                             ******
******                                                             ******
******                                                             ******
******                                                             ******
                                                                >  ******
*****                                                              *****
****                                                               ****
***                                                                ***
**                                                                 **
*                                                                  *

In this case the solution output in on the right, and the blank line in your output indicates that your output is missing a line that is found in the solution output. You can then modify your program to make it work perfectly well.

Note that it is normal for the 6 that the user entered not to be found in the output. That's a side effect of redirecting the output of programs to files: the keyboard input is not captured.

Hints


Note (added 3/31/17): You can expect that the number of stars selected by the user will never be more than 30.

You will find out that you will need a for loop to control how many lines you need to print. The for-loop will require ecx to control the number of iterations. However, printing a string requires the use of ecx, so your ecx register will be modified inside the loop by the print section... So you need to save the ecx register somewhere before you print, and restore it after you've finished printing. You can use another register, or you can use a memory variable for storage...

Submission


Submit your asm file (not the executable) to Moodle. Document it well. You won't be able to run or evaluate it, so make sure your program works exactly as the solution program!!!

Problem #2


Write a bash script called hw5_2.sh that works the same way the hw5_1.asm program works, except it will get the integer input on the command line, rather than prompting the user.

Here is an example of how your bash script should work:

231b@aurora  $ ./hw5_2.sh 3
*
**
***
***
***
***
***
**
*
231b@aurora  $ ./hw5_2.sh 1
*
*
*
231b@aurora  $ ./hw5_2.sh 2
*
**
**
**
**
*


Testing


I cannot make available the solution bash-script, since it would be impossible to hide its code from you, but I can provide you with a python program that behaves exactly the same way your bash script should.

  • Get a copy of it as follows:
getcopy hw5_2sol.pyc

and run it as follows:


231b@aurora $ python hw5_2sol.pyc 5
*
**
***
****
*****
*****
*****
*****
*****
*****
*****
****
***
**
*

  • The example above shows the output for 5. It contains 3x5 = 15 lines, 5 lines for the upper triangle, 5 lines for the square, and 5 lines for the bottom triangle.


Submission


  • Submit your hw5_2.sh program on Moodle. Again, you will not be able to evaluate your code on Moodle, so make sure it works well before you submit! Document your code according to the examples of good documentation provided in the lab on bash scripts.



Problem 3


  • Assume that you have a program that uses a loop to compute Fibonacci numbers.
  • The Fibonacci terms get stored in an array called Fib as they are computed. The array is an array of dwords.
  • In the loop, the program computes


           Fib[ i ] = Fib[ i-1 ] + Fib[ i-2 ];


  • The program uses some form of addressing mode to store each Fib[i] term in the array.
  • If we are not worried about possible overflow of the arithmetic, i.e. we are not worried that the computation might become invalid at some point, and if we assume that the array can be as large as we want (we have a lot of RAM in our computer), what is a good approximation to the number of Fibonacci terms an assembly program can compute in 1 second, if our computer has a 2.5 GHz Pentium processor?
  • Answer the multiple-choice question on Moodle.