简体   繁体   English

从内存中错误地加载值以mips方式注册

[英]incorrect loading of value from memory to register in mips

I do a project in mips in mars simulator and my program hangs on syscall due to the odd behavior. 我在火星模拟器中用mips进行了一个项目,由于奇怪的行为,我的程序挂起在系统调用上。

I want to store a given char buffer to file and pass to syscall the appropriate length of it. 我想将一个给定的char缓冲区存储到文件中,并传递给syscall适当的长度。

.data
line_length: .space 4
# ...

.text
# ...

lb  $t0, line_length
li  $v0, 15
lb  $a0, io_descriptors+4
la  $a1, output_line
move    $a2, $t0
syscall

The value stored in line_length is 0x80. 存储在line_length中的值是0x80。 And that is indeed the case also just before and after the lb instruction. 在lb指令之前和之后也确实如此。 The value in $t0 after lb is 0xffffff80 though, for a reason beyond my comprehension. lb之后的$ t0中的值是0xffffff80,但这是我无法理解的原因。 As the number is negative, the syscall fails. 由于数字为负数,系统调用失败。 What could be the reason that $t0 doesn't store 0x80 value? 可能是因为$ t0没有存储0x80值? How can I fix it? 我该如何解决?

When loading a single byte (8 bits) into a register (32 bits on MIPS), the value needs to be extended to fill the remaining 24 bits. 将单个字节(8位)加载到寄存器(MIPS上为32位)时,需要扩展该值以填充剩余的24位。 There are two options: 有两种选择:

  • Zero extend which fills the remaining upper bits with zeros. 零扩展 ,用填充剩余的高位。 This makes sense when the value is unsigned . 当值无符号时,这是有意义的。
  • Sign extend which fills the remaining upper bits with a copy of the most-significant bit. 符号扩展 ,用最高有效位的副本填充剩余的高位。 This makes sense when the value is signed . 签名值时这是有意义的。

lb loads a single byte, and sign-extends it before putting it into the 32-bit register. lb加载一个字节,并在将其放入32位寄存器之前对其进行符号扩展 Since you're reading 0x80 , the most-significant bit is a 1, so the 32-bit result is 0xFFFFFF80 . 由于您正在读取0x80 ,因此最高有效位为1,因此32位结果为0xFFFFFF80

Since you've declared line_length to be 4 bytes, you want to use lw which loads a full 32-bit word (with no extension). 由于您已将line_length声明为4个字节,因此您希望使用lw来加载完整的32位字(没有扩展名)。 Just make sure you're being consistent with the instructions you use to access and manipulate this variable. 只需确保您与用于访问和操作此变量的说明一致。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM