Assembly language programming Programming assignment 1 Write a program to calcul
ID: 3726492 • Letter: A
Question
Assembly language programming
Programming assignment 1
Write a program to calculate the Nth number in the Fibonacci Sequence. You may use the any of the frameworks provided on blackboard in the frameworks zip(windows 32). If you use console 32 or console 64 the correct number must be placed in the EAX register before you move 0 into EAX to return from the procedure. The N to select which number in the sequence to calculate must be declared in the data segment for console 32 and console 64 applications. Submissions that use the Windows 32 or Windows 64 to ask the user for N and display the output will receive an addition 4 bonus points.
here is a sample to show you how the programming language looks like:
; program to find sum 1+2+. . .+n for n-l, 2, . . . 586 .MODEL FLAT STACK 4096 DATA CODE PROC mov mov main ebx, 0 eax, 0 ; number- 0 ;sum := 0 forever: inc add mp ebx eax, ebX forever ; add 1 to number ; add number to sum ; repeat main ENDP ENDExplanation / Answer
Solution in C programming:
//Fibonacci Series using Dynamic Programming
#include<stdio.h>
int fib(int n)
{
/* Declare an array to store Fibonacci numbers. */
int f[n+1];
int i;
/* 0th and 1st number of the series are 0 and 1*/
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++)
{
/* Add the previous 2 numbers in the series
and store it */
f[i] = f[i-1] + f[i-2];
}
return f[n];
}
int main ()
{
int n ;
scanf("%d", n);
printf("%d", fib(n));
getchar();
return 0;
}
Solution in x86 gcc:
fib:
push rbp
mov rbp, rsp
push rbx
sub rsp, 56
mov DWORD PTR [rbp-52], edi
mov rax, rsp
mov rsi, rax
mov eax, DWORD PTR [rbp-52]
add eax, 1
movsx rdx, eax
sub rdx, 1
mov QWORD PTR [rbp-32], rdx
movsx rdx, eax
mov r8, rdx
mov r9d, 0
movsx rdx, eax
mov rcx, rdx
mov ebx, 0
cdqe
sal rax, 2
lea rdx, [rax+3]
mov eax, 16
sub rax, 1
add rax, rdx
mov ebx, 16
mov edx, 0
div rbx
imul rax, rax, 16
sub rsp, rax
mov rax, rsp
add rax, 3
shr rax, 2
sal rax, 2
mov QWORD PTR [rbp-40], rax
mov rax, QWORD PTR [rbp-40]
mov DWORD PTR [rax], 0
mov rax, QWORD PTR [rbp-40]
mov DWORD PTR [rax+4], 1
mov DWORD PTR [rbp-20], 2
jmp .L2
.L3:
mov eax, DWORD PTR [rbp-20]
lea edx, [rax-1]
mov rax, QWORD PTR [rbp-40]
movsx rdx, edx
mov ecx, DWORD PTR [rax+rdx*4]
mov eax, DWORD PTR [rbp-20]
lea edx, [rax-2]
mov rax, QWORD PTR [rbp-40]
movsx rdx, edx
mov eax, DWORD PTR [rax+rdx*4]
add ecx, eax
mov rax, QWORD PTR [rbp-40]
mov edx, DWORD PTR [rbp-20]
movsx rdx, edx
mov DWORD PTR [rax+rdx*4], ecx
add DWORD PTR [rbp-20], 1
.L2:
mov eax, DWORD PTR [rbp-20]
cmp eax, DWORD PTR [rbp-52]
jle .L3
mov rax, QWORD PTR [rbp-40]
mov edx, DWORD PTR [rbp-52]
movsx rdx, edx
mov eax, DWORD PTR [rax+rdx*4]
mov rsp, rsi
mov rbx, QWORD PTR [rbp-8]
leave
ret
.LC0:
.string "%d"
main:
push rbp
mov rbp, rsp
sub rsp, 16
mov eax, DWORD PTR [rbp-4]
mov esi, eax
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call __isoc99_scanf
mov eax, DWORD PTR [rbp-4]
mov edi, eax
call fib
mov esi, eax
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
call getchar
mov eax, 0
leave
ret
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.