In the following code, at the instructions:
la $t0, suma
sw $t2, ($t0)
lw $a1, ($t0)
la $t1, najmanji
sh $t4, ($t1)
, the second la instruction loads addres of symbol najmanji
and it ends up being the same address as for symbol suma
, debugging in gdb I saw that after the last sh instruction suma
gets overwritten. Why does that happen? Why are the addresses of two different symbols the same? The print ends up being correct because I’m loading it into the a register sequentially, but still I am interensted in why it works like that?
.section .data
.global niz
niz: .short 12, 3, -9, 1200, -2400, 490, 800, -23, 5 , 22
.section .rodata
.global str
str: .string "Suma: %dnNajmanji: %dn"
.section .bss
.global suma
suma: .word
.global najmanji
najmanji: .short
.section .text
.global main
main:
addiu $t0, $0, 10 // size
addu $t1, $0, $0 // i = 0
addu $t2, $0, $0 // suma_ = 0
la $t3, niz // &niz
lh $t4, ($t3) // najmanji_ = niz[0]
while:
slt $t5, $t1, $t0
beq $t5, $0, iskoci
sll $t6, $t1, 1
addu $t6, $t3, $t6
lh $t6, ($t6)
addu $t2, $t2, $t6
slt $t7, $t6, $t4
beq $t7, $0, preskoci
addu $t4, $0, $t6
preskoci:
addiu $t1, $t1, 1
j while
iskoci:
la $t0, suma
sw $t2, ($t0)
lw $a1, ($t0)
la $t1, najmanji
sh $t4, ($t1)
lh $a2, ($t1)
la $a0, str
addiu $sp, $sp, -24
sw $ra, 20($sp)
jal printf
lw $ra, 20($sp)
addiu $sp, $sp, 24
addu $v0, $0, $0
jr $ra