Uploaded by arshadwaqas03

Computer Architecture and Assembly Language Programming - CS401 Spring 2007 Assignment 02 Solution

advertisement
Assignment No. 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.
Note: For any query about the assignment, contact at cs401@vu.edu.pk
GUD LUCK
Marks: 15
Question:
Marks [5+10 = 15]
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
mov bx, [bp+4]
; get passed argument
cmp bx,0
jle return0
; if num<=0
; fibonacci=0
cmp bx,1
je return1
; if num==10
; fibonacci=1
; else
dec bx
push bx
call fibonacci
;cleared by ret 2
mov [bp-2],ax ;save ax, in local var
mov bx,
sub bx,2
push bx
call fibonacci
[bp+4] ; get passed argument
;cleared by ret 2
mov dx,[bp-2] ;get previous val of ax, saved above
add dx,ax
mov ax,dx
; return fibonacci in ax
jmp retfromfunc
return0:
mov ax,0
jmp retfromfunc
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)
Note: This Assignment covers Lecture# 07 to 15.
Deadline
Your assignment must be uploaded/submitted at or before Wednesday 11 April, 2007.
Download