Question: Please fix my code to work as expected Question 1 segment . bssfib _ array resq 5 ; space for 5 fibonacci numberssegment . textglobal
Please fix my code to work as expected
Question
segment bssfibarray resq ; space for fibonacci numberssegment textglobal mainmain: mov qword fibarray ; store fib mov qword fibarray ; store fib mov rcx ; start at fibstart: cmp rcx ; stop at fib jge end ; exit loop if rcx greater than push rcx ; save current index call fibonacci ; call fibrcx pop rcx ; restore index mov fibarray rcx rax ; store result in fibarray inc rcx ; next index jmp start ; repeatend: leave retfibonacci: cmp rcx ; check if rcx is je basecasezero ; return cmp rcx ; check if rcx is je basecaseone ; return push rcx ; save rcx dec rcx ; rcx call fibonacci ; compute fibrcx mov rbx rax ; save fibrcx result in rbx pop rcx ; restore rcx push rcx ; save rcx again sub rcx ; rcx call fibonacci ; fibrcx add rax, rbx ; add fibrcx fibrcx pop rcx ; restore rcx ret ; return resultbasecasezero: mov rax, ; fib retbasecaseone: mov rax, ; fib retI wrote this program to compute and store the first Fibonacci numbers in memory. I reserved space for numbers usingresqand initialized the first two values asand Then, I used a loop to calculatefibthroughfibby calling a recursive function that computes each Fibonacci number as the sum of the two preceding numbers. The result of each calculation is stored in thefibarray. Everything worked as expected, and there were no errors when running this code. The program correctly calculates and stores the Fibonacci numbers.Palindrome Question section datastr db "refer", ; input stringlen equ $ str ; length of the stringsection bssispalindrome resb ; result: palindrome, notsection textglobal startstart: ; Set up the stack frame push rbp ; save base pointer mov rbp rsp ; set base pointer to stack pointer lea rsi, str ; rsi points to the start of the string lea rdi, str len ; rdi points to the end of the string mov bl ; assume its a palindrome set result to checkloop: cmp rsi, rdi ; stop when start pointer meets end pointer jg palindrome ; if pointers cross, it's a palindrome mov alrsi ; load character from the start mov ahrdi; load character from the end cmp al ah ; compare characters jne notpalindrome ; if not equal, it's not a palindrome inc rsi ; move start pointer forward dec rdi ; move end pointer backward jmp checkloop ; repeat the looppalindrome: mov ispalindrome bl ; store palindrome jmp exitnotpalindrome: mov bl ; set result to not a palindrome mov ispalindrome bl jmp exitexit: leave ; clean up the stack frame ret ; return
these are the comments of my grader
The result is really close, but it doesn't match the Fibonacci sequence
Again the result is really close, there are just some small syntax errors causing problems:Use of keyword as variable name causes errors during execution, change str to string to fix itlea rdi, str len needs to belea rdi, str len as $ returns the address after the string, so will point to the null terminator and will point to the last char.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
