I have a working program that multiplies integers to give a result in MIPS. My o
ID: 3632068 • Letter: I
Question
I have a working program that multiplies integers to give a result in MIPS. My only problem is that I need the result to be printed in hex not decimal format. Below is my program, followed by another program that converts from decimal to hex but I need a way to implement the second (conversion) program into the first. Thanks!First program (the multiplication program):
.data
out_string: .asciiz "The result is: "
.text
main: li $v0, 5
syscall
move $t0, $v0
li $v0, 5
syscall
move $t1, $v0
li $v0, 5
syscall
move $t2, $v0
li $v0, 5
syscall
mult $t2, $v0
mfhi $t3
mflo $t2
mult $t0, $t1
mfhi $t1
mflo $t0
mult $t3, $t1 #hi
mflo $t1
mult $t2, $t0 #lo
mflo $t0
mfhi $t2
li $v0, 4
la $a0, out_string
syscall
li $v0, 1
move $a0, $t1
syscall
li $v0, 1
move $a0, $t2
syscall
li $v0, 1
move $a0, $t0
syscall
li $v0, 10
syscall
-----------------------------------------------------------------------
Second program (converts from decimal to hex, needs to be implemented into first program):
.data
prompt: .asciiz "Enter the decimal number to convert: "
ans: .asciiz " Hexadecimal equivalent: "
result: .space 8
.text
.globl main
main:
la $a0, prompt
li $v0, 4
syscall
li $v0, 5
syscall
move $t2, $v0
la $a0, ans
li $v0, 4
syscall
li $t0, 8 # counter
la $t3, result # where answer will be stored
Loop:
beqz $t0, Exit # branch to exit if counter is equal to zero
rol $t2, $t2, 4 # rotate 4 bits to the left
and $t4, $t2, 0xf # mask with 1111
ble $t4, 9, Sum # if less than or equal to nine, branch to sum
addi $t4, $t4, 55 # if greater than nine, add 55
b End
Sum:
addi $t4, $t4, 48 # add 48 to result
End:
sb $t4, 0($t3) # store hex digit into result
addi $t3, $t3, 1 # increment address counter
addi $t0, $t0, -1 # decrement loop counter
j Loop
Exit:
la $a0, result
li $v0, 4
syscall
la $v0, 10
syscall
------------------------------------------------------------------
Explanation / Answer
Dear.. # Write a MIPS code that asks the user for decimal number# Convert it to hex and print the result
.data prompt: .asciiz "Enter the decimal number to convert: " ans: .asciiz " Hexadecimal equivalent: " result: .space 8 .text .globl main main: la $a0, prompt li $v0, 4 syscall li $v0, 5 syscall move $t2, $v0 la $a0, ans li $v0, 4 syscall li $t0, 8 # counter la $t3, result # where answer will be stored Loop: beqz $t0, Exit # branch to exit if counter is equal to zero rol $t2, $t2, 4 # rotate 4 bits to the left and $t4, $t2, 0xf # mask with 1111 ble $t4, 9, Sum # if less than or equal to nine, branch to sum addi $t4, $t4, 55 # if greater than nine, add 55 b End Sum: addi $t4, $t4, 48 # add 48 to result End: sb $t4, 0($t3) # store hex digit into result addi $t3, $t3, 1 # increment address counter addi $t0, $t0, -1 # decrement loop counter j Loop Exit: la $a0, result li $v0, 4 syscall li $v0, 10 syscall Exit: la $a0, result li $v0, 4 syscall li $v0, 10 syscall what you give the code is correct. Just need to change the code instead of la $v0, 10, you should have li $v0, 10.
i give u the samplecode for multiplication of two integers. mult proc; multiplies eax by ebx and places result in edx:ecx
xor ecx, ecx
xor edx, edx mul1:
test ebx, 1 jz mul2 add ecx, eax adc edx, 0 mul2: shr ebx, 1 shl eax, 1 test ebx, ebx jnz mul1 done: ret mult endp
i give u the samplecode for multiplication of two integers. mult proc; multiplies eax by ebx and places result in edx:ecx
xor ecx, ecx
xor edx, edx mul1:
test ebx, 1 jz mul2 add ecx, eax adc edx, 0 mul2: shr ebx, 1 shl eax, 1 test ebx, ebx jnz mul1 done: ret mult endp
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.