MIPS User input string number, convert to integer?
-
My assignment is prompt for a number string from the user, read the string (hint: syscall) given by the user, convert the string to an integer and store in register $s0. (do not have to worry about error checking for now). Then using syscall instruction, display the content of register $s0 to the console. This is what I have but I don't know how to convert to integer .text .globl __start __start: la $a0,str1 #Load and print string asking for string li $v0,4 syscall li $v0,8 #take in input la $a0, buffer #load byte space into address li $a1, 20 # allot the byte space for string move $t0,$a0 #save string to t0 syscall la $a0,str2 #load and print "you wrote" string li $v0,4 syscall la $a0, buffer #reload byte space to primary address move $a0,$t0 # primary address = t0 address (load pointer) li $v0,4 # print string syscall li $v0,10 #end program syscall .data buffer: .space 20 str1: .asciiz "Enter string(max 20 chars): " str2: .asciiz "You wrote:\n"
-
Answer:
You need to create a loop to inspect the characters of the string which will all be digits. You also need to know the length of the string. Map the ASCII value of each character to a binary value; e.g. 48 equals ASCII '0'. Your loop will need to increment over the length of the string, at the start of the loop you multiply the first character by 10^(n-1) and the last by 10^0. This is the same as saying that the 3 digit number 987=9*10*10 + 8*10 + 7. Each value you calculate will need to be dumped into a sum register. Note, you're going to have 2 nested loops; the first outer increments over the length of the string and finds your digit value. The second nested loop counts how many times you need to multiply your digit by 10. Since you're incrementing over the length of the string and DECREMENTING the number of times you need to multply your digit by 10, you're going to need 2 separate registers to store these index values. HINT: You can use your knowledge of ASCII for a nice shortcut. Rather than manually looking up each ASCII character value, since you know that an ASCII '0' is a decimal 48 and an ASCII '9' is a decimal 57, if you subtract 48 from the binary value of the character you'll get its numeric value. So, ASCII for '9' is 57, but minus 48==9.
Mike at Yahoo! Answers Visit the source
Other answers
You need to create a loop to inspect the characters of the string which will all be digits. You also need to know the length of the string. Map the ASCII value of each character to a binary value; e.g. 48 equals ASCII '0'. Your loop will need to increment over the length of the string, at the start of the loop you multiply the first character by 10^(n-1) and the last by 10^0. This is the same as saying that the 3 digit number 987=9*10*10 + 8*10 + 7. Each value you calculate will need to be dumped into a sum register. Note, you're going to have 2 nested loops; the first outer increments over the length of the string and finds your digit value. The second nested loop counts how many times you need to multiply your digit by 10. Since you're incrementing over the length of the string and DECREMENTING the number of times you need to multply your digit by 10, you're going to need 2 separate registers to store these index values. HINT: You can use your knowledge of ASCII for a nice shortcut. Rather than manually looking up each ASCII character value, since you know that an ASCII '0' is a decimal 48 and an ASCII '9' is a decimal 57, if you subtract 48 from the binary value of the character you'll get its numeric value. So, ASCII for '9' is 57, but minus 48==9.
godfathe...
Related Q & A:
- Is Integer Factorization Harder Than RSA Factorization?Best solution by Theoretical Computer Science
- How to Convert Json date string to more readable date format?Best solution by SharePoint
- How to count number of occurrences of pattern within a string?Best solution by Stack Overflow
- How to convert from string to char in C++?Best solution by Stack Overflow
- Python: How do you get an input from the user into a list?Best solution by stackoverflow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.