Difference between revisions of "CSC231 Homework 4 Solutions"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- =Problem 1= <br /> <source lang="asm"> ;;; File: hw4a.asm ;;; Name: Emma Gould ;;; Account: 231a-ap ;;; Description: Computes and prints the first 10 32-bit Fibonacc...")
 
(Problem 3)
 
Line 230: Line 230:
 
             global  asm_main
 
             global  asm_main
 
     asm_main:       
 
     asm_main:       
            mov  eax, 0    ;eax initialize all content to 0's
+
                        mov  eax, 0    ;eax initialize all content to 0's
 
mov  bx, 1      ;bx will hold the counter, first term
 
mov  bx, 1      ;bx will hold the counter, first term
 
mov  ax,bx      ;counter->ax, printed
 
mov  ax,bx      ;counter->ax, printed
 
call  print_int  
 
call  print_int  
 
mov  ax,0x20    ;" "->ax and printed
 
mov  ax,0x20    ;" "->ax and printed
            call  print_char  
+
                        call  print_char  
 
mov  ax, 1      ; F(1)->ax, printed and a new line is called
 
mov  ax, 1      ; F(1)->ax, printed and a new line is called
 
call  print_int   
 
call  print_int   
Line 245: Line 245:
 
call  print_int
 
call  print_int
 
mov  ax,0x20    ;" "->ax and printed
 
mov  ax,0x20    ;" "->ax and printed
            call  print_char
+
                        call  print_char
 
mov  ax, 1      ; F(2)->ax, printed and a new line is called
 
mov  ax, 1      ; F(2)->ax, printed and a new line is called
 
call print_int
 
call print_int
Line 255: Line 255:
 
call  print_int
 
call  print_int
 
mov  ax, 0x20  ;" "->ax and printed
 
mov  ax, 0x20  ;" "->ax and printed
            call  print_char
+
                        call  print_char
 
mov  ax, dx    ;Make ax carry F(n-1), or F(2)=1
 
mov  ax, dx    ;Make ax carry F(n-1), or F(2)=1
 
add  ax, ax    ;Multiply F(2) by 2
 
add  ax, ax    ;Multiply F(2) by 2
Line 261: Line 261:
 
add  ax, cx    ;Add F(2)*2+(F(1)-1), print int and new line
 
add  ax, cx    ;Add F(2)*2+(F(1)-1), print int and new line
 
call  print_int   
 
call  print_int   
            call  print_nl
+
                        call  print_nl
 
mov  cx,dx      ;Set the new F(n-2) for the next term  
 
mov  cx,dx      ;Set the new F(n-2) for the next term  
 
mov  dx,ax      ;Set the new F(n-1) for the next term  
 
mov  dx,ax      ;Set the new F(n-1) for the next term  
Line 271: Line 271:
 
call  print_int
 
call  print_int
 
mov  ax,0x20
 
mov  ax,0x20
            call  print_char
+
                        call  print_char
 
mov  ax, dx
 
mov  ax, dx
 
add  ax,ax
 
add  ax,ax
Line 277: Line 277:
 
add  ax, cx
 
add  ax, cx
 
call  print_int
 
call  print_int
            call  print_nl
+
                        call  print_nl
 
mov  cx,dx
 
mov  cx,dx
 
mov  dx,ax
 
mov  dx,ax
Line 285: Line 285:
 
call  print_int
 
call  print_int
 
mov  ax,0x20
 
mov  ax,0x20
            call  print_char
+
                        call  print_char
 
mov  ax,dx
 
mov  ax,dx
 
add  ax, ax
 
add  ax, ax
Line 291: Line 291:
 
add  ax, cx
 
add  ax, cx
 
call  print_int
 
call  print_int
            call  print_nl
+
                        call  print_nl
 
mov  cx,dx
 
mov  cx,dx
 
mov  dx, ax
 
mov  dx, ax
Line 299: Line 299:
 
call  print_int
 
call  print_int
 
mov  ax,0x20
 
mov  ax,0x20
            call  print_char
+
                        call  print_char
 
mov  ax,dx
 
mov  ax,dx
 
add  ax,ax
 
add  ax,ax
Line 305: Line 305:
 
add  ax, cx
 
add  ax, cx
 
call  print_int
 
call  print_int
            call  print_nl
+
                        call  print_nl
 
mov  cx,dx
 
mov  cx,dx
 
mov  dx,ax
 
mov  dx,ax
Line 313: Line 313:
 
call  print_int
 
call  print_int
 
mov  ax,0x20
 
mov  ax,0x20
            call  print_char
+
                        call  print_char
 
mov  ax,dx
 
mov  ax,dx
 
add  ax,ax
 
add  ax,ax
Line 319: Line 319:
 
