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

Write a MIPS program(Mars) that prompts the user for a 4-bit binary number, read

ID: 3602810 • Letter: W

Question

Write a MIPS program(Mars) that prompts the user for a 4-bit binary number, reads it as a string, and display the hexadecimal representation of the binary number. When calculating the value, use a loop to process the 4-character input string. Assume the user enters a valid binary number.

Hints:
- You need to print a single character at the end: either ‘0’ to ‘9’ or ‘A’ to ‘F’
- To convert from decimal to character representing a hex digit, add either 0x30 if the decimal value is from 0 to 9 or 0x37 if the decimal value is from 10 to 15.

Sample run (running the program 2 times)] 0111 7 program is finished running (dropped off bottom) 1110 program is finished running (dropped off bottom)

Explanation / Answer

.data

question: .asciiz "Enter a 4 bit binary number: "
line: .asciiz " "
answer: .asciiz "Answer in hexadecimal: "
result: .space 5

.text

main:

la $a0, question
li $v0, 4
syscall #prints question

li $v0, 5
syscall #stores user integer in $v0

move $t0, $v0 #puts in temp
sw $t0, result #stores in result

la $a0, line
li $v0, 4
syscall #new line

la $a0, answer
syscall #prints answer statement

li $t1, -1 #initialize $t1 to -1 so it will be 0 first loop (array index)

loop:
addi $t1, $t1, 1 #counter
beq $t1, 4, done #done 4 bits, over
lb $t2, result($t1)
beq $t2, 0, loop #if value is zero we don't need to add

beq $t1, 0, low
li $t4, 1 #revert to 2
move $t5, $t1 #$t5 same as $t1 except used to count down power

pow:

mul $t4, $t4, 2
addi $t5, $t5, -1
bne $t5, 0, pow
add $t3, $t3, $t4
b loop

low: #used for the first bit

add $t3, $t3, 1
b loop

done:

li $v0, 10
syscall

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote