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

Write the program in Notepad® and run it successfully in SPIM before uploading i

ID: 3699439 • Letter: W

Question

Write the program in Notepad® and run it successfully in SPIM before uploading it to eCampus by the due date. Ifnot running correctly, include comments on what assembly error or output you are getting. Your program is to print the binary representation of a decimal number usinga divide by 2 conversion routine 1. Use syscall 4 to prompt for a number between-128 and 127 as listed in the data declaration label prompt 2. Input a number (using svscall 5) 3. If the number is not within the limits, print message at label err: and try again. 4. Convert this decimal number to binary and output its binary representation to the console using divide by 2 method 4a. Store each remainder as its ASCII code at memory space labeled out: and output the result with svscall 4 after a header as listed in the data declaration label ans 5. The magnitude of the number will always be 8 bits with MSB- for negative numbers and 0 for positive numbers. 6. The program will continue prompting for a number as long as y is entered as listed in the data declaration label 6a. If any other character is entered, the program prints the message at label term: in the data declaration and exits the program. Hint The 8-bit binary representation of a negative number m is the same as that of positive number equal to 256m. The beginning of the program and the data declaration are given below # Program to convert a number between-128 and 127 to its binary equivalent and # outputs its binary representation to the console text main: data prompt: asciiz Tn Input a decimal number between -128 and 127: " ans: assiiz n Binary representation is err asciiz in input error-try again! term:=-aseiz n Program terminated" oewin: asciiz"Do you want to try another number? (y for yes)

Explanation / Answer

Screenshot

-------------------------------------------------------------------------------------------------

program

.data
#variable declaration
    .data
      prompt: .asciiz " input a decimal number between -128 and 127:"
      ans:    .asciiz " Binary representation is"
      err:    .asciiz " input error-try again!"
      term:   .asciiz " Program terminated"
      newin: .asciiz " Do you want to try another number?(y for yes)"
      output: .space 8
   #main program
.text
#ask user to enter value
again:la $a0,prompt
       li $v0,4
       syscall
       #read user entered value
       li $v0,5
       syscall
       #move value to a0
       move $a0,$v0
       #if it is not in range generate error
       blt $a0,-128,error
       bgt,$a0,127,error
       #answer prompt
       la $a0,ans
       li $v0,4
       syscall
       #call binary function
       jal bin
       #prompt user for continue or not
       la $a0,newin
       li $v0,4
       syscall
       #read user input
       li $v0,12
       syscall
       #user input not y then terminate the program otherwise ask user input
       beq $v0,121,again
       #terminate prompt
       la $a0,term
       li $v0,4
       syscall
       #end of the program
       li $v0,10
       syscall
       #error message
error:
       la $a0,err
       li $v0,4
       syscall
       #binary calculation loop
bin:
     #negative checking
      blt $a0,0,binNeg
      #load output address
      la $t0,output
      #if value reah 0 or less exit
      beqz $a0,loop1
      #divide by 2 get remainder
      div $a0,$a0,2
      #remainder in t1
      mfhi $t1
      #store in to output space
     sb $t1,7($t0)
     #increment memory
     addi $t0,$t0,-1
     #continue loop
     j bin
     #print the output
loop1:
#load output address and counter
     li $t1,8
     la $t0,output
loop2:
#counter 0 exit
     beqz $t1,exit
     #loop through data to print
    lb $a0,0($t0)
    li $v0,1
    syscall
   addi $t1,$t1,-1
   addi $t0,$t0,1
   j loop2
   #return to main program
exit:
   jr $ra

binNeg:
#load output address
      la $t0,output
      #if value reah 0 or less exit
      beqz $a0,loop1Neg
      #divide by 2 get remainder
      div $a0,$a0,2
      #remainder in t1
      mfhi $t1
      #store in to output space
     sb $t1,7($t0)
     #increment memory
     addi $t0,$t0,-1
     #continue loop
     j binNeg
     #print the output
loop1Neg:
#load output address and counter
     li $s0,1
     li $t1,8
     la $t0,output
     #negative magnitude
     sw $s0,0($t0)
loop2Neg:
#counter 0 exit
     beqz $t1,exitNeg
     #loop through data to print
    lb $a0,0($t0)
    li $v0,1
    syscall
   addi $t1,$t1,-1
   addi $t0,$t0,1
   j loop2Neg
   #return to main program
exitNeg:
   jr $ra
   -------------------------------------------------------------------

Note:-

I am using mars.