In: Computer Science
Write a MIPS assembly language program that inputs a floating-point number and shows its internal 32-bit representation as a 8-digit hexadecimal representation. For example: 2.5 decimal is 10.1 binary, which normalized is 1.01x21 and would be stored in the IEEE format as 0100 0000 0010 0000 0000 0000 0000 0000 which is 0x40200000
.data
msg: .asciiz "\nPlease enter an unsigned integer: " #input
prompt
array: .space 20 #reserve space for 5-integer array
.text
main:
la $t3, array #load pointer for traversing array
la $s0, array #load initial position of array
#initialize counter for loop
add $t0, $t0, $zero #set counter to 0
li $t1, 5 #set maximum number of passes to 5
#initialize counter for hexadecimal loop
add $t4, $t4, $zero #set counter to 0
la $t5, array #load pointer for traversing array
#loop to insert numbers into array
insert_loop:
beq $t0, $t1, hexadecimal_loop #if $t0 = $t1 then exit loop
li $v0, 4 #system call code for printing string
la $a0, msg
syscall
li $v0, 5 #system call for reading integer
syscall
move $t2, $v0 #move read integer to $t2
bltz $t2, error_msg #bltz = branch if ($t2) < zero
bgt $t2, 32768, error_msg #branch if t2 > 32768
sw $t2, ($t3) #stores t2 in t3 array
addi $t3, $t3, 4 #increment array by 4 (move to next index)
addi $t0, $t0, 1 #increment counter by 1
j insert_loop
#loop for hexadecimal subprogram
hexadecimal_loop:
add $s2, $s2, $zero #set counter for loop
li $s3, 5 #set max number of passes to 5
beq $t4, $t1, exit #when $t4 = $t1, then exit loop
lw $s1, ($t5) #store array value into $s1
add $a0, $zero, $s1 #"move" value to $a0 to use as parameter
jal hexadecimal
addi $t5, $t5, 4 #increment array by 4 (move to next index)
#hexadecimal subprogram
hexadecimal:
add $t5, $a0, $zero #copy parameter to $t5
beq $s2, $s3, return_result #when $s2 = $s3 then exit loop
andi $t6, $t5, 15 #logical AND on $t5 and 15
addi $t7, $t7, 10 #variable to check again
bgt $t6, $t7, more_than_ten #if $t6 > 10
blt $t6, $t7, less_than_ten #if $t6 < 10
addi $s2, $s2, 1 #increment counter by 1
#error message display
error_msg:
li $v0, 4
la $a0, error
syscall
j insert_loop
less_than_ten:
addi $t7, $t6, 10
srl $t5, $t5, 4
j hexadecimal
more_than_ten:
addi $t7, $t6, -10
addi $t7, $t7, 65
srl $t5, $t5, 4
j hexadecimal
exit:
li $v0, 10 # System call code for exiting program
syscall