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

Exam 3 bonus Programming Problem Write the program in Notepad® and run it succes

ID: 3712748 • Letter: E

Question

Exam 3 bonus Programming Problem

Write the program in Notepad® and run it successfully in SPIM before uploading it to eCampus as a text file with .s extension. If not 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 using a 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 syscall 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. Push each remainder on the stack then pop them and print them using syscall 1. A header as listed in the data declaration label ans: should precede that.

4b. The magnitude of the number will always be 8 bits with MSB =1 for negative numbers and 0 for positive numbers.

5. The program will continue prompting for a number as long as y is entered when prompted as listed in the data declaration label newin:

5a. 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 256 + m.

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 " 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) "

Please write it the code clean and clear with explanations thanks you, give me some screenshots of output too!

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)"
    
#main program
.text
.globl main
main:
#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
       #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
       #binary calculation loop
bin:
     #negative checking
      blt $a0,0,binNeg
      #stack to store remainder
      addi $sp,$sp,32
      #divide by 2 get remainder
      #1 time
      div $a0,$a0,2
      #remainder in t1
      mfhi $t1
      #store value in stack
      sw $t1,0($sp)
      #2 time
      div $a0,$a0,2
      #remainder in t1
      mfhi $t1
      #store value in stack
      sw $t1,4($sp)
      #3 time
      div $a0,$a0,2
      #remainder in t1
      mfhi $t1
      #store value in stack
      sw $t1,8($sp)
      #4 time
      div $a0,$a0,2
      #remainder in t1
      mfhi $t1
      #store value in stack
      sw $t1,12($sp)
      #5 time
      div $a0,$a0,2
      #remainder in t1
      mfhi $t1
      #store value in stack
      sw $t1,16($sp)
      #6 time
      div $a0,$a0,2
      #remainder in t1
      mfhi $t1
      #store value in stack
      sw $t1,20($sp)
      #7 time
      div $a0,$a0,2
      #remainder in t1
      mfhi $t1
      #store value in stack
      sw $t1,24($sp)
      #8 time
      div $a0,$a0,2
      #remainder in t1
      mfhi $t1
      #store value in stack
      sw $t1,28($sp)
   
     #print the output
    lw $a0,28($sp)
    li $v0,1
    syscall
     lw $a0,24($sp)
    li $v0,1
    syscall
     lw $a0,20($sp)
    li $v0,1
    syscall
     lw $a0,16($sp)
    li $v0,1
    syscall
    lw $a0,12($sp)
    li $v0,1
    syscall
   lw $a0,8($sp)
    li $v0,1
    syscall
     lw $a0,4($sp)
    li $v0,1
    syscall
     lw $a0,0($sp)
    li $v0,1
    syscall
   #return to main program
exit:
   addi $sp,$sp,-32
   jr $ra

binNeg:
       #stack for remainder
       addi $sp,$sp,32
    
      #divide by 2 get remainder
      #1 time
      div $a0,$a0,2
      #remainder in t1
      mfhi $t1
      #store value in stack
      sw $t1,0($sp)
      #2 time
      div $a0,$a0,2
      #remainder in t1
      mfhi $t1
      #store value in stack
      sw $t1,4($sp)
      #3 time
      div $a0,$a0,2
      #remainder in t1
      mfhi $t1
      #store value in stack
      sw $t1,8($sp)
      #4 time
      div $a0,$a0,2
      #remainder in t1
      mfhi $t1
      #store value in stack
      sw $t1,12($sp)
      #5 time
      div $a0,$a0,2
      #remainder in t1
      mfhi $t1
      #store value in stack
      sw $t1,16($sp)
      #6 time
      div $a0,$a0,2
      #remainder in t1
      mfhi $t1
      #store value in stack
      sw $t1,20($sp)
      #7 time
      div $a0,$a0,2
      #remainder in t1
      mfhi $t1
      #store value in stack
      sw $t1,24($sp)
      #8 time
      li $t1,1
      #store value in stack
      sw $t1,28($sp)
   
     #print the output
    lw $a0,28($sp)
    li $v0,1
    syscall
     lw $a0,24($sp)
    li $v0,1
    syscall
     lw $a0,20($sp)
    li $v0,1
    syscall
     lw $a0,16($sp)
    li $v0,1
    syscall
    lw $a0,12($sp)
    li $v0,1
    syscall
   lw $a0,8($sp)
    li $v0,1
    syscall
     lw $a0,4($sp)
    li $v0,1
    syscall
     lw $a0,0($sp)
    li $v0,1
    syscall
   #return to main program
exitNeg:
   addi $sp,$sp,-32
   jr $ra
   .end

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