PDA

View Full Version : mips help!




notwist
Jan 27, 2007, 04:26 PM
I'm trying to get a user to enter in a string of characters and then parse each character and save them into each of their own registers. So far i have the user entering the string, but i don't know where the string is stored.

li $v0, 8
la $a0, expr
li $a1, 20
syscall

For example if someone enters in the string '56+'. In ASCII, the string is represented by 3 sets of 2 numbers. i want to store the 56 and + in their own registers. How can i do this? I know there's a sb and lb instruction, but I'm not sure how to implement since I don't even know how to load the input string! :confused: :mad:



gnasher729
Jan 28, 2007, 05:09 AM
I'm trying to get a user to enter in a string of characters and then parse each character and save them into each of their own registers. So far i have the user entering the string, but i don't know where the string is stored.

li $v0, 8
la $a0, expr
li $a1, 20
syscall

For example if someone enters in the string '56+'. In ASCII, the string is represented by 3 sets of 2 numbers. i want to store the 56 and + in their own registers. How can i do this? I know there's a sb and lb instruction, but I'm not sure how to implement since I don't even know how to load the input string! :confused: :mad:


You are the optimist! I'll post a proper reply as soon as Apple releases a Macintosh that uses a MIPS processor.

mufflon
Jan 28, 2007, 06:40 AM
I believe there are some better commands for entering / managing text in the consiole (which I think it is), however they are somewhere else (@uni) and I would really like to know what this is for - learning assembly is kinda uni-related as well and if this is a home assignment you should better tell us as much...


.. I would propose you use som basic c-derivate which is available for mips - it'll make this easier.

LtRammstein
Jan 31, 2007, 10:23 PM
To begin, it's funny that mentioned that you won't touch MIPS until they make a Mac compiler. FYI, all compilers use assembly code, it's just MIPS was the first, then came the others from Intel and IBM. Visual Studios.NET 2005 uses the x86 Assembler.

Now the question at hand. Parsing it in C would give you the best idea on what to write, but I think you would need to do it differently.

li $v0, 8
la $a0, expr
li $a1, 20
syscall

You would need to define variables in the .data function to use. Second, you would have to use a specific command, in this case, you can use a variable named string1 and have it be a .ascii variable type. The .ascii means that what is inputted to it is a basic ASCII character. Two, you'd need to define an array. For this you would have to use array: .space 40. Remember MIPS does everything by a word (4 bits) so if you want an array of 10 items you need to multiply it by 4 to get the right space.

From here you can then put each individual character into a register, probably a $t0-9 or $s0-9 register.

Hopefully you understand that you will need at least 2 loops to do this.

By the way, I am in a MIPS programming class at my uni, and right now I have a homework due Wednesday on recursive programming in MIPS (which is a total pain in the ass).

Hopefully this explanation helps!

Steve

notwist
Feb 6, 2007, 01:52 PM
To begin, it's funny that mentioned that you won't touch MIPS until they make a Mac compiler. FYI, all compilers use assembly code, it's just MIPS was the first, then came the others from Intel and IBM. Visual Studios.NET 2005 uses the x86 Assembler.

Now the question at hand. Parsing it in C would give you the best idea on what to write, but I think you would need to do it differently.

li $v0, 8
la $a0, expr
li $a1, 20
syscall

You would need to define variables in the .data function to use. Second, you would have to use a specific command, in this case, you can use a variable named string1 and have it be a .ascii variable type. The .ascii means that what is inputted to it is a basic ASCII character. Two, you'd need to define an array. For this you would have to use array: .space 40. Remember MIPS does everything by a word (4 bits) so if you want an array of 10 items you need to multiply it by 4 to get the right space.

From here you can then put each individual character into a register, probably a $t0-9 or $s0-9 register.

Hopefully you understand that you will need at least 2 loops to do this.

By the way, I am in a MIPS programming class at my uni, and right now I have a homework due Wednesday on recursive programming in MIPS (which is a total pain in the ass).

Hopefully this explanation helps!

Steve

Thanks. If I am just reading each seperate char and putting into a register, what is the array for? Also, How do I distinguish between when a person enters a 1 digit or 2 digit number? Argh I hate this class!

LtRammstein
Feb 7, 2007, 02:50 PM
This is the great part about programming!

Look at this code and see if it helps you understand.


move $t2 $a0 # $a0 is the array
li $t0, 0 # Index variable
move $t1 $v0 # The size of the array to use

insertLoop:
bge $t0 $t1 loopDone

li $v0 5
syscall
sw $v0 ($t2)

addi $t0 $t0 1
addi $t2 $t2 4

j insertLoop

loopDone:
jr $ra


That is an insertion loop to the array. After the user inputs a character or number they MUST HIT ENTER for it to be stored in the array. So they can have them be double digit number.

To be safe, I recommend allocating space on the stack.

Lets say they put 5 items in the array. Make sure you have the array size in a $s0 register. From there you can use the rest of the $s0 and $t0 registers to put those items in the array, that of course is another loop.

Hopefully this makes sense.

Steve