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 1
segment .bssfib_array resq 5 ; space for 5 fibonacci numberssegment .textglobal mainmain: mov qword [fib_array],0 ; store fib(0)=0 mov qword [fib_array +8],1 ; store fib(1)=1 mov rcx,2 ; start at fib(2)start: cmp rcx,5 ; stop at fib(5) jge end ; exit loop if rcx greater than 5 push rcx ; save current index call fibonacci ; call fib(rcx) pop rcx ; restore index mov [fib_array + rcx *8], rax ; store result in fib_array inc rcx ; next index jmp start ; repeatend: leave retfibonacci: cmp rcx,0 ; check if rcx is 0 je base_case_zero ; return 0 cmp rcx,1 ; check if rcx is 1 je base_case_one ; return 1 push rcx ; save rcx dec rcx ; rcx -1 call fibonacci ; compute fib(rcx -1) mov rbx, rax ; save fib(rcx -1) result in rbx pop rcx ; restore rcx push rcx ; save rcx again sub rcx,2 ; rcx -2 call fibonacci ; fib(rcx -2) add rax, rbx ; add fib(rcx -1)+ fib(rcx -2) pop rcx ; restore rcx ret ; return resultbase_case_zero: mov rax, 0 ; fib(0)=0 retbase_case_one: mov rax, 1 ; fib(1)=1 retI wrote this program to compute and store the first 5 Fibonacci numbers in memory. I reserved space for 5 numbers usingresqand initialized the first two values as0and1. Then, I used a loop to calculatefib(2)throughfib(4)by 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 thefib_array. 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", 0 ; input stringlen equ $ - str ; length of the stringsection .bssis_palindrome resb 1 ; result: 1= palindrome, 0= notsection .textglobal _start_start: ; 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 -1]; rdi points to the end of the string mov bl,1 ; assume its a palindrome (set result to 1)check_loop: cmp rsi, rdi ; stop when start pointer meets end pointer jg palindrome ; if pointers cross, it's a palindrome mov al,[rsi] ; load character from the start mov ah,[rdi]; load character from the end cmp al, ah ; compare characters jne not_palindrome ; if not equal, it's not a palindrome inc rsi ; move start pointer forward dec rdi ; move end pointer backward jmp check_loop ; repeat the looppalindrome: mov [is_palindrome], bl ; store 1(palindrome) jmp exitnot_palindrome: mov bl,0 ; set result to 0(not a palindrome) mov [is_palindrome], bl jmp exitexit: leave ; clean up the stack frame ret ; return
these are the comments of my grader
1 The result is really close, but it doesn't match the Fibonacci sequence .
2.-3 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 it.lea rdi, [str + len -1] needs to belea rdi, [str + len -2] as $ returns the address after the string, so -1 will point to the null terminator and -2 will point to the last char.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!