Question

MIPS Programming 1 In this project, you are going to write a MIPS program to read an integer number and convert it to 8- bit
Part II (50%, due date: March 1) for negative number . Write a non-leaf function to convert a negative integer number to 2s
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Required non-leaf function:

twocomplement twoComplement: # make a room in stack to store return address # save return address addi $sp,ssp,-4 # load the

exitloop2: # display the 1s complement la $a0,msg2 syscall la a0,strNum li $v0 , 4 syscall la a0,nevline syscall # add 1 to

Sample output:

Mars Messages Run IVO Enter a number between -128 and 127-9 absolute value = 00001001 1s complement = 11110110 2s complemen

Complete program:

, data s trN: .asciiz 00000000 nium: .word 0 prompt1: .asciiz Enter a number between -128 and 127 : msgo: .asciizn binar

toBinary toBinary: # load the integer # li ti ,7 loop counter loop1: index add $t2,sal,stl div $to, t0,2 # 牝2-address of char

twocomplement twoComplement: # make a room in stack to store return address # save return address addi $sp,ssp,-4 # load the

exitloop2: # display the 1s complement la $a0,msg2 syscall la a0,strNum li $v0 , 4 syscall la a0,nevline syscall # add 1 to

Sample output:

Mars Messages Run O Enter a number between-128 and 127-9 absolute value00001001 1s complement11110110 2s complement 1111011

Code to copy:

###################################twoscomp.asm##########################
.data
strNum: .asciiz "00000000"
num: .word 0
prompt1: .asciiz "Enter a number between -128 and 127 :"
msg0: .asciiz "\n binary value= "
msg1: .asciiz "\nabsolute value = "
msg2: .asciiz "\n1's complement = "
msg3: .asciiz "\n2's complement = "
newline: .asciiz "\n"
errMsg: .asciiz "Error: Out of range"
#main
.text
   # prompt for an integer
   la $a0,prompt1
   li $v0,4
   syscall
   # read an integer
   li $v0,5
   syscall
   # check whether the value is within the range or not
   blt $v0,-128,outofrange
   bgt $v0,127,outofrange
   # save the integer into num
   sw $v0,num
   # check whehter the user entered number is positive or negative
   bltz $v0,call2scomplement
   # pass address(pointer) of num and address of strNum
   # to function
   la $a0,num
   la $a1,strNum
   jal toBinary       # if user enters a +ve number, call toBinary
   la $a0,msg0
   li $v0,4
   syscall  
   la $a0,strNum
   li $v0,4
   syscall
   la $a0,newline
   li $v0,4
   syscall
   j exit
call2scomplement:
   # pass address(pointer) of num and address of strNum
   # to function
   la $a0,num
   la $a1,strNum
   jal twoComplement   # if user enters a -ve number, call twoComplement
   j exit
   # if user enters an out of range number, print error message
outofrange:          
   la $a0,errMsg
   li $v0,4
   syscall
   # exit the program
exit:
   li $v0,10
   syscall
################################# toBinary ###################################
toBinary:  
   lw $t0,0($a0)       # load the integer
   li $t1,7       # loop counter
loop1:  
   add $t2,$a1,$t1       # $t2=address of character at index $t1
   div $t0,$t0,2       # $t0=$t0/2
   mfhi $t3       # $t3=$t0%2(remainder)
   beqz $t3,store0       # if remainder is 0, store '0' into location $t2
   li $t4,'1'       # $t4='1'
   sb $t4,0($t2)       # otherwise, store '1' into location $t2
   j next           # jump to label next
store0:              
   li $t4,'0'       # $t4='0'
   sb $t4,0($t2)       # store '0' into location $t2
next:              
   beqz $t0,exitloop1   # if $t0 is zero, exit the loop
   sub $t1,$t1,1       # otherwise decrement the loop counter by 1
   j loop1           # go to loop1
exitloop1:
   jr $ra           # return
############################# twosComplement ######################################
twoComplement:
   addi $sp,$sp,-4       # make a room in stack to store return address
   sw $ra,0($sp)       # save return address
      
   lw $t0,0($a0)       # load the negative integer
   not $t0,$t0      
   add $t0,$t0,1       # $t0=absolute value of the integer
   sw $t0,0($a0)       # save back value into num
   jal toBinary       # call toBinary
   # print the returned bainry string of the absolute value
   la $a0,msg1
   li $v0,4
   syscall  
   la $a0,strNum
   li $v0,4
   syscall
   la $a0,newline
   li $v0,4
   syscall
   # do 1's complement(flip the binary bits)
   li $t1,7       # loop counter
loop2:
   add $t2,$a1,$t1       # $t2=address of the character at index $t1
   lb $t3,0($t2)       # load the binary bit(character) from location $t2
   beq $t3,'0',flipto1   # if the binary bit is 0, flip it to 1  
   li $t4,'0'
   sb $t4,0($t2)       # save 0
   j next1
flipto1:           # if the binary bit is 1, flip to 0 and store back
   li $t4,'1'      
   sb,$t4,0($t2)       # save 1
next1:
   beqz $t1,exitloop2   # if $t1 is 0, exit the loop
   sub $t1,$t1,1       # otherwise, decrement the loop counter by 1
   j loop2           # jump to loop2
exitloop2:          
   # display the 1's complement
   la $a0,msg2
   li $v0,4
   syscall  
   la $a0,strNum
   li $v0,4
   syscall
   la $a0,newline
   li $v0,4
   syscall
   # add 1 to result of 1's complement
   li $t0,7       # loop counter
   li $t7,1       # $t7=carry=1
