In: Computer Science
public static void main(String [] args)
{
int[] a = new int[20];
a[0] = 0;
a[1] = 1;
for(int i = 2; i < 20; i++){
a[i] = a[i - 1] + a[i - 2];
}
for(int i = 0; i < a.length; i++)
System.out.println(a[i]);
}
what would be the code when you convert this code, which is java into assembly code? Convert java into assembly code for the code above
Answer:
The Assembly Language Code For The Given Java Code Is Given As Below :
push rbp
mov rbp, rsp
sub rsp, 96
mov DWORD PTR [rbp-96], 0
mov DWORD PTR [rbp-92], 1
mov DWORD PTR [rbp-4], 2
.L3:
cmp DWORD PTR [rbp-4], 19
jg .L2
mov eax, DWORD PTR [rbp-4]
sub eax, 1
cdqe
mov edx, DWORD PTR [rbp-96+rax*4]
mov eax, DWORD PTR [rbp-4]
sub eax, 2
cdqe
mov eax, DWORD PTR [rbp-96+rax*4]
add edx, eax
mov eax, DWORD PTR [rbp-4]
cdqe
mov DWORD PTR [rbp-96+rax*4], edx
add DWORD PTR [rbp-4], 1
jmp .L3
.L2:
mov DWORD PTR [rbp-8], 0
.L5:
cmp DWORD PTR [rbp-8], 19
jg .L4
mov eax, DWORD PTR [rbp-8]
cdqe
mov eax, DWORD PTR [rbp-96+rax*4]
mov esi, eax
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char>
>::operator<<(int)
add DWORD PTR [rbp-8], 1
jmp .L5
.L4:
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 .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
_GLOBAL__sub_I_main:
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret