Wednesday 15 April 2015

Converting C to mips assembly, Unaligned address error -



Converting C to mips assembly, Unaligned address error -

i'm doing assignment now, it's done maintain getting error saying

unaligned address in inst/data fetch: 0x10010016

at line:

lw $t3,0($a1) # value of b[k] , save t3

i search online , find reply saying have utilize .align 2 prepare this, doesn't work problem.

can please give me hint on this, literally spend 6 hours on this..

thank much

here code:

# -> $a0 # b -> $a1 # n -> $a2 # j -> $a3 # k -> $s0 # -> $t0 .data .align 2 arra: .word 1,2,7,4,5 arrb: .word 3,4,7,2,9 .text la $a0, arra # have array a[] = { 1,2,7,4,5} la $a1, arrb # have array b[] = {3,4,7,2,9} addi $a2,$zero,0 # n = 0 addi $a2,$zero,3 # n = 3 addi $a3,$zero,0 # j = 0 addi $a3,$zero,3 # j = 3 addi $s0,$zero,0 # k = 0 addi $s0,$zero,2 # k = 2 g: addi $sp, $sp, -24 sw $ra, 20($sp) # save $ra on stack sw $s0, 16($sp) # save $s0 (k) on stack sw $a0, 12($sp) # save a0(a) on stack sw $a1, 8($sp) # save a1(b) on stack sw $a2, 4($sp) # save a2(n) on stack sw $a3, 0($sp) # save a3(j) on stack move $a3,$s0 # set j = k jal f # f(a,b,n,k,k) add together $a1,$a1,$s0 # homecoming address of b[k] lw $t3,0($a1) # value of b[k] , save t3 add together $v0, $t3,$zero # homecoming value lw $a3,0($sp) lw $a2,4($sp) lw $a1,8($sp) lw $a0,12($sp) lw $s0,16($sp) lw $ra,20($sp) addi $sp,$sp,24 jr $ra f: bne $a2, $zero, else # if (n != 0) go else addi $t0, $zero, 1 # set $t0 = 1 sw $t0, 0($a1) # set b[0] = 1 addi $t0, $zero, 1 # set $t0 = 1 sw $t0, 0($a1) # set b[0] = 1 addi $t0, $zero, 1 # set = 1 loop forloop: slt $t1,$s0, $t0 # if k < i, end loop, utilize $t1 store boolean value bne $t1, 1, forloopdone add together $a1, $a1, $t0 # b[i] address add together $t2,$zero,$zero sw $t2, 0($a1) # b[i] = 0 addi $t0, $t0, 1 # = + 1 j forloop else: bne $a3, $zero, updatej # test if (j == 0), if not, j = j -1 j iteratef updatej: addi $a3, $a3, -1 # j = j -1 j iteratef iteratef: addi $a2, $a2, -1 # iterate, n = n - 1 j f # f(b, a, n-1, j_update, k) bne $a3, $zero, forloop1ini # if (j != 0), go for_loop1_ini lw $a0, 0($a0) # there might sth wrong here sw $a1, 0($a1) # set b[0] = a[0] addi $a3, $a3, 1 # j++ forloop1ini: addi $t0, $a3, 0 # set = j forloop1start: slt $t1,$s0, $t0 bne $t1, 1, forloop1done add together $a0, $a0, $t0 # a[i] address lw $t1, 0($a0) # t1 = a[i] lw $t2, -4($a0) # b[i] add together $a1, $a1, $t0 # b[i] address add together $t1, $t1, $t2 # t1 = a[i-1] + a[i] sw $t1, 0($t1) # b[i] = a[i-1] + a[i] addi $t0, $t0, 1 # i++ j forloop1start forloop1done: nop forloopdone: nop jr $ra

your problem not taking business relationship size of each element of array. each element occupies 4 bytes. therefore, instead of issuing

add together $a1,$a1,$s0 # homecoming address of b[k]

you should multiply index 4 (which size of element) effective offset:

sll $s1, $s0, 2 # compute effective offset (i.e. multiply index 4) add together $a1,$a1,$s1 # homecoming address of b[k] lw $t3,0($a1) # value of b[k] , save t3

c assembly mips

No comments:

Post a Comment