Search code examples
assemblymipsspimqtspim

Can't load a word starting at x+4 address into registry in QtSpim


I want to load a word from memory ,at adress of x +4 into a registry but the following code doesn't work. What am I doing wrong?

.data

    x:  .word   10
    y:  .word   11
    z:  .word   12

    .text
    main:
        lw  $t0, x
        lw  $t1, 4($t0)
        li  $v0, 10
        syscall

I get an Unaligned Adress in inst/data fetch error and Exception Occured


Solution

  • You're not loading the address of x properly:

    lw  $t0, x
    

    That instruction (actually, it's two instructions, because lw reg,absolute_address is a pseudo-instruction) loads the value located at x into $t0. So you get $t10 = 10, and then on the next line you try to load from address 10 + 4 == 14, which of course you can't do because the address needs to be word-aligned.

    What you should have used if you wanted to load the address of x into $t0 is:

    la  $t0, x