Printing string array in MIPS - arrays

I want to print out an array in MIPS. This is what I did
.globl main
.data
hello: .asciiz "Hello, the string is:\n"
names:
.align 3
.asciiz "MIPS"
.align 3
.asciiz "IS"
.align 3
.asciiz "CRAZY"
.align 3
.text
main:
la $a0, hello
li $v0, 4
syscall
#print the first member of the names array
la $t0, names
lw $a0, 0($t0)
li $v0, 4
syscall
#exit
li $v0, 10
syscall
But when I assemble and run, MARS report there is an address out of range. I doubt that what I did wrong is taking the first element out of the array. Could someone help me explain what is wrong in my code?

You need to load the address of the String that you need to print and not the string itself
la $t0, names
lw $a0, 0($t0)
instead of this write the below
la $a0,names
This would print "MIPS IS CRAZY"

The problem is that syscall 4 requires $a0 to be the address of the string to be displayed, but you are loading garbage to $a0 (either 0 or the contents of the first characters of the string to be displayed).
Don't know why you are aligning the strings either.
I'd modify the code to look like this:
.globl main
.data
hello: .asciiz "Hello, the string is:\n"
names1:
.asciiz "MIPS"
names2:
.asciiz "IS"
names3:
.asciiz "CRAZY"
.text
main:
la $a0, hello
li $v0, 4
syscall
#print the first member of the names array
la $a0, names1
syscall
#print the second member of the names array
la $a0, names2
syscall
#exit
li $v0, 10
syscall

li $t0,0
addi $t0,$t0,16 #->porque al usar aling va de 8 en 8 (0,8,16...)
la $a0,names($t0)

Related

Print array list in MIPS

So I need help printing off an array list in MIPS. The following code is the array that's provided. How do I print that off to say "Original array is: " Here is the code that I have so far
.data
list: .word 1 2 3 4 5 6
.text
.globl main
main:
li $v0,4 # print syscall
la $a0,list
syscall
li $v0,8 # read array
la $a0,list
li $a1,1000
syscall
la $a0,list
jal list.length # get array length
move $a1,$v0 # save array length
la $a0,list
jal reverse
li $v0,4 # print array
la $a0,list
syscall
li $v0,10 # exit
syscall

Error of incorrect type in MIPS

I am getting an "operand of incorrect type" error for the following code at the line where I try to load the individual characters. I am trying to write a program that inputs two strings and compares them (i.e. prints if one is greater than or less than or equal to the other). I want to use a loop to load each letter until either a GT or LT is found, or alternatively, at the end I would know they are equal. Any help would be appreciated!
.data
prompt1: .asciiz "Enter string 1: "
prompt2: .asciiz "Enter string 2: "
gthan: .asciiz "s1 > s2\n"
lthan: .asciiz "s1 < s2\n"
equalto: .asciiz "The strings are equal\n"
s1size: .word 80
s2size: .word 80
s1: .space 20
s2: .space 20
.text
main:
# read in strings
li $v0, 4
la $a0, prompt1
syscall
li $v0, 8 #read in string
la $a0, s1
lw $a1, s1size
move $s0, $a0 #move to s0
syscall
li $v0, 4
la $a0, prompt2
syscall
li $v0, 8 #read in string
la $a0, s2
lw $a1, s2size
move $s1, $a0 #move to s1
syscall
li $a0, 0 #set sentinel
start_loop:
#load single character
la $t0, s1
lb $a1, $a0($t0)
la $t0, s2
lb $a2, $a0($t0)
#compare
bgt $a1, $a2, greaterthan
blt $a1, $a2, lessthan
beq $a1, $a2, equal
addi $a0, $a0, 4
end_loop:
greaterthan:
li $v0, 4
la $a0, gthan
syscall
lessthan:
li $v0, 4
la $a0, gthan
syscall
equal:
li $v0, 4
la $a0, equalto
syscall

MIPS: Storing integer input into memory using sw causes address out of range error

