In: Computer Science
Write a MIPS program that multiples every element in an array of size 10 by 4 without using the multiply instruction.
.data
arr: .word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 # array with
10 elements
.text
.globl main
main:
la $t0, arr
# loading base address of arr to $t0
li $t1, 10
# loading array size to
$t1
li $t2, 0
# initializing loop counter to 0
multiply:
beq $t2, $t1, end
# if counter>array size, break
the loop
sll $t3, $t2, 2
# calculating offset address
add $t3, $t3, $t0
# adding offset address and base address to get effective
address
lw $t4, ($t3)
# getting value from the array
sll $t4, $t4, 2
# multiplying the value by 2, using shift instruction
sw $t4, ($t3)
# storing number back to array
addi $t2, $t2, 1
# increamenting counter
j multiply
# looping again
end:
li $v0, 10
syscall