add  ax, cx
 
add  ax, cx
 
call  print_int
 
call  print_int
            call  print_nl
+
                        call  print_nl
 
mov  cx,dx
 
mov  cx,dx
 
mov  dx,ax
 
mov  dx,ax
Line 327: Line 327:
 
call  print_int
 
call  print_int
 
mov  ax,0x20
 
mov  ax,0x20
            call  print_char
+
                        call  print_char
 
mov  ax,dx
 
mov  ax,dx
 
add  ax,ax
 
add  ax,ax
Line 333: Line 333:
 
add  ax, cx
 
add  ax, cx
 
call  print_int
 
call  print_int
            call  print_nl
+
                        call  print_nl
 
mov  cx,dx
 
mov  cx,dx
 
mov  dx,ax
 
mov  dx,ax
Line 341: Line 341:
 
call  print_int
 
call  print_int
 
mov  ax,0x20
 
mov  ax,0x20
            call  print_char
+
                        call  print_char
 
mov  ax,dx
 
mov  ax,dx
 
add  ax,ax
 
add  ax,ax
Line 347: Line 347:
 
add  ax, cx
 
add  ax, cx
 
call  print_int
 
call  print_int
            call  print_nl
+
                        call  print_nl
 
mov  cx,dx
 
mov  cx,dx
 
mov  dx,ax
 
mov  dx,ax
Line 355: Line 355:
 
call  print_int
 
call  print_int
 
mov  ax,0x20
 
mov  ax,0x20
            call  print_char
+
                        call  print_char
 
mov  ax,dx
 
mov  ax,dx
 
add  ax,ax
 
add  ax,ax
Line 361: Line 361:
 
add  ax, cx
 
add  ax, cx
 
call  print_int
 
call  print_int
            call  print_nl
+
                        call  print_nl
 
              
 
              
 
             ;; return to C program
 
             ;; return to C program
  
            ret
+
                        ret
 
</source>
 
</source>
 
<br />
 
<br />
  
 
[[Category:CSC231]][[Category:Homework]]
 
[[Category:CSC231]][[Category:Homework]]

Latest revision as of 20:54, 9 October 2012

--D. Thiebaut 21:52, 9 October 2012 (EDT)


Problem 1


;;; File: hw4a.asm
;;; Name: Emma Gould
;;; Account: 231a-ap
;;; Description: Computes and prints the first 10 32-bit Fibonacci terms.
;;; To Assemble, Link, and Run:
;;;		nasm -f elf -F stabs hw4a.asm
;;;		gcc -m32 -o hw4a driver.c asm_io.o hw4a.o
;;;		./hw4a

%include "asm_io.inc"

	;; --------------------
	;; data segment
	;; --------------------	
		section .data
Fib		dd	0x0	; create an array of 10
		dd	0x0	; double-words (each 32 bits)
		dd	0x0	; and set them equal to 0
		dd	0x0
		dd	0x0
		dd	0x0
		dd	0x0
		dd	0x0
		dd	0x0
		dd	0x0

	;; --------------------
	;; code area
	;; --------------------
		section .text
		global	asm_main
asm_main:

		mov	eax, 1	        	; move 1 into eax
		call	print_int		; print the contents of eax ("1")
		call	print_nl		; print a new blank line
		call	print_int		; print the contents of eax ("1")
		call	print_nl		; print a new blank line
	
		mov	dword[Fib], eax		; move eax into Fib[0] -> 1
		mov	dword[Fib+4], eax	; move eax into Fib[1] -> 1
		add	eax, dword[Fib]		; add Fib[0] to eax -> 2
		call	print_int		; print the contents of eax ("2")
		call	print_nl		; print a new blank line
	
		mov	dword[Fib+8], eax	; move eax into Fib[2] -> 2
		add	eax, dword[Fib+4]	; add Fib[1] to eax -> 3
		call	print_int		; print the contents of eax ("3")
		call	print_nl		; print a new blank line
	
		mov	dword[Fib+12], eax	; move eax into Fib[3] -> 3
		add	eax, dword[Fib+8]	; add Fib[2] to eax -> 5
		call	print_int		; print the contents of eax ("5")
		call	print_nl		; print a new blank line
	
		mov	dword[Fib+16], eax	; move eax into Fib[4] -> 5
		add	eax, dword[Fib+12]	; add Fib[3] to eax -> 8
		call	print_int		; print the contents of eax ("8")
		call	print_nl		; print a new blank line
	
		mov	dword[Fib+20], eax	; move eax into Fib[5] -> 8
		add	eax, dword[Fib+16]	; add Fib[4] to eax -> 13
		call    print_int		; print the contents of eax ("13")
		call	print_nl		; print a new blank line
	
		mov	dword[Fib+24], eax	; move eax into Fib[6] -> 13
		add	eax, dword[Fib+20]	; add Fib[5] to eax -> 21
		call    print_int		; print the contents of eax ("21")
		call	print_nl		; print a new blank line

		mov	dword[Fib+28], eax	; move eax into Fib[7] -> 21
		add	eax, dword[Fib+24]	; add Fib[6] to eax -> 34
		call    print_int		; print the contents of eax ("34")
		call	print_nl		; print a new blank line
	
		mov	dword[Fib+32], eax	; move eax into Fib[8] -> 34
		add	eax, dword[Fib+28]	; add Fib[7] to eax -> 55
		call    print_int		; print the contents of eax ("55")
		call	print_nl		; print a new blank line
		mov	dword[Fib+36], eax	; move eax into Fib[9] -> 55

	;; return to C program
		ret