I am trying to store a dynamic array into a program using a stack, by first reading an input n and then creating a stack with [n] inputs. However, when I try to store word, it creates an error that I don't understand.
.data
ask_for_size: .asciiz "\nInsert the array size\n"
ask_for_int: .asciiz "\nInput number\n"
input: .space 16
.text
Main:
li $v0,4
la $a0,ask_for_size
syscall
li $v0,5
la $a0,input
syscall
add $t0,$v0,$zero
li $sp,0x00001000 #stack pointer
li $s0,0x00001000 #base pointer
la $a0,ask_for_int
Loop1:
li $v0,4
syscall
li $v0,5
syscall
sw $v0,0($sp)
addi $sp,$sp,-4
addi $t0,$t0,-1
bne $t0,$zero,Loop1
j Loop2
Loop2:
Shouldn't you add -4 to $sp before you sw $v0 into it?
addi $sp,$sp,-4
sw $v0,0($sp)
It seems to me that at this time you're overwriting past the end of $sp allocated for your process.
Also you load $a0 only once. Are you sure it does not get modified? Otherwise, maybe that would work better?
Loop1:
li $v0,4
la $a0,ask_for_int
syscall
...

Can't print contents of array

I am doing my homework and it says that I have to ask user for how many members they want to be present in the array. Then the user should enter the number in their array and then the program prints the array. I cannot print contents from my array in the console window.
Here is my code:
.data
max: .word -9999
array: .space 12
message1: .asciiz "Enter an integer:\n"
message2: .asciiz "Specify how many numbers should be stored in the array (atmost 8): \n"
message3: .asciiz "The array content is: \n"
message4: .asciiz "The maximum is: \n"
message5: .asciiz "They have the same maximum.\n"
message6: .asciiz "The first array has a larger maximum.\n"
message7: .asciiz "The second array has a larger maximum.\n"
.text
.globl main
main:
lw $s1, max
la $s3, array
li $s2, 3
la $a0, message2
li $v0, 4
syscall
li $v0, 5
syscall
move $t0, $v0
blt $t0, $s2, Read
Read:
la $a0, message1
li $v0, 4
syscall
li $v0, 5
syscall
move $t1, $v0
sw $t1, 0($s3)
addi $s3, $s3, 4
addi $t0, $t0, -1
bgt $t0, $zero, Read
j Print
#blt $t0, $s2, Print
Print:
la $a0, message3,
li $v0, 4
syscall
jr $ra
Thanks for the help.
When you enter Print you've got the end of the array plus 4 in $s3, so you could do something like this:
$s2 = ADDRESSOF(array)
while ($s2 != $s3) do
print_int($s2[0]) // syscall 1
print_character(' ') // syscall 11
$s2 += 4
end while
This is pseudo-code to illustrate the logic; I'll leave the actual assembly implementation to you since it's your assignment.
(By the way, this: la $a0, message3, is a typo. There shouldn't be a comma at the end of that line)

Error Accessing Array in MIPS assembly

I made this code for finding LCM of two numbers. It is the starting chunk which is having problems. I tried to find the problem but couldn't figure it out. It is giving me error of unaligned address and other exceptions when I try to Load word or Store word. Here is the code:
.data
user: .asciiz "enter first number\n"
user2: .asciiz "enter second number\n"
array1: .space 500
array2: .space 500
array3: .space 500
.text
main:
la $a0,user
li $v0,4
syscall
li $v0,5
syscall
move $s0,$v0
la $a0,user2
li $v0,4
syscall
li $v0,5
syscall
move $s1,$v0
li $t0,0
li $t1,0
li $t2,2
li $t3,3
li $t4,0
li $t5,5
li $t6,7
li $t7,0
li $t8,0
li $t9,0
li $s8,0
la $t8,array1
la $t9,array2
j Loop1
Loop1:
div $s0,$t2
mflo $s2
mfhi $s3 # remainder
beq $s2,1,Loop2
xor $s5,$s3,$0
beq $s5,1,Odd3
add $t4,$t7,$t8
sw $t2,0($t4) # error
addi $t7,$t7,4
j Loop1
Regards
If it complains about unaligned address, then you should go look why it's unaligned. Assemblers are typically smart enough to align data as appropriate, but you are using the .space directive which doesn't have any type (and hence alignment) information. By chance your strings make the arrays unaligned. You can fix this by manually adding a .align 2 directive before array1.

Resources