Write a MIPS main program that calls a MIPS function \"calloc\". Your main progr
ID: 3916094 • Letter: W
Question
Write a MIPS main program that calls a MIPS function "calloc". Your main program and the calloc function must follow the function-call convention (without using the stack for parameter and return value transmission)
The function calloc allocates and initializes a dynamic integer array. The function gets the number of integers (say n) and the initial value for the array elements (say k), allocates a dynamic array of n integers, initializes each integer to the value k, and returns a pointer to the array.
Explanation / Answer
Solution
.zero 1
.LC0:
.string "p2["
.LC1:
.string "] == "
main:
push rbp
mov rbp, rsp
push rbx
sub rsp, 40
mov esi, 4
mov edi, 4
call calloc
mov QWORD PTR [rbp-32], rax
mov esi, 16
mov edi, 1
call calloc
mov QWORD PTR [rbp-40], rax
mov esi, 4
mov edi, 4
call calloc
mov QWORD PTR [rbp-48], rax
cmp QWORD PTR [rbp-40], 0
je .L2
mov DWORD PTR [rbp-20], 0
.L4:
cmp DWORD PTR [rbp-20], 3
jg .L2
mov eax, DWORD PTR [rbp-20]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-40]
add rax, rdx
mov ebx, DWORD PTR [rax]
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*)
mov rdx, rax
mov eax, DWORD PTR [rbp-20]
mov esi, eax
mov rdi, rdx
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:.LC1
mov rdi, rax
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*)
mov esi, ebx
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, 10
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)
add DWORD PTR [rbp-20], 1
jmp .L4
.L2:
mov rax, QWORD PTR [rbp-32]
mov rdi, rax
call free
mov rax, QWORD PTR [rbp-40]
mov rdi, rax
call free
mov rax, QWORD PTR [rbp-48]
mov rdi, rax
call free
mov eax, 0
add rsp, 40
pop rbx
pop rbp
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 .L8
cmp DWORD PTR [rbp-8], 65535
jne .L8
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
.L8:
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
Feel free to reach out regarding any queries . And please do rate the answer . Thank you .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.