Print array list in MIPS - arrays

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

Related

Assembly: allocate registers into array from C

I have a code in C and I need to translate it into assembly in a dinamic way.
Is there a simple way I could do that?
The C code:
int size, i;
printf("Type array size:");
scanf("%d", &size);
int vector[size];
for (i = 0; i<size; i++){
printf("type value of array[%d]: ", i);
scanf("%d", &vector[i]);
}
My Mips code:
So far, I haven't been translating it dinamically, it just prints the same prompt over and over.
I need the prompt in assembly to look like the one in C.
.data
vec: .space 0
size: .asciiz "\n\n Type array size:"
prompt: .asciiz "Type value of array:"
.text
.globl main
main:
la $a1, vec
li $v0, 4
la $a0, size
syscall
li $v0, 5
syscall
move $a2, $v0 #$a2 holds array size
j Loop
Loop:
beq $a2, $t1, END
li $v0, 4
la $a0, prompt
syscall
li $v0,5 #gets value as printf
syscall
move $s1,$v0
sw $s1, 0($a1)
la $a1, 4($a1) #moves on to next value of the array
addi $t1, $t1, 1
j Loop
END:
a counter needed to be used and its value needed to be updated inside the loop
.data
vec: .space 0
size: .asciiz "\n\n Type array size:"
prompt: .asciiz "Type value of array:["
closebrack: .asciiz "]"
.text
.globl main
main:
la $a1, vec
li $t0, 0 #counter=0
li $v0, 4
la $a0, size
syscall
li $v0, 5
syscall
move $a2, $v0 #$a2 holds array size
j Loop
Loop:
beq $a2, $t1, END
li $v0, 4
la $a0, prompt
syscall
add, $t0, 1 #counter++
move $a0, $t0
li $v0, 1 #print counter
syscall
li $v0, 4
la $a0, closebrack # print the closing bracket "]"
syscall
li $v0,5 #gets value as printf
syscall
move $s1,$v0
sw $s1, 0($a1)
la $a1, 4($a1) #moves on to next value of the array
addi $t1, $t1, 1
j Loop
END:
li, $v0,10
syscall

Recursively Reverse Array MIPS

So I am trying to recursively reverse an array but for some reason it's only printing the original array. I need it to print the reverse array with it being reversed. My code is below. Any help would be greatly appreciated.
.data
list: .word 1 2 3 4 5 6
size: .space 6
head: .asciiz "The Original Array is:\n"
back: .asciiz "The Reverse Array is:\n"
.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 arrlen
move $a1,$v0
la $a0,list
jal reverse
li $v0,4
la $a0,list
syscall
li $v0,10
syscall
reverse:
move $t0,$ra
addu $a1,$a0,$a1
addiu $a1,$a1,-1
j reverse_next
reverse_loop:
jal swap
addiu $a0,$a0,1
addiu $a1,$a1,-1
reverse_next:
ble $a0,$a1,reverse_loop
jr $t0
swap:
lb $v0,0($a0)
lb $v1,0($a1)
sb $v1,0($a0)
sb $v0,0($a1)
jr $ra
arrlen:
move $v0,$a0
arraylen_loop:
lb $t1,0($a0)
addi $a0,$a0,1
bnez $t1,arraylen_loop
subu $v0,$a0,$v0
subi $v0,$v0,1
jr $ra

read and sum array of integers in assembly

.data
prompt1: .asciiz "\n\n Enter an integer please:"
array: .space 24
linefeed: .asciiz "\n"
enterkey: .asciiz "Press any key to end program."
.text
main:
li $s0, 0
for:
bge $s0, 6, end_for
li $v0, 4 #syscall to print string
la $a0, prompt1 #address of string to print
syscall
li $v0, 5 #syscall to read an integer
syscall
move $t1,$v0
sw $t1,array($t0) #save the number to read into array
addi $t0,$t0,4
addi $s0,$s0,1
j for
end_for:
# print out a line feed
li $v0,4 # code for print_string
la $a0,linefeed # point $a0 to linefeed string
syscall # print linefeed
# wait for the enter key to be pressed to end program
li $v0,4 # code for print_string
la $a0,enterkey # point $a0 to enterkey string
syscall # print enterkey
# wait for input by getting an integer from the user (integer is ignored)
li $v0,5 # code for read_int
syscall #get int from user --> returned in $v0
# All done, thank you!
li $v0,10 # code for exit
syscall # exit program
this is my code.I am trying to store 6 integers in an array and then read again the array of integers and sum them and then printing the sum.I apologize for my bad english
It would be basically the same loop you have just written, but instead of writing the number to the array you would have to read it from the array and sum the values.
E.g.:
li $s0, 0
li $a0, 0
li $t0, 0
forsum:
bge $s0, 6, end_forsum
lw $t1,array($t0) # Load the number from array
addu $a0, $a0, $t1 # Compute the sum
addi $t0,$t0,4
addi $s0,$s0,1
j forsum
end_forsum:
li $v0,1
syscall # Print sum
You can also compute the sum while reading the values from the user input...

Printing string array in MIPS

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)

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
...

Resources