In: Computer Science
MIPS Assembly Language
Write a MIPS assembly language program that asks the user to input an integer and then prints out a string that shows how that integer should be encoded using 16 bits. Your program should handle both positive and negative valued inputs. Your program should also print out an error message if the given input cannot be expressed as a 16 bit signed integer.
As an example, if the input is 12, your program should output “0000000000001100”. If the input is 34000, your program should print out an error since the largest 16 bit signed integer is 215 - 1 = 32767.
.data
ask_str1: .asciiz "Enter a number between -32768 , 32767 :: "
printError: .asciiz "Number out of range!! Exiting...."
result_str: .asciiz ""
printMessage: .asciiz "Binary conversion is : "
.align 2
.text
.globl __start
__start:
# ask and store the first number
li $v0, 4
la $a0, ask_str1
syscall
li $v0, 5
syscall
bgt $v0,32767,exit
blt $v0,-32768,exit
move $s0, $v0
li $v0, 4
la $a0, printMessage
syscall
jal print_bin
# New Line
li $v0, 10
syscall
print_bin:
add $t0, $zero, $s0 # put our input ($a0) into $t0
add $t1, $zero, $zero # Zero out $t1
addi $t3, $zero, 1 # load 1 as a mask
sll $t3, $t3, 15 # move the mask to appropriate position
addi $t4, $zero, 16 # loop counter
loop:
and $t1, $t0, $t3 # and the input with the mask
beq $t1, $zero, print # Branch to print if its 0
add $t1, $zero, $zero # Zero out $t1
addi $t1, $zero, 1 # Put a 1 in $t1
j print
print: li $v0, 1
move $a0, $t1
syscall
srl $t3, $t3, 1
addi $t4, $t4, -1
bne $t4, $zero, loop
jr $ra
exit:
li $v0, 4
la $a0, printError
syscall
output: