CSC231 Homework 6 Solution 2014
--D. Thiebaut (talk) 21:29, 14 November 2014 (EST)
Problem #1
;;; ; f.asm
;;; ; D. Thiebaut
;;; ; A simple program with one global function that computes
;;; ; f(a) = 2*a
;;; ;
;;; ; -----------------------------------------------------------
section .text
global f1
;;; ;------------------------------------------------------------
;;; ; f(x): a function that receives a parameter through
;;; ; the stack and that computes y = 2*x
;;; ; 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
push eax
mov eax, dword[ebp+8]
add eax, eax
add eax, dword[ebp+8]
dec eax
mov dword[ebp+12], eax
pop eax
pop ebp
ret 4
Problem #2
;;; ; f2.asm
;;; ; D. Thiebaut
;;; ; A simple program with one global function that computes
;;; ; f(a) = 2*a
;;; ;
;;; ; -----------------------------------------------------------
section .text
global f2
;;; ;------------------------------------------------------------
;;; ; f(x): a function that receives a parameter through
;;; ; the stack and that computes y = 2*x
;;; ; 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
cmp dword[ebp+8], 1
jne .recurse
mov dword[ebp+12], 1
pop ebp
ret 4
.recurse:
push eax ; save eax
push dword 0 ; make room
mov eax, dword[ebp+8] ; n
dec eax ; n-1
push eax
call f2 ;f2(n-1)
pop eax ; n
add eax, dword[ebp+8] ; n+f2(n-1...)
mov dword [ebp+12], eax
pop eax
pop ebp
ret 4
Problem #3
;;; ; f3_4.asm
;;; ; D. Thiebaut
;;; ; A simple program with one global function that computes
;;; ; f(a) = 2*a
;;; ;
;;; ; -----------------------------------------------------------
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
push eax
mov eax, [ebp+12]
add eax, eax
add eax, [ebp+8]
add eax, [ebp+8]
add eax, [ebp+8]
mov dword[ebp+16], eax
pop eax
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 eax
push ebx
push dword 0
push dword [ebp+8]
push dword 3
call f3
pop eax
mov ebx, eax
add ebx, eax
add ebx, eax
push dword 0
mov eax, [ebp+8]
dec eax
push eax
mov eax, [ebp+8]
inc eax
push eax
call f3
pop eax
add eax, ebx
mov dword[ebp+12], eax
pop ebx
pop eax
pop ebp
ret 4
Problem #4
;;; ; f.asm
;;; ; D. Thiebaut
;;; ; A simple program with one global function that computes
;;; ; f(a) = 2*a
;;; ;
;;; ; -----------------------------------------------------------
section .text
global f5
global f6
;;; ;------------------------------------------------------------
;;; ; f5(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)
;;; ;------------------------------------------------------------
f5: push ebp
mov ebp, esp
cmp dword[ebp+8], 1
jne .recurse
mov dword[ebp+12], 1
pop ebp
ret 4
.recurse: push eax
mov eax, dword[ebp+8]
dec eax
push eax
push eax
call f5
pop eax
add eax, dword[ebp+8]
mov dword[ebp+12], eax
pop eax
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(1)
;;; ; x is at [ebp+8]
;;; ;------------------------------------------------------------
f6: push ebp
mov ebp, esp
push eax
push ebx
cmp dword[ebp+8], 1
jne .recurse
mov eax, 1
push eax
push eax
call f5
pop dword[ebp+12]
pop ebx
pop eax
pop ebp
ret 4
.recurse:
mov eax, dword[ebp+8]
dec eax
push eax
push eax
call f6
pop ebx
mov eax, dword[ebp+8]
push eax
push eax
call f5
pop eax
add eax, ebx
mov dword[ebp+12], eax
pop ebx
pop eax
pop ebp
ret 4
Problem #5
global f7
section .text
f7: push ebp
mov ebp, esp
cmp dword[ebp+8], 2
jg .recurse
mov dword[ebp+12], 1
pop ebp
ret 4
.recurse:
push eax
push ebx
mov eax, dword[ebp+8]
dec eax
push eax
push eax
call f7
pop ebx
mov eax, dword[ebp+8]
sub eax, 2
push eax
push eax
call f7
pop eax
add eax, ebx
mov dword[ebp+12], eax
pop ebx
pop eax
pop ebp
ret 4