loop3:
   add $t1,$t0,$a1       # $t1= address of character at index $t0
   lb $t2,0($t1)       # load binary bit
   sub $t2,$t2,48       # $t2=$t2-48. 48 is the ASCII for '0'
   add $t2,$t2,$t7       # $t2=$t2+carry
   and $t3,$t2,0x01   # $t3=sum & 0x01
   add $t3,$t3,'0'       # $t3=$t3+'0'
   sb $t3,0($t1)       # save back the result of the addition
   srl $t2,$t2,1       # shift right the sum($t2)
   and $t7,$t2,0x01   # carry($t7)= $t2 & 0x01
   beqz $t0,exitloop3   # if $t0 is 0, exit the loop
   sub $t0,$t0,1       # otherwise, decrement the loop counter by 1
   j loop3           # jump to loop3
exitloop3:
   # display the 2's complement
   la $a0,msg3
   li $v0,4
   syscall  
   la $a0,strNum
   li $v0,4
   syscall
   la $a0,newline
   li $v0,4
   syscall
      
   lw $ra,0($sp)       # restore the return address
   addi $sp,$sp,4       # re set the stack pointer
   jr $ra           # return
##################################################################################

Add a comment
Know the answer?
Add Answer to:
MIPS Programming 1 In this project, you are going to write a MIPS program to read...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write a C program!! Q2 and Q3 Write the following functioned int search(int a[], int n,...

    write a C program!! Q2 and Q3 Write the following functioned int search(int a[], int n, int key, int **loc); a is an array to be searched, n is the number of elements in the array, key is the search key, and loc is a pointer to the first location of the search key in array a (if found) or NULL otherwise. The function returns 1 if key is found in a, and returns 0 otherwise. Write a main() and...

  • 2. Searching a String: Write a MIPS assembly language program to do the following: Read a...

    2. Searching a String: Write a MIPS assembly language program to do the following: Read a string and store it in memory. Limit the string length to 100 characters. Then, ask the user to enter a character. Search and count the number of occurrences of the character in the string. The search is not case sensitive. Lowercase and uppercase letters should be equal. Then ask the user to enter a string of two characters. Search and count the number of...

  • MIPS programming question Problem 1: Write a program that asks the user to input a string...

    MIPS programming question Problem 1: Write a program that asks the user to input a string (or no more than 50 characters). Your program should then output the length of the string. The string length should be determined using a separate function strlen that will accept the address of the string and return its length. For the purposes of this exercise, the length of a string will be defined as the number of non-null and non-newline characters until either the...

  • Introduction: In this lab, you will write a MIPS program to read in (up to) 50...

    Introduction: In this lab, you will write a MIPS program to read in (up to) 50 integer values from the user, store them in an array, print out the amay, one number per line, reverse the elements in the array and finally print out the elements in the just-reversed) array. Feel free to do this lab and all assembly programming labs) in Windows. You must use MARS Getting started: l. In MARS, create a new assembly file with the name...

  • Create a program (java): that reads a 8-bit String input, must be read in as such....

    Create a program (java): that reads a 8-bit String input, must be read in as such. Then convert that string of 1s and 0s to decimal as if it were encoded using the following representations: Unsigned 1's Complement 2's Complement Q(4.4) (2's complement) 1 bit (sign) - 3 bits (exponent: use bias) - 4 bits (significand), implied binary point w/ 1. Also check for 0.... Do not use helper functions, must be done iterating string and modifying an int/long value....

  • Write a C program, that would take a negative number as a input from the console,...

    Write a C program, that would take a negative number as a input from the console, and convert it into 2’s complement binary representation. Recall that, there are two steps to that: ● 1’s complement of the positive bitwise representation, which is similar to bit flipping. ○ Find out the appropriate bitwise operator for that, or do it manually. ● Add 1 with the 1’s complement number. ○ Simply add 1 with the number you get in the previous step....

  • Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the...

    Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the program just the two functions for my C++ program. I wanted to post the sample code from my class but it won't allow me it's too long. This calculate shall have the following five functionalities: Provide sign extension for a binary number Provide two’s complement for a binary nunber string signed_extension(string b);              // precondition: s is a string that...

  • How do I write a C program called binary that takes a single command line argument,...

    How do I write a C program called binary that takes a single command line argument, and integer, in decimal, and prints out the binary representation of the number with 64 bits. The argument must be stored as a long. Use atol to convert the command line argument. Include a function char *binary(long n, char *b) { ... } that stores the binary representation in the string b with '0's and '1's. It is the responsibility of the calling program...

  • question 1 part 2 and 3 thank you (47) Naruto Notone C Sign In er Sign...

    question 1 part 2 and 3 thank you (47) Naruto Notone C Sign In er Sign Up | Ch ® UFC & MMA × Secure I https://piazza-resourcess3.amazonaws.com/jgopch0cb93d8/j .pdfAWSAccessKeyld-AKAILDNRL/4ALKBWOHA8lexpires-15200435/2&Signature-ol9aXG9 /UAKIHS0QUwMeyBX.. ☆ ミ quations must be properly tyne-set including superscript-s expunents, Always watch the course websile for updates on the assignments. Question 1 (4 points) Show you work I. Convert 2727 into a 32-bit two's complement binary number 2. Convert -5795 into a 16-bit two's complement binary number 3. Add the above...

  • In this part of the assignment, you will write MIPS assembly code to replace characters in...

    In this part of the assignment, you will write MIPS assembly code to replace characters in a string. This program asks the user for a string named string and two characters orig and new. It then replaces each instance of the character orig found in string with the character new. It outputs the resulting string and the number of substitutions that were made. For example, if string were "wow", orig were 'w', and new were 'b', the program would output...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT