Difference between revisions of "CSC231 Homework 5 Solutions 2010"
(Created page with '--~~~~ ---- =Program 1= <code><pre> ;;; ; File: hw5a.asm ;;; ; Author: Tiffany Q. Liu ;;; ; Acct: 231a-ac ;;; ; Date: October 28, 2010 ;;; ; ;;; ; Desc: Print the N words in ms…') |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] 14:54, 19 November 2010 (UTC) | --[[User:Thiebaut|D. Thiebaut]] 14:54, 19 November 2010 (UTC) | ||
---- | ---- | ||
+ | <onlydft> | ||
=Program 1= | =Program 1= | ||
<code><pre> | <code><pre> | ||
− | + | ;;; ; File: hw5a.asm | |
;;; ; Author: Tiffany Q. Liu | ;;; ; Author: Tiffany Q. Liu | ||
;;; ; Acct: 231a-ac | ;;; ; Acct: 231a-ac | ||
Line 115: | Line 116: | ||
</pre></code> | </pre></code> | ||
+ | |||
+ | =Program 2= | ||
+ | <code><pre> | ||
+ | ;;; ; File: hw5b.asm | ||
+ | ;;; ; Author: Tiffany Q. Liu | ||
+ | ;;; ; Acct: 231a-ac | ||
+ | ;;; ; Date: October 28, 2010 | ||
+ | ;;; ; | ||
+ | ;;; ; Desc: Prints the first 10 rows of Pascal's triangle using one array of | ||
+ | ;;; ; 10 bytes. | ||
+ | ;;; ; | ||
+ | ;;; ; to assemble and run: | ||
+ | ;;; ; | ||
+ | ;;; ; nasm -f elf -F stabs hw5b.asm | ||
+ | ;;; ; ld -melf_i386 -o hw5b hw5b.o | ||
+ | ;;; ; ./hw5b | ||
+ | ;;; ; | ||
+ | ;;; ; Output: | ||
+ | ;;; ; 1 0 0 0 0 0 0 0 0 0 0 | ||
+ | ;;; ; 1 1 0 0 0 0 0 0 0 0 0 | ||
+ | ;;; ; 1 2 1 0 0 0 0 0 0 0 0 | ||
+ | ;;; ; 1 3 3 1 0 0 0 0 0 0 0 | ||
+ | ;;; ; 1 4 6 4 1 0 0 0 0 0 0 | ||
+ | ;;; ; 1 5 10 10 5 1 0 0 0 0 | ||
+ | ;;; ; 1 6 15 20 15 6 1 0 0 0 | ||
+ | ;;; ; 1 7 21 35 35 21 7 1 0 0 | ||
+ | ;;; ; 1 8 28 56 70 56 28 8 1 0 | ||
+ | ;;; ; 1 9 36 84 126 126 84 36 9 1 | ||
+ | ;;; ; ------------------------------------------------------------------- | ||
+ | |||
+ | %include "asm_io.inc" | ||
+ | |||
+ | ;; ------------------------- | ||
+ | ;; data segment | ||
+ | ;; ------------------------- | ||
+ | section .data | ||
+ | pas dw 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00 | ||
+ | space db " ", 0x00 | ||
+ | N equ 10 | ||
+ | ;; ------------------------- | ||
+ | ;; code area | ||
+ | ;; ------------------------- | ||
+ | section .text | ||
+ | global asm_main | ||
+ | |||
+ | asm_main: | ||
+ | mov ecx, N ; ecx <- 10, init outer loop counter | ||
+ | forOut: | ||
+ | push ecx ; save outer loop counter to stack | ||
+ | mov ecx, N ; ecx <- 10, init 1st inner loop counter | ||
+ | mov ebx, pas ; ebx <- first index of pas, | ||
+ | ; set up pointer | ||
+ | forIn1: mov eax, 0 ; eax <- 0, clear out eax | ||
+ | mov ax, word[ebx] ; pass address of the int pas to ax | ||
+ | call print_int ; call a function in asm_io library | ||
+ | ; that will print the int | ||
+ | |||
+ | mov eax, space ; eax <- " ", prepare to print space | ||
+ | call print_string ; call function in asm_io library that | ||
+ | ; will print the string | ||
+ | add ebx, 1*2 ; ebx <- ebx + 2, increment ebx to point | ||
+ | ; to the next index in array of words | ||
+ | |||
+ | endIn1: loop forIn1 ; loop until all the ints in the array | ||
+ | ; are printed with a space in between | ||
+ | ; each int | ||
+ | |||
+ | call print_nl ; call the function that prints | ||
+ | ; a new blank line | ||
+ | |||
+ | mov ebx, pas+(N-1)*2 ; ebx <- addr of last pascal int in | ||
+ | ; the array | ||
+ | mov ecx, N-1 ; ecx <- 1, init 2nd inner loop counter | ||
+ | ; one less than the number of int since | ||
+ | ; the first int never gets changed | ||
+ | forIn2: mov eax, 0 ; eax <- 0, clear out eax | ||
+ | mov edx, 0 ; edx <- 0, clear out edx | ||
+ | mov ax, word[ebx] ; ax <- int ebx is pointing to | ||
+ | mov dx, word[ebx-1*2] ; dx <- int to the left of the one | ||
+ | ; stored in ax | ||
+ | add ax, dx ; ax <- ax+dx, obtain next pascal int in | ||
+ | ; ax by adding the current int to its | ||
+ | ; left neighbor | ||
+ | mov word[ebx], ax ; move sum back to index ebx points to | ||
+ | sub ebx, 1*2 ; ebx <- ebx - 2, move pointer to left | ||
+ | ; to point to the prev word | ||
+ | endIn2: loop forIn2 ; loop until all ints in array are | ||
+ | ; updated | ||
+ | |||
+ | pop ecx ; retain outer loop counter from stack | ||
+ | loop forOut ; loop until all first 10 rows of | ||
+ | ; Pascal's Triangle are printed | ||
+ | |||
+ | ;; return to C program | ||
+ | |||
+ | ret | ||
+ | </pre></code> | ||
+ | </onlydft> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:CSC231]][[Category:Homework]][[Category:Asm]] |