views:

58

answers:

2

I have the following question here

.data
a: .asciiz "2021"
x: .byte 7,2,12
.text
main: addi $t2, $0, 1
lb $t3, a($t2)

Can someone explain to me, HOW the value of $t3 is 48?

thanks EDIT this is an another question which is similiar, and is confusing.

.data
a: .word 12,-5,4,0
x: .byte 5
.text
main: addi $t1, $0, 8
lw $t2, a($0)
lw $t3, a($t1)

How will you load word, from index 8, when 'a' has a length of 4?

+1  A: 

Yes, when you add $0 and 1, you get 1, which is put into $t2.

Then, when you evaluate a($t2), that's the second byte (offset 1 since it's based at offset 0) of a which is the "0", ASCII code 0x30 or 48.


From various pieces of information:

ADDI -- Add immediate (with overflow)
Description:
    Adds a register and a sign-extended immediate value
    and stores the result in a register
Operation:
    $t = $s + imm; advance_pc (4);
Syntax:
    addi $t, $s, imm

LB -- Load byte
Description:
    A byte is loaded into a register from the specified address.
Operation:
    $t = MEM[$s + offset]; advance_pc (4);
Syntax:
    lb $t, offset($s)

Register $0 always contains the hardwired value 0. MIPS has established a set of conventions as to how registers should be used. These suggestions are guidelines, which are not enforced by the hardware. However a program that violates them will not work properly with other software.

Those little snippets should hopefully be enough to explain what it's doing.


And, regarding your edit, you're incorrectly thinking that .word 12,-5,4,0 has a length of 4 bytes. In fact it has a length of 16 bytes since words in MIPS are 32 bits (four bytes) wide.

So when you load from byte offset 8, you will get the word 4.

paxdiablo
thanks, but how did you know it was 0x30? is this common knowledge or is there a formula for figuring out the hex?
It's pretty much common knowledge, you just need to get yourself an ASCII table from somewhere (book, internet, ...). The numbers `0..9` are ASCII codes `0x30..0x39`.
paxdiablo
Oh okay, I guess our prof will give us a ASCII sheet. If you can answer 1 more question which is similiar, which i added above.