CSC231 Homework 6 2012
--D. Thiebaut 13:48, 10 October 2012 (EDT)
Contents
This assignment is due on 10/17/12, at 11:59 p.m. + 1 minute.
Grading
- Reminder: I use the Smith scale for grading: 4.0 = A, 3.7 = A-, 3.3 = B+, 3.0 = B, etc.
- I will start taking points off for badly documented programs. More points will be assigned to documentation as the semester progresses. A well documented program should sport the following features:
- A header containing the date, name of program, name of programmer, an explanation of what the program does, an example of its output if possible, and how to generate the executable
- Documentation inside the code.
- You do not need to document each line. You can have a one-line comment above a group of instruction to explain what that section does.
- If you document an instruction, do not paraphrase what the instruction does, but indicate how that instruction affects the progress of the computation.
- Example:
inc eax ; add 1 to eax (that's not informative) inc eax ; n = n + 1 (much better: the n of our algorithm gets incremented)
Example Program
Make a copy of this program in your directory and run it. Verify that it prints 10 numbers, starting at 0, and stopping at 9 (included).
;;; loopExample.asm
;;;a simple demo program to show how to implement a counting loop
;;;Uses the driver.c program as a C "wrapper"
%include "asm_io.inc"
;; -------------------------
;; data segment
;; -------------------------
section .data
count dd 0
;; -------------------------
;; code area
;; -------------------------
section .text
global asm_main
asm_main:
mov ecx, 10 ; get ready to loop 10 times
for: mov eax, dword[count] ; load counter
call print_int ; print it
call print_nl ; new line
inc dword[count] ; increment counter
loop for ; keep looping until done
;; return to C program
ret
Problem #1 (1.5 points for code, 0.5 points for documentation)
Write a program that asks the user for an integer number (we can assume that the user will be well behaved and will never enter a negative number, or a number larger than 20), and that displays that number of Fibonacci terms. Your program must compute the Fibonacci terms, and not have them precomputed in the executable.
Here is an example of an interaction with the user:
How many Fibonacci terms do you want printed? 3 1 1 2
and another example:
How many Fibonacci terms do you want printed? 5 1 1 2 3 5
Yet, still another example:
How many Fibonacci terms do you want printed? 1 1
Submission
Store your program in a file called hw6a.asm and submit it as follows:
rsubmit hw6 hw6a.asm
Make sure you submit the .asm file, and not the executable. No extensions will be given to incorrectly submitted source files!
Problem # 2 (1.5 points for code, 0.5 points for documentation)
Write a program that outputs the first 10 rows of Pascal's Triangle.
Your program should use an array and initialize it with 1 in the first cell, and 0 everywhere, else. Then your program prints it. It's the first row of Pascal's triangle:
1 0 0 0 0 0 0 0 0 0
Then scan the array starting with the last cell (using a register pointer) 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
Keep on going "down" the array by moving the pointer left by one cell (remember that if your array is an array of words, that means decrementing the register by 2, if an array of double words, decrementing it by 4), 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 you can now 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 1 1 0 0 1 2 1 0 1 3 3 1 ...
Format your program so that it uses the driver.c and asm_io.asm programs to print integers.
Requirements
- Your program must use loops created with the LOOP instruction.
- The first row of Pascal's triangle must contain all 0s in all its cells.
Submission
Store your program in a file called hw6b.asm, and submit it as follows:
rsubmit hw6 hw6b.asm
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.
- In general, it is often a good idea to code the algorithm in a high-level language to make sure you have the logic right, then take the high-level program and translate it into assembly by hand. You can use the high-level code as comments in your assembly, and use labels that refer to the different sections of your code. For example, you can use for1:, for2:, while1:, if1:, if2: as labels referring to for loops, while loops and/or if statements.