Reputation: 181
I am trying to create a string in MIPs, then change each character from the string into an integer. Currently I am having trouble creating the new string and I can't figure out what I am doing wrong. The block of code I am inserting should be creating a memory allocation for 4 characters, then assigning a number to each location and saving the string into the memory position. It doesn't seem to be saving the string however, as when I print it later it is always blank. I am only including the block of code that doesn't seem to be working.
Any tips would be greatly appreciated!
.data
string: .space 16 #declare storage for string 4 char
string2: .asciiz "Success!"
string3: .asciiz "Failure!"
.text
main: # convert string to integer
la $t0, string # load base address of string to reg $t0
li $t1, 1 # $t1 = 1
sw $t1, ($t0) # first array element set to 1
li $t1, 2 # $t1 = 2
sw $t1, 4($t0) # second array element set to 2
li $t1, 3 # $t1 = 3
sw $t1, 8($t0) # third array element set to 3
li $t1, 0 # $t1 = 0
sw $t1, 12($t0) # third array element set to 0
# array stored at #t0
sw $t0, string
li $v0, 4 # syscall for print string
la $a0, string # load string to be printed
syscall # print digit string
This all compiles with no issues.
Upvotes: 0
Views: 246
Reputation: 22585
In MIPS every character takes 1 byte (not 4), at least when using syscall
4 to print the string.
Also note that you are trying to print unprintable characters (ASCII codes 1, 2, and 3), instead of ASCII codes for '1', '2' and '3'.
And I think sw $t0, string
instruction shouldn't be there.
Finally, note that syscall
4 assumes the string is NULL terminated, therefore if you declare storage for 4 chars, and want to use that syscall you really have 3 characters available.
Here's a snippet of the code you posted fixed to print string 123
:
.data
string: .space 4 #declare storage for string 4 char
.text
main: # convert string to integer
la $t0, string # load base address of string to reg $t0
li $t1, '1' # $t1 = 1
sb $t1, ($t0) # first array element set to 1
li $t1, '2' # $t1 = 2
sb $t1, 1($t0) # second array element set to 2
li $t1, '3' # $t1 = 3
sb $t1, 2($t0) # third array element set to 3
li $t1, 0 # $t1 = 0
sb $t1, 3($t0) # third array element set to 0
# array stored at #t0
li $v0, 4 # syscall for print string
la $a0, string # load string to be printed
syscall # print digit string
Upvotes: 1