简体   繁体   English

如何使用移位从 MIPS32 程序集中的 4 字节字符串中删除字符?

[英]How to remove a char from a 4-byte string in MIPS32 assembly using a shift?

I'm new to MIPS32 assembly and trying to delete a character in a string (delete the first character, specifically) stored in the.data section but have no clue how to do so.我是 MIPS32 程序集的新手,并试图删除存储在 .data 部分中的字符串中的一个字符(特别是删除第一个字符),但不知道该怎么做。

In the following line of code, is there a way to make it so that test just equals "bc" instead of "abc"在下面的代码行中,有没有办法让 test 等于“bc”而不是“abc”

test:           .asciiz     "abc"

Is this simply a matter of using something like logical shifting left by 2 to remove the first char, or do I need to offset by something, or is there an opcode to just straight delete it?这仅仅是使用逻辑左移2来删除第一个字符的问题,还是我需要抵消一些东西,或者是否有一个操作码可以直接删除它?

As Need to remove all non letter elements from a string in assembly (for x86) explains, removing a character in a string means copying over the whole rest of the string.正如需要从汇编中的字符串中删除所有非字母元素(对于 x86)所解释的那样,删除字符串中的字符意味着复制字符串的整个 rest。

In your case it's just 3 bytes left out of the 4 (including the terminating 0 ).在您的情况下,4 个字节中只剩下 3 个字节(包括终止的0 )。 So yes, you could do this just by shifting the word by 8 bits (1 byte).所以是的,你可以通过将单词移动 8 位(1 个字节)来做到这一点。 Especially if you make sure test is word-aligned with .p2align 2 before it, so you can safely lw and sw all 4 bytes with one load.特别是如果您确保test与之前的.p2align 2字对齐,因此您可以安全地lwsw一次加载所有 4 个字节。

For little-endian MIPS (like MARS simulates), that would be a right shift because the first byte in memory is the least significant.对于 little-endian MIPS(如 MARS 模拟),这将是一个右移,因为 memory 中的第一个字节是最不重要的。 And right shifts shift out the low (least significant) bits.右移移出低(最低有效)位。

For big-endian MIPS (most significant byte first, like some real MIPS CPUs operate), that would be a left shift, removing the most significant byte and shifting the low bits up.对于大端 MIPS(最高有效字节在前,就像一些真正的 MIPS CPU 一样),这将是左移,移除最高有效字节并将低位向上移动。


Note that this will leave the word at test being 'b', 'c', 0, 0 .请注意,这将使test中的单词为'b', 'c', 0, 0 So yes, as an implicit-length string it's "bc" .所以是的,作为隐式长度字符串,它是"bc"

Also note that if you just had a pointer in a register, you could get a pointer to "bc" by simply incrementing it by 1 instead of modifying memory.另请注意,如果您在寄存器中只有一个指针,则只需将其增加 1 即可获得指向"bc"的指针,而不是修改 memory。 Like addiu $t0, $t0, 1 .addiu $t0, $t0, 1

Or equivalently, la $t0, test+1 is a pointer to 1 byte past the start.或者等价地, la $t0, test+1是指向开始后 1 个字节的指针。

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

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