CSC231 Homework 6 Solutions
--D. Thiebaut 17:02, 23 October 2012 (EDT)
Problem #1
;;; hw6a.asm
;;; 231a-ad
;;; Naomi Long
;;;
;;; Program asks for the number of fibonacci terms the user
;;; wants to print and then loops, printing that number of
;;; fibonacci terms. Can only print 46 terms before registers
;;; hit overflow. Program can accept 0 as an input.
;;;
;;; Sample output sandwiched between two prompts:
;;; [231a-ad@grendel ~/hw6]$ ./hw6a
;;; How many Fibonacci terms do you want printed? 5
;;; 1
;;; 1
;;; 2
;;; 3
;;; 5
;;; [231a-ad@grendel ~/hw6]$
;;;
;;; Uses the driver.c program as a C "wrapper"
;;; To run:
;;; nasm -f elf -F stabs hw6a.asm
;;; gcc -m32 -o hw6a driver.c asm_io.o hw6a.o
;;; ./hw6a
%include "asm_io.inc"
;;; -------------------------
;;; data segment
;;; -------------------------
section .data
msg db "How many Fibonacci terms do you want printed? ",0x0
sum dd 0
;;; -------------------------
;;; code area
;;; -------------------------
section .text
global asm_main
asm_main:
;;; Python equivalent of hw6a.asm:
;;;
;;; user_input = eval(input("How many Fibonacci terms do you want printed? "))
;;; fibn_1 = 1
;;; fibn_2 = 0
;;; fibn = 0
;;; for i in range(user_input):
;;; fibn = fibn_1 + fibn_2
;;; print(fibn)
;;; fibn_1 = fibn_2
;;; fibn_2 = fibn
mov eax, msg
call print_string ; prints prompt to get user_input
call read_int ; gets user_input = number of
; number of fib terms to print
mov ecx, eax ; get ready to loop user_input times
jcxz skip_fib_compute ; prints no numbers if user_input == 0
mov ebx, 1 ; fibn_1 = 1
mov edx, 0 ; fibn_2 = 0
for:
mov dword[sum], ebx ; sum = fibn_1
add dword[sum], edx ; sum = fibn_1 + fibn_2
mov eax, dword[sum] ; eax = fibn = fibn_1 + fibn_2
mov ebx, edx ; fibn_1 = fibn_2
mov edx, eax ; fibn_2 = fibn
call print_int ; print fibn
call print_nl ; print new line
loop for ; keep looping until done
skip_fib_compute:
;;; return to C program
ret
Problem #2
;;; File: hw6b.asm
;;; Name: Emma Gould
;;; Account: 231a-ap
;;; Description: Computes and prints the first ten rows of pascal's triangle, beginning with "1",
;;; with zeroes everywhere else.
;;;
;;; Note: This program uses double words for the array but the largest integer stored is 126,
;;; which can easily fit in a byte.
;;;
;;; Due: 10/17/12
;;; To assemble, link, and run:
;;; nasm -f elf -F stabs hw6b.asm
;;; gcc -m32 -o hw6b driver.c asm_io.o hw6b.o
;;; ./hw6b
%include "asm_io.inc"
;; ------------------------------
;; data section
;; ------------------------------
section .data
Fib times 10 dd 0; array of double words
space db " ",0x00 ; tab character (for nicer output)
;; ------------------------------
;; code section
;; ------------------------------
section .text
global asm_main
asm_main:
mov ecx, 10 ; # of times for1 will loop = n
mov esi, Fib ; esi <- base address for Fib
mov dword[Fib], 1 ; initialize the array Fib with 1
; Loop for1 -----------------------------------
for1: mov edx, ecx ; edx <- n (saves the counter for for1)
mov ecx, 10 ; # of times printloop will loop in each for1 loop = 10
mov edi, 0 ; edi <- pointer = 4
; Loop printloop ------------------------------
printloop: mov eax, dword[esi+edi] ; eax <- Fib[N] (where N is the base address
; plus the pointer)
call print_int ; print Fib[N]
mov eax, space ; eax <- space (" ")
call print_string ; print space (" ")
add edi, 4 ; edi <- edi + 4 (esi+edi = N + 1)
loop printloop ; executes loop printloop until ecx = 0 (ten times
; each time the loop for1 executes)
; End loop printloop --------------------------
mov ecx, 9 ; # of times for2 will loop in each for1 loop = 9
mov edi, 36 ; edi <- pointer = 36 (esi + 36 is the last number
; in the Fib array)
; Loop for2 -----------------------------------
for2: mov eax, dword[esi+edi] ; eax <- Fib[N] (where N is the base address of Fib
; plus the pointer)
add eax, dword[esi+edi-4] ; eax <- Fib[N] + Fib[N-1]
mov dword[esi+edi], eax ; new Fib [N] <- old Fib[N] + Fib[N-1]
sub edi, 4 ; edi <- edi - 4 (so now esi+edi will be the old N-1)
loop for2 ; execute loop for2 until ecx = 0
; End loop for2 -------------------------------
mov ecx, edx ; ecx <- n
call print_nl ; print a new line
loop for1 ; loop until n = 0 (ecx = 0)
; End loop for1 -------------------------------
;; return to C program
ret