Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS prog
ID: 3803761 • Letter: P
Question
Please write MIPS program that runs in QtSpim (ex: MipsFile.s)
Write a MIPS program that will read in a base (as an integer) and a value (nonnegative integer but as an ASCII string) in that base and print out the decimal value; you must implement a function (which accepts a base and an address for a string as parameters, and returns the value) and call the function from the main program. The base will be given in decimal and will be between 2 and 36, and the digits for the values will be in set [0…9, a…z]. You can assume that no overflow will occur and the value is valid in the given base.
An example run will be:
Enter a base (between 2 and 36 in decimal): 2
Enter a number in base 2: 101
The value in decimal is: 5
The program needs to be able to test 3 different cases and run in QtSpim like a .s file
here is a c program that shows the functionality
This must be MIPS program
Explanation / Answer
convert2dec(char*, int):
push rbp
mov rbp, rsp
mov QWORD PTR [rbp-24], rdi
mov DWORD PTR [rbp-28], esi
mov DWORD PTR [rbp-8], 0
mov DWORD PTR [rbp-4], 0
.L5:
mov eax, DWORD PTR [rbp-4]
movsx rdx, eax
mov rax, QWORD PTR [rbp-24]
add rax, rdx
movzx eax, BYTE PTR [rax]
cmp al, 13
jle .L2
mov eax, DWORD PTR [rbp-4]
movsx rdx, eax
mov rax, QWORD PTR [rbp-24]
add rax, rdx
movzx eax, BYTE PTR [rax]
cmp al, 57
jle .L3
mov eax, DWORD PTR [rbp-8]
imul eax, DWORD PTR [rbp-28]
mov edx, eax
mov eax, DWORD PTR [rbp-4]
movsx rcx, eax
mov rax, QWORD PTR [rbp-24]
add rax, rcx
movzx eax, BYTE PTR [rax]
movsx eax, al
add eax, edx
sub eax, 87
mov DWORD PTR [rbp-8], eax
jmp .L4
.L3:
mov eax, DWORD PTR [rbp-8]
imul eax, DWORD PTR [rbp-28]
mov edx, eax
mov eax, DWORD PTR [rbp-4]
movsx rcx, eax
mov rax, QWORD PTR [rbp-24]
add rax, rcx
movzx eax, BYTE PTR [rax]
movsx eax, al
add eax, edx
sub eax, 48
mov DWORD PTR [rbp-8], eax
.L4:
add DWORD PTR [rbp-4], 1
jmp .L5
.L2:
mov eax, DWORD PTR [rbp-8]
pop rbp
ret
.LC0:
.string "Please the base (in decimal): "
.LC1:
.string "Please enter a nonnegative number: "
.LC2:
.string "The decimal value is:"
main:
push rbp
mov rbp, rsp
sub rsp, 288
mov DWORD PTR [rbp-276], edi
mov QWORD PTR [rbp-288], rsi
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-8]
mov rsi, rax
mov edi, OFFSET FLAT:std::cin
call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
mov esi, OFFSET FLAT:.LC1
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-272]
mov rsi, rax
mov edi, OFFSET FLAT:std::cin
call std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char*)
mov edx, DWORD PTR [rbp-8]
lea rax, [rbp-272]
mov esi, edx
mov rdi, rax
call convert2dec(char*, int)
mov DWORD PTR [rbp-4], eax
mov esi, OFFSET FLAT:.LC2
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 eax, DWORD PTR [rbp-4]
mov esi, eax
mov edi, OFFSET FLAT:std::cout
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 .L11
cmp DWORD PTR [rbp-8], 65535
jne .L11
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
.L11:
nop
leave
ret
_GLOBAL__sub_I__Z11convert2decPci:
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret