How to convert a string into integers (in MIPS)

In summary, the user is working on a project for their Computer Organization class and needs to read in two separate times in military format without using ":". They are able to read and save the strings, but need help manipulating them into integers. They are unsure of how to do this and would appreciate any suggestions.
  • #1
Rat_Rodz
1
0
Hey,

I'm working on a project for my Computer Organization class and I have to read in 2 separate times (in military format with no ":" ) as strings and then find and print the time difference. I am able to read in and save the strings no problem, but I don't have a clue as to how I can manipulate them into integers. any suggestions? ...I would post the code that I have already, but I'm not sure as to how to go about it.
 
Technology news on Phys.org
  • #2
Rat_Rodz said:
Hey,

I'm working on a project for my Computer Organization class and I have to read in 2 separate times (in military format with no ":" ) as strings and then find and print the time difference. I am able to read in and save the strings no problem, but I don't have a clue as to how I can manipulate them into integers. any suggestions? ...I would post the code that I have already, but I'm not sure as to how to go about it.

Hey Rat_Rodz and welcome to the forums.

The first thing to do is to make sure that the string is valid. For the moment let's assume that the string is required to be only in base 10 (ie only have characters '0' to '9').

So assuming we string has the correct format, the rest is pretty easy.

Going from end of string to start of string we start off with our temp variable to be zero.
For each character we do temp = temp + 10^(counter) * 'value of digit'.

The digit will be calculated by using the relationship 'value of digit' = 'char value' - '0'. counter will increment in a natural loop from 0 to string_len-1.
 

1. How do I convert a string into an integer in MIPS?

To convert a string into an integer in MIPS, you will need to use the li and syscall instructions. First, load the string into a register using li. Then, use the syscall instruction with the appropriate code (usually 8) to convert the string into an integer. The result will be stored in the same register.

2. What is the difference between ASCII and Unicode when converting strings to integers in MIPS?

ASCII and Unicode are two different character encoding schemes. ASCII only supports 128 characters, while Unicode supports over 100,000 characters. When converting strings to integers in MIPS, the encoding scheme must be taken into consideration to ensure accurate conversion.

3. Can I convert a string with decimal numbers to an integer in MIPS?

Yes, it is possible to convert a string with decimal numbers to an integer in MIPS. However, you will need to use a separate register to store the decimal point and perform additional calculations to accurately convert the decimal numbers into an integer.

4. What happens if the string I am trying to convert contains non-numeric characters?

If the string you are trying to convert contains non-numeric characters, the conversion will fail and the result will be unpredictable. It is important to ensure that the string only contains valid numeric characters before attempting to convert it into an integer in MIPS.

5. Are there any built-in functions or libraries in MIPS for converting strings to integers?

No, there are no built-in functions or libraries specifically for converting strings to integers in MIPS. However, there are some libraries that can be used for string manipulation, which can be helpful in converting strings to integers. These include the string.h and stdlib.h libraries.

Similar threads

  • Programming and Computer Science
2
Replies
47
Views
3K
  • Programming and Computer Science
Replies
5
Views
762
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
8
Views
883
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
553
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
5K
Back
Top