CSC231 Homework 6 2012
--D. Thiebaut 13:48, 10 October 2012 (EDT)
Contents
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
Write a program that asks the user for an integer number (we can assume that the user will be well behave 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 Fiboacci terms do you want printed? 5 1 1 2 3 5
Yet, still another example:
How many Fiboacci 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!
Program 2
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.
- Your program can keep at most 2 rows of Pascal's triangle.
- 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:
submit 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.