Problem 2


;;; File: hw4b.asm
;;; Name: Emma Gould
;;; Account: 231a-ap
;;; Description: Computes and prints the first 10 16-bit Fibonacci terms.
;;; To Assemble, Link, and Run:
;;;             nasm -f elf -F stabs hw4b.asm
;;;             gcc -m32 -o hw4b driver.c asm_io.o hw4b.o
;;;             ./hw4b

%include "asm_io.inc"

;;;  --------------------
;;;  data segment
;;;  --------------------
                section .data
Fib		dw      0x0 ; create an array of 10
                dw      0x0 ; words (each 16 bits)
                dw      0x0 ; and set them equal to 0
	        dw      0x0
                dw      0x0
                dw      0x0
                dw      0x0
                dw      0x0
                dw      0x0
                dw      0x0

;;;  --------------------
;;;  code area
;;;  --------------------
                section .text
                global  asm_main
asm_main:
		mov	eax, 0			; move 0 into eax
	
		mov     ax, 1			; move 1 into ax
		call    print_int		; print the contents of ax ("1")
		call    print_nl		; print a new blank line
		call    print_int		; print the contents of ax ("1")
		call    print_nl		; print a new blank line

		mov     word[Fib], ax		; move ax into Fib[0] -> 1
		mov     word[Fib+2], ax		; move ax into Fib[1] -> 1
		add     ax, word[Fib]		; add Fib[0] to ax -> 2
		call    print_int	  	; print the contents of ax ("2")
		call    print_nl	  	; print a new blank line

		mov     word[Fib+4], ax		; move ax into Fib[2] -> 2
		add     ax, word[Fib+2]		; add Fib[1] to ax -> 3
		call    print_int		; print the contents of ax ("3")
		call    print_nl		; print a new blank line

		mov     word[Fib+6], ax		; move ax into Fib[3] -> 3
		add     ax, word[Fib+4]		; add Fib[2] to ax -> 5
		call    print_int		; print the contents of ax ("5")
		call    print_nl		; print a new blank line

		mov     word[Fib+8], ax		; move ax into Fib[4] -> 5
		add     ax, word[Fib+6]		; add Fib[3] to ax -> 8
		call    print_int		; print the contents of ax ("8")
		call    print_nl		; print a new blank line

		mov     word[Fib+10], ax	; move ax into Fib[5] -> 8
		add     ax, word[Fib+8]		; add Fib[4] to ax -> 13
		call    print_int		; print the contents of ax ("13")
		call    print_nl		; print a new blank line

		mov     word[Fib+12], ax	; move ax into Fib[6] -> 13
		add     ax, word[Fib+10]	; add Fib[5] to ax -> 21
		call    print_int		; print the contents of ax ("21")
		call    print_nl		; print a new blank line

		mov     word[Fib+14], ax	; move ax into Fib[7] -> 21
		add     ax, word[Fib+12]	; add Fib[6] to ax -> 34
		call    print_int		; print the contents of ax ("34")
		call    print_nl		; print a new blank line

		mov     word[Fib+16], ax	; move ax into Fib[8] -> 34
		add     ax, word[Fib+14]	; add Fib[7] to ax -> 55
		call    print_int		; print the contents of ax ("55")
		call    print_nl		; print a new blank line
		mov     word[Fib+18], ax	; move ax into Fib[9] -> 55

;;;  return to C program
		ret


Problem 3


;;; hw4c.asm
;;; Yoshie 10/2/12 (edited by D. Thiebaut)
;;; 
;;; This program computes and prints the first 10 16-bit
;;; "special "Fibonacci terms using a C "wrapper" program
;;;
;;; The "special" Fibonacci terms are calculated as follows:
;;;
;;; F(1) = 1
;;; F(2) = 1
;;; F(N) = F(N-1) * 2 + F(N-2) - 1
;;; 
;;; Prints a modified Fibonacci series:
;;;
;;; 1 1
;;; 2 1
;;; 3 2
;;; 4 4
;;; 5 9
;;; 6 21
;;; 7 50
;;; 8 120
;;; 9 289
;;; 10 697
;;; 
;;;   To assemble:
;;;       nasm -f elf -F stabs hw4c.asm
;;;       gcc -m32 -o hw4c.asm driver.c asm_io.o hw4c.o
;;;       ./hw4c.asm

     %include "asm_io.inc"

             ;; -------------------------
             ;; data segment
             ;; -------------------------
             section .data
			 ;;None 
			 
             ;; -------------------------
             ;; code area
             ;; -------------------------
             section .text
             global  asm_main
     asm_main:       
                         mov   eax, 0     ;eax initialize all content to 0's
			 mov   bx, 1      ;bx will hold the counter, first term
			 mov   ax,bx      ;counter->ax, printed
			 call  print_int 
			 mov   ax,0x20    ;" "->ax and printed
                         call  print_char 
			 mov   ax, 1      ; F(1)->ax, printed and a new line is called
			 call  print_int  
			 call  print_nl  
			 mov   cx, ax     ;cx will carry the F(n-2 term) where F(n)<2
			 
			 inc   bx         ;Second row/term, moved to ax and printed
			 mov   ax,bx     
			 call  print_int
			 mov   ax,0x20    ;" "->ax and printed
                         call  print_char
			 mov   ax, 1      ; F(2)->ax, printed and a new line is called
			 call print_int
			 call print_nl
			 mov  dx, ax      ;dx will carry the F(n-1) term where F(n)<2
			 
			 inc   bx         ;Third row/term, printed
			 mov   ax, bx
			 call  print_int
			 mov   ax, 0x20   ;" "->ax and printed
                         call  print_char
			 mov   ax, dx     ;Make ax carry F(n-1), or F(2)=1
			 add   ax, ax     ;Multiply F(2) by 2
			 dec   cx         ;Minus F(n-1) or F(1) by 1
			 add   ax, cx     ;Add F(2)*2+(F(1)-1), print int and new line
			 call  print_int  
                         call  print_nl
			 mov   cx,dx      ;Set the new F(n-2) for the next term 
			 mov   dx,ax      ;Set the new F(n-1) for the next term 
          
		     ;The same code as the third term is used to print the rest of the series
			 
			 inc   bx         ;Fourth row/term
			 mov   ax, bx
			 call  print_int
			 mov   ax,0x20
                         call  print_char
			 mov   ax, dx
			 add   ax,ax
			 dec   cx
			 add   ax, cx
			 call  print_int
                         call  print_nl
			 mov   cx,dx
			 mov   dx,ax
			 
			 inc   bx         ;Fifth row/term
			 mov   ax, bx
			 call  print_int
			 mov   ax,0x20
                         call  print_char
			 mov   ax,dx
			 add   ax, ax
			 dec   cx
			 add   ax, cx
			 call  print_int
                         call  print_nl
			 mov   cx,dx
			 mov   dx, ax
			 
			 inc   bx         ;Sixth row/term
			 mov   ax, bx
			 call  print_int
			 mov   ax,0x20
                         call  print_char
			 mov   ax,dx
			 add   ax,ax
			 dec   cx
			 add   ax, cx
			 call  print_int
                         call  print_nl
			 mov   cx,dx
			 mov   dx,ax
			 
			 inc   bx         ;Seventh row/term
			 mov   ax, bx
			 call  print_int
			 mov   ax,0x20
                         call  print_char
			 mov   ax,dx
			 add   ax,ax
			 dec   cx
			 add   ax, cx
			 call  print_int
                         call  print_nl
			 mov   cx,dx
			 mov   dx,ax
			 
			 inc   bx         ;Eighth row/term
			 mov   ax, bx
			 call  print_int
			 mov   ax,0x20
                         call  print_char
			 mov   ax,dx
			 add   ax,ax
			 dec   cx
			 add   ax, cx
			 call  print_int
                         call  print_nl
			 mov   cx,dx
			 mov   dx,ax
			 
			 inc   bx         ;Ninth row/term
			 mov   ax, bx
			 call  print_int
			 mov   ax,0x20
                         call  print_char
			 mov   ax,dx
			 add   ax,ax
			 dec   cx
			 add   ax, cx
			 call  print_int
                         call  print_nl
			 mov   cx,dx
			 mov   dx,ax
			 
			 inc   bx         ;Tenth row/term
			 mov   ax, bx
			 call  print_int
			 mov   ax,0x20
                         call  print_char
			 mov   ax,dx
			 add   ax,ax
			 dec   cx
			 add   ax, cx
			 call  print_int
                         call  print_nl
             
             ;; return to C program

                         ret