In: Computer Science
Write a MIPS assembly program that calculates the sum of all the elements in the following array: int array[10]={12,21,3,40,15,6,17,8,29,10}
MIPS Code:
main:
push rbp
mov rbp, rsp
sub rsp, 48
mov DWORD PTR [rbp-48], 12
mov DWORD PTR [rbp-44], 21
mov DWORD PTR [rbp-40], 3
mov DWORD PTR [rbp-36], 40
mov DWORD PTR [rbp-32], 15
mov DWORD PTR [rbp-28], 6
mov DWORD PTR [rbp-24], 17
mov DWORD PTR [rbp-20], 8
mov DWORD PTR [rbp-16], 29
mov DWORD PTR [rbp-12], 10
mov DWORD PTR [rbp-4], 0
mov DWORD PTR [rbp-8], 0
.L3:
cmp DWORD PTR [rbp-8], 9
jg .L2
mov eax, DWORD PTR [rbp-8]
cdqe
mov eax, DWORD PTR [rbp-48+rax*4]
add DWORD PTR [rbp-4], eax
add DWORD PTR [rbp-8], 1
jmp .L3
.L2:
mov eax, DWORD PTR [rbp-4]
mov esi, eax
mov edi, OFFSET FLAT:_ZSt4cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
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 .L7
cmp DWORD PTR [rbp-8], 65535
jne .L7
mov edi, OFFSET FLAT:_ZStL8__ioinit
call std::ios_base::Init::Init() [complete object constructor]
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:_ZStL8__ioinit
mov edi, OFFSET FLAT:_ZNSt8ios_base4InitD1Ev
call __cxa_atexit
.L7:
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
If you have any doubts, feel free to ask
Happy learning