You are on page 1of 2

Assignment No.

02
Total Marks: 15
SEMESTER SPRING 2007
CS401-Computer Architecture & Assembly Language Programming Due Date: 11/04/07

Instructions
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]
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

cmp bx,0 ; if num<=0

jle return0 ; fibonacci=0

cmp bx,1 ; if num==10

je return1 ; fibonacci=1
; else
dec bx
push bx ;cleared by ret 2
call fibonacci

mov bx, [bp+4] ; get passed argument

sub bx,2
push bx ;cleared by ret 2
call fibonacci

mov dx,[bp-2] ;get previous val of ax, saved above

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)