Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS prog
ID: 3803648 • 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
Explanation / Answer
Answer:
MIPS Assembly Language Program :
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 "%d"
.LC2:
.string "Please enter a nonnegative number: "
.LC3:
.string "%s"
.LC4:
.string "The decimal value is %d. "
main:
push rbp
mov rbp, rsp
sub rsp, 288
mov DWORD PTR [rbp-276], edi
mov QWORD PTR [rbp-288], rsi
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
lea rax, [rbp-4]
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call scanf
mov edi, OFFSET FLAT:.LC2
mov eax, 0
call printf
lea rax, [rbp-272]
mov rsi, rax
mov edi, OFFSET FLAT:.LC3
mov eax, 0
call scanf
mov edx, DWORD PTR [rbp-4]
lea rax, [rbp-272]
mov esi, edx
mov rdi, rax
call convert2dec(char*, int)
mov esi, eax
mov edi, OFFSET FLAT:.LC4
mov eax, 0
call printf
mov eax, 0
leave
ret