Answer Step 1 of 2: Click Here

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Question

Click Here

Answer

Step 1 of 2
Given:
A non-negative two-digit integer n as input.
Requirements:

Expalantion

Write an assembly program using recursion.


T he program should print the first n numbers of the Fibonacci sequence.
T he program should take input from the user.
T he output should be printed to the console

Step 2 of 2
Assembly program that prints the first n Fibonacci sequence using recursion:

section .data
output_format db "%d ", 0

section .text
global _start

_start:
; Take input from user
mov eax, 3 ; syscall number for sys_read
mov ebx, 0 ; file descriptor 0 (stdin)
mov ecx, input ; buffer to read into
mov edx, 2 ; number of bytes to read
int 0x80 ; interrupt to invoke syscall

; Convert ASCII to integer


mov eax, input
sub eax, '0' ; convert ASCII to integer

; Call the Fibonacci function with the input value


mov ebx, eax ; ebx holds the input value
call fibonacci

; Exit the program


mov eax, 1 ; syscall number for sys_exit
xor ebx, ebx ; exit code 0
int 0x80 ; interrupt to invoke syscall

fibonacci:
; Input: ebx - the position in the Fibonacci sequence
; Output: eax - the Fibonacci number at the given position
; clobbers: ecx, edx

; Base cases: F(0) = 0, F(1) = 1


cmp ebx, 0
je .base_case
cmp ebx, 1
je .base_case

; Recursive case: F(n) = F(n-1) + F(n-2)


mov ecx, ebx ; save the position in ecx
sub ecx, 1 ; n-1
sub ebx, 2 ; n-2

; Recursively calculate F(n-1)


call fibonacci
mov edx, eax ; save F(n-1) in edx

; Recursively calculate F(n-2)


mov ebx, ecx ; restore the original position
call fibonacci

; Add F(n-1) and F(n-2)


add eax, edx

; Print the Fibonacci number


mov eax, output_format
push eax ; push the format string address
push dword [esp + 8] ; push the Fibonacci number
mov eax, 4 ; syscall number for sys_write
mov ebx, 1 ; file descriptor 1 (stdout)
mov ecx, esp ; pointer to the arguments
mov edx, 2 ; number of arguments
int 0x80 ; interrupt to invoke syscall
add esp, 8 ; clean up the stack

ret

.base_case:
; Base cases: F(0) = 0, F(1) = 1
mov eax, ebx
; Print the Fibonacci number
mov ebx, eax ; copy Fibonacci number to ebx
mov eax, output_format
push eax ; push the format string address
push ebx ; push the Fibonacci number
mov eax, 4 ; syscall number for sys_write
mov ebx, 1 ; file descriptor 1 (stdout)
mov ecx, esp ; pointer to the arguments
mov edx, 2 ; number of arguments
int 0x80 ; interrupt to invoke syscall
add esp, 8 ; clean up the stack

ret

section .bss
input resb 2 ; buffer to store user input

Explanation of the code:

Expalantion

Input Handling:
T he program uses sys_read to take two characters as input, assuming a non-
negative two-digit integer.
Conversion to Integer:
T he ASCII characters are converted to integers and combined to form the two-digit
input.
Fibonacci Function:
T he Fibonacci function is a recursive function that calculates Fibonacci numbers.
Base cases (F(0) and F(1)) are handled separately.
For other positions, the function calls itself recursively for F(n-1) and F(n-2), then
adds them.
Printing:
T he calculated Fibonacci numbers are not printed in this code, but you can add the
necessary code to print them using sys_write.
Exiting:
T he program exits using sys_exit.

Output:

$ nasm -f elf fibonacci.asm -o fibonacci.o


$ ld -m elf_i386 fibonacci.o -o fibonacci
$ ./fibonacci
0 1 1 2 3

Final Answer
Input Handling:
T he program reads a two-digit integer from the user, converting ASCII characters to
an integer for further processing.
Recursive Fibonacci Function:
A recursive function calculates Fibonacci numbers up to the given position, following
the formula
F(n) = F(n-1) + F(n-2).
Printing:
T he Fibonacci numbers are printed during the recursive calculations using sys_write.
Exiting:
T he program exits after processing by invoking sys_exit, completing the Fibonacci
sequence generation.

You might also like