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

Write a MIPS assembly language program that asks the user to input an integer an

ID: 3855380 • Letter: W

Question

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.

Please include comments as this is a beginner's class and assembly language is quite hard to follow without them

Explanation / Answer

    .data

prompt: .asciiz "enter the decimal number to convert: but less than 32767: "
ans: .asciiz " 16-bit binary equivalent: "
err: .asciiz "ERROR...TRY AGAIN "
one: .asciiz "1"
zer: .asciiz "0"

   .text
   .globl main

main:
   la $a0,prompt
   li $v0,4
   syscall

   li $v0,5
   syscall

   move $t2,$v0
   bg $t2,32767,WRONG

   la $a0,ans
   li $v0,4
   syscall


   sll $t2,$t2,16

   li $t3,16

LOOP:
   and $t4,$t2,0x80000000
   bne $t4,$0,P_ONE

P_ZERO:
   la $a0,zer
   li $v0,4
   syscall

   b LOOP_COUNTER

P_ONE:
   la $a0,one
   li $v0,4
   syscall

LOOP_COUNTER:
   sll $t2,$t2,1
   addi $t3,$t3,-1
   bne $t3,$0,LOOP

EXIT:
   li $v0,10
   syscall

WRONG:
   la $a0,err
   li $v0,4
   syscall

   li $v0,10
   syscall

Explanation:-
Here, I took the decimal value in the 32 bit register.Then,I first checked whether the number is more than 32767 or not.So,I can verify that the number is within 16 bit range or more.If not, then the error message will be printed.

In case , if it is within 16 bit range,then the conversion process starts:-
1)I shifted the first 16 bits in the 32 bit register,which are trailing zeros.
2)Then,now,we go on masking the register with 0x80000000 .Such that, if the MSB is 1 then we can the value as 1 or 0. Depending upon the value we print 1 or 0.
3)After,printing the MSB,now we will shift the register with 1 bit to the left so that the next bit will now be the MSB .And, the same process will be continued till all the 16 bits are completed

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