mov eax,[b] add eax,[c] move [a], eax
lw $t1, b lw $t2, c add $t2, $t2, $t1 sw $t22, a
const int SIZE{some number}
int a[SIZE],b[SIZE],c[SIZE];
for(i = 0; i < SIZE; ++i) {
a[i] = b[i] + c[i]
}
mov ecx, 1 ; i = 1
mov ebx, SIZE ; loop bound
loop:
cmp ecx, ebx
jge done ; if i >= SIZE, exit loop
mov eax, [b + ecx*4] ; eax = b[i]
add eax, [c + ecx*4] ; eax = b[i] + c[i]
mov [a + ecx*4], eax ; a[i] = eax
inc ecx ; i++
jmp loop (Courtesy of chatGPT)
li $t0, 1 # i = 1
li $t1, SIZE # loop bound
loop:
bge $t0, $t1, done # if i >= SIZE, exit loop
sll $t2, $t0, 2 # offset = i * 4 (since word = 4 bytes)
lw $t3, b($t2) # load b[i]
lw $t4, c($t2) # load c[i]
add $t5, $t3, $t4 # t5 = b[i] + c[i]
sw $t5, a($t2) # store into a[i]
addi $t0, $t0, 1 # i++
j loop (Again, thanks to chatGPT)
add mem, mem, mem.
li $t0, 1 # i = 1
li $t1, SIZE # loop bound
loop:
bge $t0, $t1, done # if i >= SIZE, exit loop
sll $t2, $t0, 2 # offset = i * 4 (since word = 4 bytes)
add a($t2), b($t2), c($t2)
addi $t0, $t0, 1 # i++
j loop (My own brain!)
SELECTION-SORT(A) Input: A an array of items with the comparison operator Output: the array A ordered by the comparison operator
- current ← 0
- while current < A.size
- small ← current
- for next ← current +1 to A.size
- if A[next] < A[small]
- small ← next
- if small ≠ current
- swap(A[small], A[current])
- current ← current + 1
perf to determine the number of instructions executed.