Question

In: Computer Science

Write a program in MIPS assembly language to convert an ASCII number string containing positive and...

Write a program in MIPS assembly language to convert an ASCII number string containing positive and negative integer decimal strings, to an integer. Your program should expect register $a0 to hold the address of a nullterminated string containing some combination of the digits 0 through 9. Your program should compute the integer value equivalent to this string of digits, then place the number in register $v0. If a non-digit character appears anywhere in the string, your program should stop with the value −1 in register $v0. For example, if register $a0 points to a sequence of three bytes 50ten, 52ten, 0ten (the nullterminated string “24”), then when the program stops, register $v0 should contain the value 24ten. Your program should accept input from the keyboard (use the MIPS syscall functionality for I/O). READ THE INSTRUCTIONS IN THE BOOK CAREFULLY. The program is to accept text input from a keyboard (i.e. a string), and translate it to its numeric interpretation. E.g., the string "24" (which has a 2 byte ASCII character representation) should be translated to its decimal value equivalent 24. NO POPUP WINDOWS FOR INPUT - use service code 8 and NOT 54!

Solutions

Expert Solution

Copyable code:

.data

string_ascii: .asciiz   ""

prompt1: .asciiz "Enter an ASCII number string: "

.text

main:

     #prompt the user

     li $v0, 4  

    la $a0, prompt1        

     syscall

     #read the ASCII digit string from the user

     li $v0,8 #take in input

     #$a0 holds the address of a nullterminated string

     la $a0, string_ascii #load the byte space into address

     li $a1, 20 #laod the byte space for string

     syscall

              

     #jump and link to the label convertStringToInt

     jal convertStringToInt

     #store the return value $vo into $a0

     move $a1, $v0

     #print $v0 result

     li   $v0,4

     syscall

              

     #stop program

     li $v0,10

     syscall

#Converts the ASCII decimal string into integer

convertStringToInt:

     # initialize $v0 = 0

     li $v0, 0

     # assign the value '0' to $t6

     li $t6, 0x30

     # assign the value '9' to $t7

     li $t7, 0x39

     #move the the address of a nullterminated string into $t0

     move $t0, $a0

     #load $t1 with the character of the string

     lb $t1, ($t0)

L1:

     #check the character is digit or not

     #if the charcater is less than 0

     #or greater than 9, then goto the label notDigit

     blt $t1, $t6, notDigit

     bgt $t1, $t7, notDigit

     #convert char to integer by substracting $t1 and $t6

     subu $t1, $t1, $t6

     #multiple the value of v0 by 10

     mul $v0, $v0, 10

     # and $v0 and $t1

     add $v0, $v0, $t1

     #increment the $t0 by 1 to get the next character

     addiu $t0, $t0, 1

     #load $t1 with the character of the string

     lb $t1, ($t0)

     #branch to the until end of the string

     bne $t1, $0, L1

     # return integer value

     jr $ra

notDigit:

     #make the value of $v0 as -1

     #if the input string has non-digit character

     li $v0, -1

     # return -1 in $v0

     jr $ra

Sample output 1:

Sample output 2:

Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

Related Solutions

Write a program in "RISC-V" assembly to convert an ASCII string containing a positive or negative...
Write a program in "RISC-V" assembly to convert an ASCII string containing a positive or negative integer decimal string to an integer. ‘+’ and ‘-’ will appear optionally. And once they appear, they will only appear once in the first byte. If a non-digit character appears in the string, your program should stop and return -1.
Write a mips assembly language program that asks the user to enter an unsigned number and...
Write a mips assembly language program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6.
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal....
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal. 1. Request input data type. 2. Request input data. 3. Request output data type. 4. Output the data. Use any algorithm
Write a MIPS assembly program that prompts the user first for a string, then for a...
Write a MIPS assembly program that prompts the user first for a string, then for a character. The program should then search the string for the character and print out the number of times the character appears in the string. You can use as many restrictions as you want, just be sure to list them. You must allocate space in the .data segment of your program for the user string.
MIPS Assembly LanguageWrite a MIPS assembly language program that asks the user toinput an...
MIPS Assembly LanguageWrite 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...
Write a program in MIPS assembly language to perform the calculation of the following equation and...
Write a program in MIPS assembly language to perform the calculation of the following equation and save the result accordingly:    f = 5x + 3y + z Assumptions: - Registers can be used to represent variables x, y, z, and f - Initialize x, y, and z to values of your choice. f can be initialized to zero. - Use comments to specify your register usage and explain your logic
Write a mips assembly language program to ask the user to enter two integers A and...
Write a mips assembly language program to ask the user to enter two integers A and B and then display the result of computing the expression: A + 2B - 5.
write a assembly language program to convert GRAY to BCD code in 8051
write a assembly language program to convert GRAY to BCD code in 8051
Write a mips assembly language program. Ask the user whether he wants to repeat the program:...
Write a mips assembly language program. Ask the user whether he wants to repeat the program: "\nRepeat [y/n]? ". Use service code 12 to read a character and the branch instruction to repeat the main function if the user input is character 'y'.
This is to be done with MIPS assembly language. Write MIPS code which is equivalent to...
This is to be done with MIPS assembly language. Write MIPS code which is equivalent to the follow java program: int day = (int)(Math.random() * 7); switch (day) { case 1: System.out.println(“Monday”); break case 2: System.out.println(“Tuesday”); break case 3: System.out.println(“Wednesday”); break case 4: System.out.println(“Thursday”); break case 5: System.out.println(“Friday”); break case 6: System.out.println(“Saturday”); break case 0: System.out.println(“Sunday”); break }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT