Question

Write MIPS code that converts a string containing a number into an actual number. The function...

Write MIPS code that converts a string containing a number into an actual number. The function to do this conversion, atoi (Array To Integer) should have a single input (the string) and output a single number (the integer). The Java/C code is as follows:

atoi(a0: array) {

int digit, number, i = 0;

while( (array[i] >= '0') && (array[i] <= '9') )

{ digit = array[i] - '0';

number = 10 * number + digit;

i++;

}

return number;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:::

MIPS

.globl main
.data
prompt: .asciiz "Enter the number : "
prompt2: .asciiz "Converted number is : "
prompt3: .asciiz "Error.... Non numeric character found!!! "

input : .space 30
.text   
main:
li $v0,4
la $a0,prompt #it will print prompt
syscall
li $v0,8 #get string
la $a0,input
li $a1, 30
syscall

jal str2int # call the procedure str2int
move $s0,$v0 # move the return value into $s0
li $v0,4
la $a0,prompt2 #it will print prompt
syscall
move $a0,$s0
li $v0,1
syscall # print the result

li $v0, 10
syscall # Exit the program

# register $v0(return value)   

str2int:
# Check for sign
lb $t0,0($a0) # load the first byte into $t1
beq $t0,'-',negint # if sign is -,goto negint
beq $t0,'+',posint # if sign is +,goto posint
j convert
negint: li $t1,1 # set flag $t1 to 1(represents negative)
add $a0,$a0,1 # goto next ASCII character
j convert # goto convert
posint: li $t1,0 # set flag $t1 to 0(represents positive)
add $a0,$a0,1 # goto next ASCII character
convert:
li $s0,0 # sum=0
loop:
lb $t0,0($a0) # load the ASCII character into $t1
beqz $t0,exitloop # if the character is null, exit the loop
beq $t0,10,exitloop #if end of string space came
blt $t0,'0',fail # if $t1 is a non-digit character,return -1
bgt $t0,'9',fail
sub $t0,$t0,48 # convert ASCII digit to decimal digit
mul $s0,$s0,10 # multiply the previous sum with 10 and
add $s0,$s0,$t0 # the converted digit to sum
add $a0,$a0,1 # goto next ASCII character
j loop # goto loop
exitloop:
beq $t1,0,copy # if sign is +, goto copy
neg $s0,$s0 # if the sign is -, take negation of integer
copy: move $v0,$s0 # store the converted integer into $v0
j return
fail: li $v0,-1

li $v0,4
la $a0,prompt3 #it will print prompt
syscall
li $v0,10
syscall #exit

return: jr $ra # return $v0 to main

output:

Add a comment
Know the answer?
Add Answer to:
Write MIPS code that converts a string containing a number into an actual number. The function...
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
  • 1. Write a function that converts a string into an int. Assume the int is between...

    1. Write a function that converts a string into an int. Assume the int is between 10 and 99. Do not use the atoi() or the stoi() function. 2. Write a function prototype for problem 1. 3. Write a function call for the function you defined in problem 1. 4. Write a function that converts an int between 10 and 99 into a string. 5. Write a function prototype for problem 4. 6. Write a function call for function you...

  • c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input:...

    c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input: an int output: boolean function: Return true if the number is a prime number and return false otherwise */ //TO DO ************************************** /* Write a function checkPrime such that input: an array of int and size of array output: nothing function: Display the prime numbers of the array */ //TO DO ************************************** /* Write a function SumofPrimes such that input: an int output: nothing...

  • Write a C++ program that defines a function that converts a binary number string to an...

    Write a C++ program that defines a function that converts a binary number string to an integer. However, if the binary number is greater than or equal to 4,294,967,296, the function must return 0.

  • How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++...

    How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...

  • Write a function getScores(...) that is given a string, an int type array to fill and...

    Write a function getScores(...) that is given a string, an int type array to fill and the max number of elements in the array as parameters and returns an integer result. The function will find each substring of the given string separated by space characters and place it in the array. The function returns the number of integers extracted. For example: int values[3]; getScores("123 456 789", values, 3 ); would return the count of 3 and fill the array with...

  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • MIPS assembly language Covert this code to MIPS: #include <stdio.h> int function (int a) int main)i...

    MIPS assembly language Covert this code to MIPS: #include <stdio.h> int function (int a) int main)i int x=5 ; int y: y function(x); printf "yd",y); return 0; int function (int a) return 3*a+5; Assumptions: . Place arguments in $a0-$a3 . Place return values in $vO-$v1 Return address saved automatically in $ra . lgnore the stack for this example. (Thus, the function will destroy registers used by calling function

  • MIPS assembly language Implement the following code in MIPS int array [ ] {2, 3, 4,...

    MIPS assembly language Implement the following code in MIPS int array [ ] {2, 3, 4, 5, 6); int main) int num, position; scanf("%d",&num) ; position search(array, printf("The position is: num, 5); %d\n",positio int search(int array, int num, int size int position =-1; for(int i-0;i<size; i++) if(array [i]=num) { position-i; break; return position; Register map $s1: position $a0: array address $a1: num . $a2: size . $VO: return value

  • Implement the function hasDuplicates. Input will be given as a line containing a positive integer n,...

    Implement the function hasDuplicates. Input will be given as a line containing a positive integer n, followed by n rows, each containing a string. The output should be either the word true if there are any duplicates among these strings or false if there are not. Your code should work well on long lists of strings. Use the following set-up to write the code in JAVA import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) {...

  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

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
Active Questions
ADVERTISEMENT