CSC231 Homework 5 Solutions 2014

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 10:54, 29 October 2014 (EDT)



Problem #1


;;; ; Hw5_1.asm
;;; ; D. Thiebaut
;;; ; A simple program with one global function that computes
;;; ; f(a) = 2*a
;;; ;
;;; ; -----------------------------------------------------------


                section .text
                global  f1

;;; ;------------------------------------------------------------
;;; ; f1(x): a function that receives a parameter through
;;; ; the stack and that computes y = 3*x - 1
;;; ; x is at [ebp+8]
;;; ; returns the result in eax.
;;; ; does not modify any of the other registers (besides eax)
;;; ;------------------------------------------------------------
f1:             push    ebp
                mov     ebp, esp
                mov     eax, dword[ebp+8]
                add     eax, eax
                add     eax, dword[ebp+8]
                dec     eax
                pop     ebp
                ret     4


Problem #2


;;; ; Hw5_2.asm
;;; ; D. Thiebaut
;;; ; A simple program with one global function that computes
;;; ; f2(x) = x+(x-1)+(x-2)+... + 2 +1
;;; ;
;;; ; -----------------------------------------------------------


                section .text
                global  f2

;;; ;------------------------------------------------------------
;;; ; f2(x): a function that receives a parameter through
;;; ; the stack and that computes y = x+(x-1)+(x-2)...+2+1
;;; ; x is at [ebp+8]
;;; ; returns the result in eax.
;;; ; does not modify any of the other registers (besides eax)
;;; ;------------------------------------------------------------
f2:             push    ebp
                mov     ebp, esp

                push    ecx
        
                mov     ecx, [ebp+8]
                mov     eax, 0
        
for:            add     eax, ecx
                loop    for

                pop     ecx
                pop     ebp
                ret     4


Problem #3


;;; ; Hw5_3.asm
;;; ; D. Thiebaut
;;; ; A simple program with two global functions that computes
;;; ; f3( x, y ) = 2 * x + 3 * y
;;; ; f4( x ) = 3 * f3( x, 3 ) + f3( x-1, x+1 )
;;; ;
;;; ; -----------------------------------------------------------


                section .text
                global  f3
                global  f4

;;; ;------------------------------------------------------------
;;; ; f3(x): a function that receives 2 parameters through
;;; ; the stack and that computes 2x + 3y
;;; ; x is at [ebp+12]
;;; ; returns the result in eax.
;;; ; does not modify any of the other registers (besides eax)
;;; ;------------------------------------------------------------
f3:             push    ebp
                mov     ebp, esp

                mov     eax, [ebp+12]
                add     eax, eax
                add     eax, [ebp+8]
                add     eax, [ebp+8]
                add     eax, [ebp+8]
        
                pop     ebp
                ret     8

;;; ;------------------------------------------------------------
;;; ; f4(x): a function that receives a parameter through
;;; ; the stack and that computes f4(x) = 3 f3(x,3) + f3(x-1, x+1)
;;; ; y is at [ebp+8]
;;; ; returns the result in eax.
;;; ; does not modify any of the other registers (besides eax)
;;; ;------------------------------------------------------------
f4:             push    ebp
                mov     ebp, esp
                push    ebx
        
                mov     eax, [ebp+8]

                push    eax
                push    dword 3
                call    f3
                mov     ebx, eax
                add     ebx, eax
                add     ebx, eax
        
                mov     eax, [ebp+8]
                dec     eax
                push    eax
                mov     eax, [ebp+8]
                inc     eax
                push    eax
                call    f3

                add     eax, ebx
                
                pop     ebx
        
                pop     ebp
                ret     4


Problem #4


;;; ; Hw5_4.asm
;;; ; D. Thiebaut
;;; ; A simple program with two global functions that compute
;;; ; f5( x ) = x + (x-1) + (x-2) + ... + 2 + 1
;;; ; f6( x ) = f5( x ) + f5( x-1 ) + f5( x-2 ) + ... + f5( 2 ) + f5( 1 )
;;; ;
;;; ; -----------------------------------------------------------


                section .text
                global  f5
                global  f6

;;; ;------------------------------------------------------------
;;; ; f5(x): a function that receives 1 param
;;; ; in the stack and that computes x+(x-1)+...+2+1
;;; ; x is at [ebp+8]
;;; ; returns the result in eax.
;;; ; does not modify any of the other registers (besides eax)
;;; ;------------------------------------------------------------
f5:             push    ebp
                mov     ebp, esp
        
                push    ecx

                mov     ecx, [ebp+8]
                mov     eax, 0

for:            add     eax, ecx
                loop    for

                pop     ecx
        
                pop     ebp
                ret     4
        

;;; ;------------------------------------------------------------
;;; ; f6(x): a function that receives a parameter through
;;; ; the stack and that computes f6(x) = f5(x)+f5(x-1)
;;; ; + f5(x-2) + f5(2) + f5(1)...
;;; ; x is at [ebp+8]
;;; ; returns the result in eax.
;;; ; does not modify any of the other registers (besides eax)
;;; ;------------------------------------------------------------
f6:             push    ebp
                mov     ebp, esp

                push    ecx
                push    ebx
        
                mov     ebx, 0
                mov     ecx, [ebp+8]

for2:           push    ecx
                call    f5
        
                add     ebx, eax
                loop    for2

                mov     eax, ebx
        
                pop     ebx
                pop     ecx
        
                pop     ebp
                ret     4