MARIE assembly code for 7th value of Fibonacci sequence Fib1
M.A.R.I.E assembly code for 7th value of Fibonacci sequence: Fib(1) = 1, Fib(2) = 1, Fib(n) = Fib(n-1) + Fib(n-2). The program calculates Fib(7).
I have 4 errors in my code and cannot seem to find the solutions
here is the code so far,
ORG 100
Input
Store x
Input
Store y
FOR_INIT, LOAD one
STORE i
FOR_COND, LOAD i
SUBT seven
SKIPCOND 800
JUMP FOR_BODY
JUMP END_FOR
FOR_BODY, LOAD x
ADD y
STORE z
LOAD x
STORE y
LOAD y
STORE x
LOAD i
ADD one
STORE i
JUMP FOR_COND
END_FOR, LOAD z
OUTPUT
HALT
zero, Dec 0
one, Dec 1
seven, Dec 7
x, Dec 0
y, Dec 1
z, Dec 0
Solution
Answer:
MIPS Assembly Language Code :
.zero 1
L0:
push rbp
mov rbp, rsp
push rbx
sub rsp, 24
mov DWORD PTR [rbp-20], edi
cmp DWORD PTR [rbp-20], 1
je .L2
cmp DWORD PTR [rbp-20], 2
jne .L3
.L2:
mov eax, 1
jmp .L4
.L3:
mov eax, DWORD PTR [rbp-20]
sub eax, 1
mov edi, eax
call L0
mov ebx, eax
mov eax, DWORD PTR [rbp-20]
sub eax, 2
mov edi, eax
call L0
add eax, ebx
.L4:
add rsp, 24
pop rbx
pop rbp
ret
main:
push rbp
mov rbp, rsp
mov edi, 7
call L0
mov esi, eax
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))
mov eax, 0
pop rbp
ret
__static_initialization_and_destruction_0(int, int):
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], edi
mov DWORD PTR [rbp-8], esi
cmp DWORD PTR [rbp-4], 1
jne .L9
cmp DWORD PTR [rbp-8], 65535
jne .L9
mov edi, OFFSET FLAT:std::__ioinit
call std::ios_base::Init::Init()
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:std::__ioinit
mov edi, OFFSET FLAT:std::ios_base::Init::~Init()
call __cxa_atexit
.L9:
nop
leave
ret
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret


