The following ARM assembly program fragment is used to generate the Fibonacci se
ID: 3582666 • Letter: T
Question
The following ARM assembly program fragment is used to generate the Fibonacci sequence using an iterative procedure named myFn. By definition, the first 2 numbers of the Fibonacci sequence are set to 1 as (F F2-1) while each subsequent number is the sum of the previous two Fibonacci numbers: E n-1 F n-2 data n seq: word 1 1 text global main main rl base addr of n seq MOV r0, #13 assign r 0 (n) 13 call the iterative procedure myFn SWI 0x11 myFn CMP r0, #2 BLE Exit LDR r2 r1, #0 LDR 3, Irl, 4]; ADD r4, r2 r3 Exit return to the caller end Complete the above program fragment to successfully generate the Fibonacci sequence. ii) The starting address of n seg is at 0000103c Copy Table 3c into your answer book and then fill in the missing hexadecimal values of the corresponding registers after the successful execution of the complete program in i). Table 3c Registers Hexadecimal Values r 1 r2 r 38 r4 iii) Clearly state the hexadecimal value of r2 after the successful program execution if the underlined instruction is moved as the first statement in the procedure myFn of the complete program as below. myEn LDR r2 r1, #0 CMP r0, #2;Explanation / Answer
1)
std::piecewise_construct:
.zero 1
myfn(int):
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-20], edi
mov DWORD PTR [rbp-4], 1
mov DWORD PTR [rbp-8], 1
mov DWORD PTR [rbp-12], 1
mov DWORD PTR [rbp-16], 3
.L3:
mov eax, DWORD PTR [rbp-16]
cmp eax, DWORD PTR [rbp-20]
jg .L2
mov edx, DWORD PTR [rbp-8]
mov eax, DWORD PTR [rbp-4]
add eax, edx
mov DWORD PTR [rbp-12], eax
mov eax, DWORD PTR [rbp-8]
mov DWORD PTR [rbp-4], eax
mov eax, DWORD PTR [rbp-12]
mov DWORD PTR [rbp-8], eax
add DWORD PTR [rbp-16], 1
jmp .L3
.L2:
mov eax, DWORD PTR [rbp-12]
pop rbp
ret
.LC0:
.string "Enter the integer n to find nth fibonnaci no.(0 to exit): "
main:
push rbp
mov rbp, rsp
sub rsp, 16
.L8:
mov esi, OFFSET FLAT:.LC0
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
lea rax, [rbp-4]
mov rsi, rax
mov edi, OFFSET FLAT:std::cin
call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
mov eax, DWORD PTR [rbp-4]
test eax, eax
je .L11
mov eax, DWORD PTR [rbp-4]
mov edi, eax
call myfn(int)
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> >&))
jmp .L8
.L11:
nop
mov eax, 0
leave
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 .L14
cmp DWORD PTR [rbp-8], 65535
jne .L14
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
.L14:
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
2)
R1 0xcafe
R2 0xbabe
R3 0xdace
R4 0xfaee
3)
r2 0xbabe
Load upper 16 bits with 0xbabe