-1

I have a university assignment and I have been given this data:

.data 
a: .word 10
e: .word 3, 2, 1, 0
c: .word -1
d: .byte -1,0,0,0

The question is: What's the value $4 is holding if we implement the code ... and why:

la $10,d
addi $10,$10,-12
lw $4,0($10)

I wanted to check the answer before answering theoretically and when I ran the code:

.data
a: .word 10
e: .word 3, 2, 1, 0
c: .word -1
d: .byte -1, 0, 0, 0
.text
.globl main

main:
la $10,d
addi $10,$10,-12
lw $4,0($10)

li $v0,1
move $a0,$4
syscall

li $v0,10
syscall

QtSpim says the output is 1 but I thought the output should be -1. I proceeded to check with chatgpt first and it confirmed my answer:

Do any of you have any idea whether me/chatgpt are wrong or there is something going wrong with QtSpim?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
stratakos
  • 25
  • 7
  • The word you loaded is from 12 bytes before `d:`, so the 4 bytes of your `.byte` are irrelevant. You're effectively doing `lw $4, d-12`, so it's loading whatever happens to be in memory 12 bytes before `d`. Nothing in the program's source code tells you what value it will load; if it doesn't crash on an out-of-bounds load, it's just an implementation detail of QtSPIM. – Peter Cordes Apr 28 '23 at 15:50
  • `move $a0,$4` is a no-op because `$a0` and `$4` are the same register. (https://en.wikibooks.org/wiki/MIPS_Assembly/Register_File). This is why it's a bad idea to mix ABI names with register numbers; you might accidentally use the same register for two different things without noticing, in a way that causes a problem. (`$10` is `$t2`, so that's fine. `$v0` is `$2`) – Peter Cordes Apr 28 '23 at 15:50
  • 1
    As usual, ChatGPT has no idea what it's talking about. It incorrectly claims that `$a2` is `$10` on MIPS, but actually `$a2` is `$6`, and `$t2` is `$10`. Also, subtracting 12 from the address of `d` doesn't point to the first byte of the array, it points to before the start. – Peter Cordes Apr 28 '23 at 15:55
  • Even if you were loading from `d`, like `lw $a0, d` or equivalent involving `la`, you'd be loading `0x000000FF` because you're doing a 4-byte word load. (Or if you ran QtSPIM on a big-endian host machine, you'd load `0xFF000000`, since it emulates a MIPS with the host's endianness. Unlike MARS which is written in Java and emulates little-endian MIPS everywhere. But pretty much everyone's desktops these days are little-endian x86-64 or AArch64.) – Peter Cordes Apr 28 '23 at 15:58
  • @PeterCordes correct me if i am mistaken but i think that d: .byte -1,0,0,0 would take 4 bytes of memory. in reality i have more things on my code : .data a: .word 10 e: .word 3, 2, 1, 0 c: .word -1 d: .byte -1, 0, 0, 0 so what happens is that $10 is holding the value of e[2]? – stratakos Apr 28 '23 at 15:59
  • Maybe I should post those comments as an answer, but your code doesn't even make sense; `d-12` is loading from before the start of the `.data` section. I'd have expected a fault. Update: oh, so you left stuff out and this wasn't a [mcve] after all. ChatGPT's reasoning is obviously wrong that `d-12` would be the first byte of the 4-byte `.byte` array. (You're correct that there are 4 bytes following the `d:` label, from the 4 args to the `.byte` directive; my previous comment described what you'd get if you loaded all 4 as one word.) – Peter Cordes Apr 28 '23 at 16:01
  • yes, there's a `.word` with value `1` 12 bytes before the `d:` label, so that's what you load. – Peter Cordes Apr 28 '23 at 16:03
  • @PeterCordes alright mate thank you, i will edit the question right now – stratakos Apr 28 '23 at 16:06
  • 1
    If you single-step with QtSPIM's built-in debugger, you can see the address in `$10` before the `lw`. And you can look at memory in the debugger, too, so see how all those bytes from the various directives are laid out head to tail. ChatGPT is generally very bad at assembly language compared to other languages, perhaps because the same register names mean totally different things at different times in the same program. Don't trust anything it says about CPUs or assembly. – Peter Cordes Apr 28 '23 at 16:07

1 Answers1

1

I figuered out what went wrong, obviouslt me and chatgpt were wrong about this one since the "d: .byte -1,0,0,0" is capturing 4 bytes of memory and "c: .word -1" another 4 bytes. Finally the "e: .word 3,2,1,0" is capturing a total of 16 bytes which means that the operation $10-12 brought me to e[2] which holds the value of 1.

stratakos
  • 25
  • 7