any ideas? Why I am getting: Runtime exception at 0x00400020: fetch address not aligned on word boundary 0x00000007 Problem line is: lw $s1,0($a1) #copy arg2 = size of array
.data
.align 4 #added this, didnt work
size: .word 7
.align 4 #added this, didnt work
search: .word 30
.align 4 #added this,didnt work
array: .word 10,20,30,40,50,60,70
.align 4
.text
main:
la $a0,array #$a0 = address of array
lw $a1,size #a1 = size of array
lw $a2,search #$a2 = search key
COUNT:
lw $s0,0($a0) #copy arg1 = address array
addi $s1,$zero,7
lw $s1,0($a1) #copy arg2 = size of array
lw $s2,0($a2) #copy arg3 = search key (n)
addi $s2,$zero,30
COUNTLOOP:
add $v0,$zero,$zero #v0 = res
add $t0,$zero,$zero #$t0 = init i to 0
slt $t1,$t0,$s1 #check if i > size of array
beq $t1,$zero,DONECOUNT #i is n so end
sll $t2,$s0,2 #$t2 = get off set for a[i]
lw $t3,0($t2) #$t3 = get value of a[i]
bne $t3,$s2,CLOOPBTM #check if a[i] == seach key
addi $v0,$v0,1 #if above then increment res
CLOOPBTM:
addi $t0,$t0,1
j COUNTLOOP
DONECOUNT:
The problem with the code is, that you're not using the address where the size is stored but the size itself:
Here you load the address into A0 and the size (7) into A1:
Here you load the first word stored at your array (that will load a 10). This is not what you've intended.
Here you load the first word stored at location 0x000007. (your size). This is probably also not intended and will cause an exception because the address is not aligned:
and so on.
It seems to me, that you have a misunderstanding what the LW instruction does. It reads a memory location into a register. What you want in the prolog of your loop is to make copies of a register.
To do so you can use the move pseudo instruction if your assembler supports it. Otherwise use the OR instruction to copy registers like this:
for a complete example of a linear search loop try this (untested and expects that the assembler cares about the delay slots)