Reputation: 35
i'm trying to write some code that take as input integers and strings, stores them into an array and then prints them in order (int-string, int-string etc.). I will eventually add some sorting code for the integer, when i get this to work. The problem is i can't get the code to work right, i can't manage to have the output come out correctly. I'm assuming the problem lies in the array, since i'm not sure how it is supposed to work (MIPS is definitely not my forte). The code is the following:
.data
array: .space 400 #array
in_name:
.asciiz "\nInsert name: "
in_date:
.asciiz "\nInsert date (mmdd): "
appt:
.asciiz "\nList: "
spaz: .asciiz " "
.text
main:
la $s0, array #load array in s0
addi $t0, $zero, 0 #t0=0 counter
addi $s1, $zero, 0 #s1=0 array size counter
j Input
Input:
li $v0, 4
la $a0, in_date
syscall #ask date
li $v0, 5
syscall #read date
add $t1, $zero, $t0 #offset in t1
add $t1, $t1, $t1 #t1*2
add $t1, $t1, $t1 #t1*4
add $s2, $t1, $s0 #array with offset in s2
sw $v0, 0($s2) #save date
addi $t0, $t0, 1 #t0++
addi $s1, $s1, 1 #array size counter +1
li $v0, 4
la $a0, in_name
syscall #ask name
li $a0, 4
li $v0, 9
syscall #space for new word (4bytes)
la $a0, array
li $a1, 4
li $v0, 8
syscall #read name
add $t1, $zero, $t0 #offset in t1
add $t1, $t1, $t1 #t1*2
add $t1, $t1, $t1 #t1*4
add $s2, $t1, $s0 #array with offset in s2
sw $v0, 0($s2) #save name
addi $s1, $s1, 1 #array size counter +1
addi $t0, $t0, 1 #t0++
beq $s1, 10, print #if array size=10 go to print
j Input #start over until s1=10
print:
la $a0, appt
li $v0, 4
syscall #print list
addi $t0, $zero, 0 #t0=0 counter
res:
add $t1, $zero, $t0 #offset in t1
add $t1, $t1, $t1 #t1*2
add $t1, $t1, $t1 #t1*4
add $s2, $t1, $s0 #array with offset in s2
lw $a0, 0($s2) #load date
li $v0, 1
syscall #print data
addi $t0, $t0, 1 #t0++
la $a0, spaz #load space
li $v0, 4
syscall #print space
add $t1, $zero, $t0 #offset in t1
add $t1, $t1, $t1 #t1*2
add $t1, $t1, $t1 #t1*4
add $s2, $t1, $s0 #array with offset in s2
lw $a0, 0($s2) #load name
li $v0, 4
syscall #print name
addi $t0, $t0, 1 #t0++
la $a0, spaz
syscall #print space
addi $t0, $t0, 1 #t0++ counter
bne $t0, $s1, res #start over until t0=s1
j end
end:
li $v0, 10
syscall #the end
Depending on which program i use to test it i get an error or the program ends correctly but with a wrong output (just some numbers). Can someone point out to me how should i correct it to make it work properly? Thanks
Upvotes: 1
Views: 1425
Reputation: 3574
I changed the program a little. I created 2 arrays for numbers and strings. As I read date and name I save them to num_array
and str_array
respectively. This makes it easier to deal with addressing, and might be easier for processing later. Comments in the code should be helpful.
.data
num_array: .space 400 #array
str_array: .space 400
in_name: .asciiz "\nInsert name: "
in_date: .asciiz "\nInsert date (mmdd): "
appt: .asciiz "\nList: "
spaz: .asciiz " "
.text
main:
la $s0, num_array #load array in s0
la $s1, str_array
addi $t0, $zero, 0 #t0=0 counter
addi $s2, $zero, 0 #s1=0 array size counter
j Input
Input:
# prompt for date
li $v0, 4
la $a0, in_date
syscall
# read date
li $v0, 5
syscall
# store date in the num_array
sw $v0, 0($s0)
# increment counter and move to next position in the array
addi $t0, $t0, 1
addi $s2, $s2, 1
addi $s0, $s0, 4
# prompt for name
li $v0, 4
la $a0, in_name
syscall #ask name
# store name (max 4 bytes) to str_array ($s1)
move $a0, $s1
li $a1, 4
li $v0, 8
syscall #read name
# move to the beginnig of next string in $s1
# increment by 4 because length of each input word is 4
addi $s1, $s1, 4
beq $s2, 10, print #if array size=10 go to print
j Input #start over until s1=10
print:
# print "List:"
la $a0, appt
li $v0, 4
syscall #print list
addi $t0, $zero, 0 #t0=0 counter
la $s0, num_array # address of num_array
la $s1, str_array # address of str_array
res:
# get number from num_array and print it
lw $a0, 0($s0)
li $v0, 1
syscall
# move to the next element, increment by 4 because numbers take 1 word = 4 bytes
addi $s0, $s0, 4
# get string from str_array and print it
la $a0, 0($s1)
li $v0, 4
syscall
# print space
la $a0, 32
li $v0, 11
syscall
# move to the next element, increment by 4 because strs have 4 bytes length (in your case)
addi $s1, $s1, 4
# increment loop counter
addi $t0, $t0, 1
blt $t0, $s2, res # $s2 is size of the array
end:
li $v0, 10
syscall #the end
Here is the output:
Insert date (mmdd): 1201
Insert name: aaa
Insert date (mmdd): 1202
Insert name: bbb
Insert date (mmdd): 1203
Insert name: ccc
Insert date (mmdd): 1204
Insert name: ddd
Insert date (mmdd): 1205
Insert name: eee
Insert date (mmdd): 1206
Insert name: fff
Insert date (mmdd): 1207
Insert name: ggg
Insert date (mmdd): 1208
Insert name: hhh
Insert date (mmdd): 1209
Insert name: iii
Insert date (mmdd): 1210
Insert name: jjj
List: 1201aaa 1202bbb 1203ccc 1204ddd 1205eee 1206fff 1207ggg 1208hhh 1209iii 1210jjj
-- program is finished running --
Upvotes: 2