Computer Architecture and Assembly Language Programming - CS401 Spring 2007 Assignment 02 Solution
Computer Architecture and Assembly Language Programming - CS401 Spring 2007 Assignment 02 Solution
02
Total Marks: 15
SEMESTER SPRING 2007
CS401-Computer Architecture & Assembly Language Programming Due Date: 11/04/07
Instructions
Please read the following instructions carefully before submitting assignment:
It should be clear that your assignment will not get any credit if:
o The assignment is submitted after due date.
o The submitted assignment does not open or file corrupt.
o The assignment is copied.
o There is not mentioned the student Id in the assignment File or
name of file is other than student ID.
1. Replace the following invalid instructions with a single instruction that has the same effect.
a. pop ip
b. mov ip, L5
c. sub sp, 2
mov [ss:sp], ax
d. mov ax, [ss:sp]
add sp, 2
e. add sp, 6
mov ip, [ss:sp-6]
Solution:
a. ret
b. jmp L5
c. push ax
d. pop ax
e. call
2. Write a recursive function to calculate the fibonaccii of a number. The number is passed as a parameter
via the stack and the calculated fibonaccii number is returned in the AX register. A local variable should
be used to store the return value from the first recursive call. Fibonaccii function is defined as follows:
fibonaccii(0) = 0
fibonaccii(1) = 1
fibonaccii(n) = fibonaccii(n-1) + fibonaccii(n-2)
Solution:
Following is the function that must be called in the program to return the Fibonacci of the number passed as
parameter.
fibonacci :
push bp
mov bp,sp
sub sp,2 ;creat local variable
sub bx,2
push bx ;cleared by ret 2
call fibonacci
return1:
mov ax,1
retfromfunc:
mov sp,bp ;restore sp-2, which was done for local variable
pop bp
ret 2 ;clear the stack (discard bx, pushed b4 calling this proc)