如何在寄存器中存储以2为底的数字?

时间:2018-12-22 09:45:02

标签: assembly binary ascii mips mars

但是我的教练没有回应。对于MIPS程序,我还有一个额外的信用问题,如下所示:

Extra credit covers binary to ASCII data type conversion. It is useful 
to convert the 2’s complement integer into an ASCII string so that it
can be displayed on the monitor. Derive a binary-to-ASCII conversion
routine, BinarytoASCII, for converting a 2’s complement integer stored 
in a0 register into an ASCII string stored in v0 register. The value 
initially in a0 is restricted to be within the range -999 to +999. After   
the algorithm completes execution, v0 contains the sign of the value 
initially stored in a0. The following three bytes contain the three 
ASCII codes corresponding to the three decimal digits representing its
magnitude. This algorithm always produces a string of four characters 
independent of the sign and magnitude of the integer being converted.

也许我没有正确阅读此问题,但不是不可能将二进制值直接存储到MIPS的寄存器中吗?似乎这是在要求十进制转换为ASCII。如果我错了,您能告诉我如何在MIPS中将以2为基数的号码放入寄存器吗?谢谢

1 个答案:

答案 0 :(得分:0)

寄存器中的数字为二进制。这就是为什么32位寄存器可以使用2的补码存储-2^31 .. 2^31-1中带符号的值的原因。 https://en.wikipedia.org/wiki/Two%27s_complement

如果寄存器中的本机存储格式为十进制,则左移和右移将乘以10的幂,而不是2的幂。


编码与值分开。给定32位寄存器中0b11111111111111111111110000011001的位模式,您可以将编码值描述/表示为

  • -999小数,将其解释为带符号的2的补码
  • 0xfffffc19无符号十六进制
  • 037777776031八进制
  • 0b11111111111111111111110000011001二进制

这些都是相同的数字,只是表达数学值(作为负十进制数字)或2的补码位模式(十六进制,八进制和二进制)的方式不同。

通常,十六进制仅用于描述位模式,因此通常不会看到像-0x3e7这样的负十六进制数。


常见的误解是寄存器或存储器中的数字是“十六进制”的。十六进制是二进制数字的序列化格式,允许它们以人类可读格式存储为文本。如果数字以ASCII字符序列存储,则只能以十六进制表示。

十进制数字的ASCII字符串是数字序列化的另一种方法,这就是您在这里要执行的操作。

十六进制和十进制通常在asm源代码中使用,但是汇编器会将数字汇编成32位二进制字。 (或者将16位立即数作为指令流的一部分,例如addiu $t0, $t1, 0x1234

您可以使用十六进制来讨论二进制数的值,但实际上它是以2位为基础存储的。