Any help to find the answer would be greatly appreciated.
.data
array: .word 1,2,3,4,5,6,7,8,9,10
.text
.globl main
main:
la $t0, array
li $t1, 4
mult $t1, $t1
mflo $t1
add $t0, $t0, $t1
sw $t1, 20($t0)
What are the values of the array after executing the instructions? Im confused on this part. Do I just add 20 to each value in the array?
Some hints
.data
array: .word 1,2,3,4,5,6,7,8,9,10 # 10 32 bits values #array
.text
.globl main
main:
la $t0, array # Load address of array into $t0
li $t1, 4 # Load constant 4 into $t1
mult $t1, $t1 # Multiply $t1 x $t1
mflo $t1 # Put result (lower word) into $t1 ( 16 )
add $t0, $t0, $t1 # $t0 = $t0 + $t1 ( array + 16 )
sw $t1, 20($t0) # Store $t1 at address ( $t0 + 20 )
Provided that a word is 4 bytes, i.e. storing at (array + 4) overwrites the 2 in array (2nd value), you should be able to figure out the answer to your question.
Related
Write a MIPS program that defines a one-dimensional array of 10 integers in the static area of the data segment, asks the user to input all 10 array elements, computes, and displays their sum.
.data
arr: .word 1,2,3,4,5,6,7,8,9,10
msg: .asciiz "Result: "
.text
.globl main
main:
addi $t0, $zero, 0 # clear i
addi $t1, $zero, 0 # clear sum
ori $t2, $zero, 10 # Initializing t2 to its constant value 10
la $t3, arr # load address of array into t4
loop:
slt $t4, $t0, $t2 # compare, $t4 = i < sum ? 1 : 0
beq $t4, $zero, end # if i is not < 10, exit the loop
lw $t4, 0($t3) # load current array element into t4
add $t1, $t1, $t4 # add it to sum
add $t0, $t0, 1 # increment i
add $t3, $t3, 4 # increment current array element pointer
j loop
end:
addi $v0, $zero, 4 # Now we print out result: string
la $a0, msg
syscall
addi $v0, $zero, 1 # followed by the actual sum (which is in t1)
add $a0, $t1, $zero
syscall
jr $ra
.end main
I am trying to initialize memory array to values 1, 2, 3.. 10. Though I am having a bit of trouble. Here is my work so far:
.data
myarray: .space 10
.text
la $t3, myarray # Load the address of myarray
addi $t2, $zero, 1 # Initialize the first data value in register.
addi $t0, $zero, 10 # Initialize loop counter $t0 to the value 10
top:
sw $t2, 0($t3) # Copy data from register $t2 to address [ 0 +
# contents of register $t3]
addi $t0, $t0, -1 # Decrement the loop counter
bne $t0, $zero, top
Any help would be much appreciated.
There are several problems with your code.
If you use sw (store word), you assume a "word" array. Its size should be 4*10. If you wand a byte array, use sb .
You do not increment the array pointer in $t3
Same problem for the array values in $t2
.data
myarray: .space 10
.text
la $t3, myarray # Load the address of myarray
addi $t2, $zero, 1 # Initialize the first data value in register.
addi $t0, $zero, 10 # Initialize loop counter $t0 to the value 10
top:
sb $t2, 0($t3) # Copy data from register $t2 to address [ 0 +
# contents of register $t3]
addi $t0, $t0,-1 # Decrement the loop counter
addi $t3, $t3, 1 # next array element
addi $t2, $t2, 1 # value of next array element
bne $t0, $zero, top
As suggested by #PeterCordes, this can be optimized by merging the loop counter and the array values register to suppressed one instruction in the loop. The corresponding loop in C will be
for(i=1, ptr=array; i!=11; ptr++,i++) *ptr=i;
And the corresponding code
.data
myarray: .space 10
.text
la $t3, myarray # Load the address of myarray
addi $t2, $zero, 1 # Initialize the first data value in register.
addi $t0, $zero, 11 # Break the loop when array value reaches 11
top:
sb $t2, 0($t3) # Copy data from register $t2 to address [ 0 +
# contents of register $t3]
addi $t2, $t2, 1 # Increment array value/loop counter
addi $t3, $t3, 1 # next array element
bne $t0, $t2, top
Here is my MIPS code. It is supposed to input 10 integers in array and then output them. I can input integers but after entering 10 integers it doesn't output any array element.
.data
arr: .space 40
.text
.globl main
main:
la $t0, arr #copying array address into $t0
iterateForInput:
blt $t4, 9, forLoopInput
sub $t0, $t0, 40
iterateForOutput:
blt $t4, 9, forLoopOutput
li $v0, 10
syscall
forLoopInput:
li $v0, 5 #input integer
syscall
move $t1, $v0 #moving integer into $t1
sw $t1, ($t0) #storing integer at array address
add $t0, $t0, 4
add $t4, $t4, 1
b iterateForInput
forLoopOutput:
lw $t6, ($t0)
li $v0, 1
move $a0, $t6
syscall
add $t0, $t0, 4
add $t4, $t4, 1
b iterateForOutput
I know how to iterate through an array in mips. However I can't seem to figure out how to compare two elements in the same array? Can anybody point me in the right direction here?
if(array[i] > array[i - 3])
I believe it would be a branch if greater than comparing two registers but how would I get the data into the two registers? Thanks!
You cant just iterate an array... You have to add a multiple of 4 (=> index * 4) to the memory address of the array. Then you can load the value at this modified memory addres and compair it to a diffrent moddified addres...
.data
array: .word 1,2,3
#Index's you want to compair
i1: .word 0
i2: .word 1
.text
MAIN:
lw $a0 i1
lw $a1 i2
li $t5 4 #load multiplier
mul $a0 $a0 $t5
mul $a1 $a1 $t5
la $t0 array
la $t1 array
add $t0 $t0 $a0
add $t1 $t1 $a1
lw $t0 ($t0)
lw $t1 ($t1)
beq $t0 $t1 TRUE
Trying to convert this c code into MIPS and run it in SPIM.
int A[100], B[100];
for(i=1; i<100; 1++){
A[i] = A[i-1] + B[i];
}
So far this is what I have.
# comments are delimted by has marks
.data
A: .word 0:100 # array of 12 integers
B: .word 0:100 # array of 12 integers
.text
main:
li $v0, 1 # load the value "1" into register $v0
li $t0, 1 # load the value "1" into register $t0
li $t1, 100 # load the value "100" into register $t1
blt $t0, $t1, loop # branches to Loop if $t0 < 100
la $t9, B
la $t8, A
loop:
sll $t0, $t0, 2
add $t2, $t9, $t0
lw $s4, 0($t9)
add $t3, $t0, -1
add $t4, $t8, $t3
lw $s5, 0($t4)
add $t5, $t2, $s5
add $t6, $s0, $t0
sw $t7, 0($t5)
addi $t0, $t0, 1
li $v0, 1 # system call for print_int
move $a0, $t0 # the sum to print
syscall # print the sum
When running in SPIM I get the following errors:
Exception occurred at PC=0x00400040
Bad address in data/stack read: 0x00000004
Exception occurred at PC=0x0040004c
Unaligned address in inst/data fetch: 0x00000003
Exception occurred at PC=0x00400058
Bad address in data/stack read: 0x00000000
Attempt to execute non-instruction at 0x0040006c
Some direction would be nice. Thanks
You are branching to loop (blt $t0, $t1, loop) before you initialize the pointers to A and B. You need to move the blt $t0, $t1, loop to the end of your code, not have it at the beginning.
I hate to do this, but there are too many things wrong to list them all. Try this:
.data
A: .word 0:100 # array of 100 integers
B: .word 0:100 # array of 100 integers
.text
main:
li $t0, 4 # load the value "1" into register $t0
li $t1, 400 # load the value "100" into register $t1
la $t9, B
la $t8, A
loop:
add $t2, $t9, $t0 # $t2 = B + i
lw $s4, 0($t9) # $s4 = B[i]
add $t3, $t0, -4 # $t3 = i - 1
add $t4, $t8, $t3 # $t4 = A + i - 1
lw $s5, 0($t4) # $s5 = A[i - 1]
add $t5, $t8, $t0 # $t5 = A + i
add $t6, $s4, $s5 # $t6 = B[i] + A[i - 1]
sw $t6, 0($t5) # A[i] = $t6
addi $t0, $t0, 4 # i++
li $v0, 1 # system call for print_int
move $a0, $t6 # the sum to print
syscall # print the sum
blt $t0, $t1, loop # branches to Loop if $t0 < 100
Right off the bat, s1 and s2 should be initialized to the stack-based (I assume) address of your arrays.