开发者

Simple MIPS question, about load byte

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 simi开发者_如何学JAVAliar, 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?


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.


FYI there is an ASCII chart on page 11 of handout 4 ;) But I dont get this: "when, 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"" The binary representation of '2021' I believe is: 00110010001100000011001000110001 So, when you say "which is the "0"" do you mean the 2nd bit from the right? Is that the 0 you are talking about? I dont get where the zero comes from.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