Difference between revisions of "CSC231 Homework 7 2017"
(→Problem #1: Programming in C) |
(→Problem #1: Programming in C) |
||
(18 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
− | <center> | + | <!--center> |
<font size="+2">Page under construction!</font> | <font size="+2">Page under construction!</font> | ||
</center> | </center> | ||
[[File:UnderConstruction.jpg|center|300px]] | [[File:UnderConstruction.jpg|center|300px]] | ||
+ | <br /--> | ||
+ | <br /> | ||
+ | __TOC__ | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
Line 27: | Line 30: | ||
D. Thiebaut | D. Thiebaut | ||
− | This program gets a | + | This program gets a string from the command line and prints it on the screen. |
*/ | */ | ||
Line 44: | Line 47: | ||
char stars[100]; | char stars[100]; | ||
− | //--- see if | + | //--- see if user entered arguments on the command line --- |
if ( argc < 2 ) { | if ( argc < 2 ) { | ||
//--- No, didn't. Print syntax information and quit --- | //--- No, didn't. Print syntax information and quit --- | ||
Line 69: | Line 72: | ||
231b@aurora ~/hw/hw7 $ '''./hw7_1 hello there''' | 231b@aurora ~/hw/hw7 $ '''./hw7_1 hello there''' | ||
hello-there | hello-there | ||
− | 231b@aurora ~/hw/hw7 $ ./hw7_1 hello there CSC231! | + | 231b@aurora ~/hw/hw7 $ '''./hw7_1 hello there CSC231!''' |
hello-there-CSC231! | hello-there-CSC231! | ||
<br /> | <br /> | ||
Line 75: | Line 78: | ||
<br /> | <br /> | ||
* Modify '''hw7_1.c''' so that it creates a '''logo''' with the sentence entered by the user on the command line, as illustrated in the examples below. | * Modify '''hw7_1.c''' so that it creates a '''logo''' with the sentence entered by the user on the command line, as illustrated in the examples below. | ||
− | * | + | * Note: Your program should emulate the solution program ''exactly'': |
<br /> | <br /> | ||
Line 106: | Line 109: | ||
******** | ******** | ||
********* | ********* | ||
− | * my hat * | + | * my-hat * |
********* | ********* | ||
******** | ******** | ||
Line 145: | Line 148: | ||
***************** | ***************** | ||
****************** | ****************** | ||
− | * this works well * | + | * this-works-well * |
****************** | ****************** | ||
***************** | ***************** | ||
Line 166: | Line 169: | ||
<br /> | <br /> | ||
+ | |||
==Submission== | ==Submission== | ||
<br /> | <br /> | ||
Line 172: | Line 176: | ||
<br /> | <br /> | ||
− | =Problem 2= | + | =Problem 2: Pascal Triangle= |
+ | <br /> | ||
+ | |||
+ | [[Image:CSC231_PascalTriangle.gif | right]] | ||
+ | |||
+ | Write a program called '''hw7_2.asm''' that '''computes''' and '''outputs''' the first 10 rows of [http://mathforum.org/workshops/usi/pascal/pascal_intro.html Pascal's Triangle]. | ||
+ | |||
+ | Your program should behave as follows: | ||
+ | |||
+ | # It should use an array of 10 numbers and should initialize it with 1 in the first cell, and 0 everywhere else. | ||
+ | # The first thing your program will do is to print this array, with spaces separating the numbers. Use '''_printDec''' to print the numbers. This first output corresponds to the first row of Pascal's triangle: | ||
<br /> | <br /> | ||
− | + | ::<source lang="text"> | |
+ | 1 0 0 0 0 0 0 0 0 0 | ||
+ | </source> | ||
<br /> | <br /> | ||
+ | # Your program will then scan the array starting with the last cell and replace this cell with the sum of itself and its left neighbor: | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | |||
+ | 1 0 0 0 0 0 0 0 0 0 | ||
+ | \| 0+0 = 0 | ||
+ | 1 0 0 0 0 0 0 0 0 0 | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | # The program will then keep on going "down" the array by moving the pointer/index left by one cell, summing up cells with their left neighbors: | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | |||
+ | 1 0 0 0 0 0 0 0 0 0 | ||
+ | \| 0+0 = 0 | ||
+ | 1 0 0 0 0 0 0 0 0 0 | ||
+ | |||
+ | |||
+ | 1 0 0 0 0 0 0 0 0 0 | ||
+ | \| 0+0 = 0 | ||
+ | 1 0 0 0 0 0 0 0 0 0 | ||
+ | |||
+ | |||
+ | 1 0 0 0 0 0 0 0 0 0 | ||
+ | \| 0+0 = 0 | ||
+ | 1 0 0 0 0 0 0 0 0 0 | ||
+ | |||
+ | . . . | ||
+ | |||
+ | 1 0 0 0 0 0 0 0 0 0 | ||
+ | \| 1+0 = 1 | ||
+ | 1 1 0 0 0 0 0 0 0 0 | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | # You end up with the second row of Pascal's triangle: | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | |||
+ | 1 1 0 0 0 0 0 0 0 0 0 | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | : which your program will print. | ||
+ | |||
+ | Repeat this process until you have printed all 10 rows of the triangle. Row 1 is the one with one 1 and nine 0s on it. | ||
+ | |||
+ | Your program should output the rows one above the other, including the zeros: | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | |||
+ | 1 0 0 0 0 0 0 0 0 0 | ||
+ | 1 1 0 0 0 0 0 0 0 0 | ||
+ | 1 2 1 0 0 0 0 0 0 0 | ||
+ | 1 3 3 1 0 0 0 0 0 0 | ||
+ | ... | ||
+ | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | ==Submission== | ||
+ | <br /> | ||
+ | Submit your program on Moodle, in the Homework 7, Program 2 section. | ||
+ | <br /> | ||
+ | ====Hints==== | ||
+ | <br /> | ||
+ | # Figure out what the largest number you will get is, and decide whether you want to use an array of bytes, words, or double words. | ||
+ | # You may want to start by initializing your array to Pascal's Row 4, for example, and transform it into Row 5, using just one loop that scans the array. Once this works, you can then add an outside loop that will make the program compute the other rows, starting with Row 0. | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
=Problem 3= | =Problem 3= | ||
<br /> | <br /> | ||
− | ...to be | + | * Get a copy of the executable named '''wrapper''': |
+ | |||
+ | getcopy wrapper | ||
+ | |||
+ | * Get a copy of its source: | ||
+ | |||
+ | getcopy wrapper.asm | ||
+ | |||
+ | * Debug wrapper with '''ddd''': | ||
+ | |||
+ | cleanupddd | ||
+ | ddd wrapper & | ||
+ | |||
+ | : (the '''cleanupddd''' command is required as ddd tends to not work if you do not clean up the directory where it stores its setup file every time you use it...) | ||
+ | * Following the steps of this [[CSC231_DDD_Short_Tutorial|short ddd tutorial]], you should end up with a window similar to this one. Yours, though, should also show the machine code window which this picture doesn't show. | ||
+ | <br /> | ||
+ | [[Image:dddWrapperMystery.png|center|600px]] | ||
+ | <br /> | ||
+ | * You will notice that part of the source code is missing, because I have hidden it inside another file called mystery.asm. Unfortunately it is not available to you, and you have to debug the program without its source, trying to figure out what it does just using the step-by-step debugging and observing what ends up in registers, and follow what instructions are executed in the '''machine code''' window. | ||
+ | <br /> | ||
+ | * Go to the Homework 7 Problem 3 section in Moodle and answer the different questions. | ||
+ | <br /> | ||
+ | ==Possibly Useful Hints== | ||
+ | <br /> | ||
+ | The names of the variables used by the programmer who wrote the '''mystery.asm''' file are not available, and, without them, it's impossible to display the memory contents using '''ddd'''. However, Linux has powerful commands for looking inside executable files... and one of them is known to display the contents of executable in hex... It could be that this command ''might'' be useful to figure out the name of the variables used in an executable... | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /><br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
Line 192: | Line 323: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
− | [[Category:CSC231]][[Category:Nasm]][[Category:Assembly]] | + | [[Category:CSC231]][[Category:Homework]][[Category:Nasm]][[Category:Assembly]] |
Latest revision as of 09:45, 17 April 2017
--D. Thiebaut (talk) 08:28, 8 April 2017 (EDT)
Contents
This assignment is due on Monday, 4/17/2017, at 11:55 p.m.
Problem #1: Programming in C
- Create the C program below, and run it. Name it hw7_1.c. It will serve as the seed for your solution program for Problem 1.
/* hw7_1.c D. Thiebaut This program gets a string from the command line and prints it on the screen. */ #include <stdio.h> #include <stdlib.h> #include <string.h> //-------------------------------------------------------------------- // MAIN PROGRAM //-------------------------------------------------------------------- void main( int argc, char *argv[] ) { //--- variables --- int n, i, name; char sentence[100]; char stars[100]; //--- see if user entered arguments on the command line --- if ( argc < 2 ) { //--- No, didn't. Print syntax information and quit --- printf( "Syntax: %s string [string...]\n", argv[0] ); exit( 1 ); } //--- print the arguments back to the user, with a dash in between --- for ( i = 1; i < argc; i++ ) { if ( i > 1 ) printf( "-" ); printf( "%s", argv[i] ); } printf( "\n" ); //--- Done! --- }
- Here are different ways of using it...
231b@aurora ~/hw/hw7 $ gcc -o hw7_1 hw7_1.c 231b@aurora ~/hw/hw7 $ ./hw7_1 hello hello 231b@aurora ~/hw/hw7 $ ./hw7_1 hello there hello-there 231b@aurora ~/hw/hw7 $ ./hw7_1 hello there CSC231! hello-there-CSC231!
Your Assignment
- Modify hw7_1.c so that it creates a logo with the sentence entered by the user on the command line, as illustrated in the examples below.
- Note: Your program should emulate the solution program exactly:
231b@aurora ~/hw/hw7 $ ./hw7_1 hello * ** *** **** ***** ****** ******* ******** * hello * ******** ******* ****** ***** **** *** ** * 231b@aurora ~/hw/hw7 $ ./hw7_1 my hat * ** *** **** ***** ****** ******* ******** ********* * my-hat * ********* ******** ******* ****** ***** **** *** ** * 231b@aurora ~/hw/hw7 $ ./hw7_1 A * ** *** **** * A * **** *** ** * 231b@aurora ~/hw/hw7 $ ./hw7_1 this works well * ** *** **** ***** ****** ******* ******** ********* ********** *********** ************ ************* ************** *************** **************** ***************** ****************** * this-works-well * ****************** ***************** **************** *************** ************** ************* ************ *********** ********** ********* ******** ******* ****** ***** **** *** ** *
Submission
- Submit your program in the Homework 7 section on Moodle.
Problem 2: Pascal Triangle
Write a program called hw7_2.asm that computes and outputs the first 10 rows of Pascal's Triangle.
Your program should behave as follows:
- It should use an array of 10 numbers and should initialize it with 1 in the first cell, and 0 everywhere else.
- The first thing your program will do is to print this array, with spaces separating the numbers. Use _printDec to print the numbers. This first output corresponds to the first row of Pascal's triangle:
1 0 0 0 0 0 0 0 0 0
- Your program will then scan the array starting with the last cell and replace this cell with the sum of itself and its left neighbor:
1 0 0 0 0 0 0 0 0 0 \| 0+0 = 0 1 0 0 0 0 0 0 0 0 0
- The program will then keep on going "down" the array by moving the pointer/index left by one cell, summing up cells with their left neighbors:
1 0 0 0 0 0 0 0 0 0 \| 0+0 = 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 \| 0+0 = 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 \| 0+0 = 0 1 0 0 0 0 0 0 0 0 0 . . . 1 0 0 0 0 0 0 0 0 0 \| 1+0 = 1 1 1 0 0 0 0 0 0 0 0
- You end up with the second row of Pascal's triangle:
1 1 0 0 0 0 0 0 0 0 0
- which your program will print.
Repeat this process until you have printed all 10 rows of the triangle. Row 1 is the one with one 1 and nine 0s on it.
Your program should output the rows one above the other, including the zeros:
1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 1 3 3 1 0 0 0 0 0 0 ...
Submission
Submit your program on Moodle, in the Homework 7, Program 2 section.
Hints
- Figure out what the largest number you will get is, and decide whether you want to use an array of bytes, words, or double words.
- You may want to start by initializing your array to Pascal's Row 4, for example, and transform it into Row 5, using just one loop that scans the array. Once this works, you can then add an outside loop that will make the program compute the other rows, starting with Row 0.
Problem 3
- Get a copy of the executable named wrapper:
getcopy wrapper
- Get a copy of its source:
getcopy wrapper.asm
- Debug wrapper with ddd:
cleanupddd ddd wrapper &
- (the cleanupddd command is required as ddd tends to not work if you do not clean up the directory where it stores its setup file every time you use it...)
- Following the steps of this short ddd tutorial, you should end up with a window similar to this one. Yours, though, should also show the machine code window which this picture doesn't show.
- You will notice that part of the source code is missing, because I have hidden it inside another file called mystery.asm. Unfortunately it is not available to you, and you have to debug the program without its source, trying to figure out what it does just using the step-by-step debugging and observing what ends up in registers, and follow what instructions are executed in the machine code window.
- Go to the Homework 7 Problem 3 section in Moodle and answer the different questions.
Possibly Useful Hints
The names of the variables used by the programmer who wrote the mystery.asm file are not available, and, without them, it's impossible to display the memory contents using ddd. However, Linux has powerful commands for looking inside executable files... and one of them is known to display the contents of executable in hex... It could be that this command might be useful to figure out the name of the variables used in an executable...