如何在寄存器中存储基数 2?
How to store a base 2 number in a register?
但我的讲师没有回应。我有一个 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 的转换。如果我错了,你能展示如何将 base-2 数字放入 MIPS 中的寄存器吗?谢谢
寄存器中的数字是二进制的。这就是为什么 32 位寄存器可以存储来自 -2^31 .. 2^31-1
的有符号值,使用 2 的补码编码。 https://en.wikipedia.org/wiki/Two%27s_complement
如果寄存器中的本机存储格式是十进制,则向左和向右移位将 multiply/divide 10 的幂,而不是 2 的幂。
编码与值是分开的。给定 32 位寄存器中 0b11111111111111111111110000011001
的位模式,您可以将编码值描述/表示为
-999
十进制,将其解释为带符号的 2 的补码
0xfffffc19
无符号十六进制
037777776031
八进制
0b11111111111111111111110000011001
二进制
这些都是相同的数字,只是表达数学值(作为负十进制数)或 2 的补码位模式(十六进制、八进制和二进制)的不同方式。
通常十六进制仅用于描述位模式,因此您通常不会看到像 -0x3e7
.
这样的负十六进制数
寄存器或内存中的数字是 "in hex" 是一种常见的误解。十六进制是二进制数的序列化格式,允许它们以人类可读的格式存储为文本。如果数字存储为 ASCII 字符序列,则该数字只有十六进制。
十进制数字的 ASCII 字符串是另一种序列化数字的方法,这就是这里要求您做的。
十六进制和十进制在asm源代码中很常用,但是assembler会将你的数字assemble转换成32位二进制字。 (或者 16 位立即数作为指令流的一部分,例如 addiu $t0, $t1, 0x1234
)
您可以使用十六进制谈论二进制数的值,但它实际上是以 2 位为基数存储的。
但我的讲师没有回应。我有一个 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 的转换。如果我错了,你能展示如何将 base-2 数字放入 MIPS 中的寄存器吗?谢谢
寄存器中的数字是二进制的。这就是为什么 32 位寄存器可以存储来自 -2^31 .. 2^31-1
的有符号值,使用 2 的补码编码。 https://en.wikipedia.org/wiki/Two%27s_complement
如果寄存器中的本机存储格式是十进制,则向左和向右移位将 multiply/divide 10 的幂,而不是 2 的幂。
编码与值是分开的。给定 32 位寄存器中 0b11111111111111111111110000011001
的位模式,您可以将编码值描述/表示为
-999
十进制,将其解释为带符号的 2 的补码0xfffffc19
无符号十六进制037777776031
八进制0b11111111111111111111110000011001
二进制
这些都是相同的数字,只是表达数学值(作为负十进制数)或 2 的补码位模式(十六进制、八进制和二进制)的不同方式。
通常十六进制仅用于描述位模式,因此您通常不会看到像 -0x3e7
.
寄存器或内存中的数字是 "in hex" 是一种常见的误解。十六进制是二进制数的序列化格式,允许它们以人类可读的格式存储为文本。如果数字存储为 ASCII 字符序列,则该数字只有十六进制。
十进制数字的 ASCII 字符串是另一种序列化数字的方法,这就是这里要求您做的。
十六进制和十进制在asm源代码中很常用,但是assembler会将你的数字assemble转换成32位二进制字。 (或者 16 位立即数作为指令流的一部分,例如 addiu $t0, $t1, 0x1234
)
您可以使用十六进制谈论二进制数的值,但它实际上是以 2 位为基数存储的。