Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MIPS Assembly (.asm) Number Converter: Write a MARS program that can perform con

ID: 3792757 • Letter: M

Question

MIPS Assembly (.asm)

Number Converter: Write a MARS program that can perform conversions between binary, decimal, and hexadecimal representations for positive integers (under 32 bits). Your program should accept a string that follows the following format -- invalid inputs or formats should be rejected. Format: , where the input and output types can be one of "b", "d", "h". Hex inputs will start with 0x and use lower case letters. You should then print the converted answer and prompt the user again (unless the user enters a string starting with e). Here is an example run of this program:
What would you like to convert?
d b 33
100001
What would you like to convert?
b h 100010
0x22
What would you like to convert?
h d 0x23
35
What would you like to convert?
d b 4b
Sorry, invalid format.
What would you like to convert?
f g 43
Sorry, invalid format.
What would you like to convert?
e
Goodbye.

Explanation / Answer

.LC0:

.string "Enter any decimal number: "

.LC1:

.string "%ld"

.LC2:

.string "Equivalent hexadecimal value of decimal number %d: "

main:

push rbp

mov rbp, rsp

sub rsp, 144

mov DWORD PTR [rbp-12], 1

mov edi, OFFSET FLAT:.LC0

mov eax, 0

call printf

lea rax, [rbp-32]

mov rsi, rax

mov edi, OFFSET FLAT:.LC1

mov eax, 0

call scanf

mov rax, QWORD PTR [rbp-32]

mov QWORD PTR [rbp-8], rax

.L5:

cmp QWORD PTR [rbp-8], 0

je .L2

mov rax, QWORD PTR [rbp-8]

cqo

shr rdx, 60

add rax, rdx

and eax, 15

sub rax, rdx

mov DWORD PTR [rbp-20], eax

cmp DWORD PTR [rbp-20], 9

jg .L3

add DWORD PTR [rbp-20], 48

jmp .L4

.L3:

add DWORD PTR [rbp-20], 55

.L4:

mov eax, DWORD PTR [rbp-12]

lea edx, [rax+1]

mov DWORD PTR [rbp-12], edx

mov edx, DWORD PTR [rbp-20]

cdqe

mov BYTE PTR [rbp-144+rax], dl

mov rax, QWORD PTR [rbp-8]

lea rdx, [rax+15]

test rax, rax

cmovs rax, rdx

sar rax, 4

mov QWORD PTR [rbp-8], rax

jmp .L5

.L2:

mov rax, QWORD PTR [rbp-32]

mov rsi, rax

mov edi, OFFSET FLAT:.LC2

mov eax, 0

call printf

mov eax, DWORD PTR [rbp-12]

sub eax, 1

mov DWORD PTR [rbp-16], eax

.L7:

cmp DWORD PTR [rbp-16], 0

jle .L6

mov eax, DWORD PTR [rbp-16]

cdqe

movzx eax, BYTE PTR [rbp-144+rax]

movsx eax, al

mov edi, eax

call putchar

sub DWORD PTR [rbp-16], 1

jmp .L7

.L6:

mov eax, 0

leave

ret

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote