Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Assignment 4-recursive GCD function in ASM and an iterative- non-recursive- procedure -Fibonacci number sequence

Click Here to enroll in this course now


Assembly language x86 processors for beginners

60-266 – Assignment #4

Programming Exercise 1
The greatest common divisor (GCD) of two integers X and Y is the largest integer Z that will evenly divide both integers. The GCD algorithm involves integer division in a loop.
Write the GCD function in ASM. Your main program must call this function with 32-bit unsigned integers X and Y. GCD function should return the value Z (GCD of X and Y) in register EAX, and the main program should display this value Z.
Your main calls your GCD procedure 5 times, using the following pairs (5, 20), (24, 18), (11, 7), (432, 226), (0, 0). After each procedure call, main displays the GCD of the input pair.
Two different pseudocodes of the GCD algorithm are given below. Implement whichever pseudocode into ASM program.
function GCD(a, b) while b ≠ 0 t := b; b := a mod b; a := t; return a;
Or
function GCD(a, b) while a ≠ b if a > b a := a − b; else b := b − a; return a;

Answer(partial):

INCLUDE Irvine32.inc

.data
	array DWORD  5, 20, 24, 18, 11, 7, 432, 226, 0, 0
	str1 BYTE "GCD is: ", 0
	
.code
main PROC

	mov  ecx, LENGTHOF array/2
	mov  esi, OFFSET array
   L1:	
	mov  eax, [esi]
	mov  ebx, [esi + 4]

	add  esi, TYPE array * 2
   loop L1

exit
main ENDP

GCD PROC
	push ebx
	push edx
	cmp ebx,0
	jne L1
	JMP more
	
   L1 : 
	mov  edx, 0

	mov  ebx, edx; next iteration
	jmp  L1
	
   more:
	mov eax, ebx; EAX = GCD
	pop edx
	pop ebx
		
	ret
		
GCD ENDP

END main

Programming Exercise 2
Write the recursive GCD function in ASM. Your main program must call this function with 32-bit unsigned parameters X and Y. GCD function should return the value Z (GCD of X and Y) in register EAX, and the main program should display this value Z.
Your main calls you’re the recursive GCD procedure 5 times, using the following pairs (5, 20), (24, 18), (11, 7), (432, 226), (0, 0). After each procedure call, main displays the GCD of the input pair.
The pseudocode of the recursive GCD algorithm is given below.
function gcd(a, b) if b = 0 return a; else return gcd(b, a mod b);

Answer(partial):

INCLUDE Irvine32.inc

.data
	array DWORD  5, 20, 24, 18, 11, 7, 432, 226, 0, 0
	str1 BYTE "GCD is: ", 0

.code
main PROC

	mov  ecx, LENGTHOF array/2
	mov  esi, OFFSET array
   L1:	
	mov  eax, [esi]

	add  esi, TYPE array * 2
	loop L1

exit
main ENDP

GCD PROC
	push ebp

	JMP Zero
	
   L1 : 
	mov  edx, 0
	div  ebx; divide int1 by int2

	mov  eax, ebx; no: prepare for  next iteration
	mov  ebx, edx
	call GCD
	
   Zero:
   
	mov eax, ebx; EAX = GCD
	pop ebp	
	ret 
		
GCD ENDP

END main

Programming Exercise 3
Write an iterative (that is, a non-recursive) procedure for calculating the factorial of an integer number N. Also, you should write a MAIN procedure that calls the factorial procedure with parameter N, and display the result.

Answer(partial):

INCLUDE Irvine32.inc

.data
	msg1 BYTE "Please enter a number greater than 0 and less than 18 to calculate the factorial "
	BYTE " (0 to quit): ", 0
	msg2 BYTE "The factorial is: ", 0

.code
main PROC

   L1:
	mov edx, OFFSET msg1; prompt the user
	call WriteString
	call ReadInt; get a value N from the user
	call Crlf

	call WriteDec; display factorial
	call Crlf

   loop L1
  quit:
exit
main ENDP

Factorial PROC

.data
notValid  BYTE "Error ", 0

.code

	cmp eax,1
	jne next

	je endof
	
   next:
	cmp eax, 18
	
	jmp endof; quit procedure

   more:
	mov ecx, eax;  this is a counter
   L2:
	dec ecx; ecx = n - 1
	
   endof:
ret

Factorial ENDP

END main

Programming Exercise 4
Write an ASM program that reads an integer N and then displays the first N values of the Fibonacci number sequence, described by:
Fib(0) = 0, Fib(1) = 1, Fib(N) = Fib(N-2) + Fib(N-1)
Thus, if the input is N = 10, your program Ass4-Q1.exe should display the following single line:
Fibonacci sequence with N = 10 is: 0 1 1 2 3 5 8 13 21 34 55.
Your MAIN program should call a recursive Fib procedure with parameter k, and the Fib procedure should return the k-th Fibonacci number in register EAX, and then the MAIN program should display the k-th Fibonacci number in the sequence.
You may want to declare the Fibonacci sequence as an array of maximum size 10, and have the Fib procedure to compute the value of each cell of the array. Also, there are other ways to do this.

Answer(partial):

include Irvine32.inc

.data
msg1 byte "Please enter a number", 0dh, 0ah, 0
msg2 byte "Febonacci sequence is: ", 0
spce byte "  ", 0

.code
main PROC
mov ecx, 0
mov edx, OFFSET msg1
call WriteString
call ReadInt
mov ecx, eax; print N numbers
mov edx, eax
push ecx
push edx

mov edx, OFFSET msg2
call WriteString
pop edx
pop ecx
mov ecx, 0
mov ecx, edx
inc ecx
L1 :
jecxz quit

loop L1
call crlf
quit :
exit
main ENDP

fib PROC C
push ebp
mov  ebp, esp


add esp, 4; clean up the stack
add eax, [ebp - 4]; add result and stored first result
jmp Quit
next :
mov eax, 1; start values : 1, 1
Quit :
	mov esp, ebp; restore esp
	pop ebp; restore ebp
	ret; return EAX, stack not cleaned up
	fib ENDP

	END main

Click Here to enroll in this course now


Assembly language x86 processors for beginners

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!