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.
In the following line of code, is there a way to make it so that test just equals "bc" instead of "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?
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.
In your case it's just 3 bytes left out of the 4 (including the terminating 0
). So yes, you could do this just by shifting the word by 8 bits (1 byte). 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.
For little-endian MIPS (like MARS simulates), that would be a right shift because the first byte in memory is the least significant. 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.
Note that this will leave the word at test
being 'b', 'c', 0, 0
. So yes, as an implicit-length string it's "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. Like addiu $t0, $t0, 1
.
Or equivalently, la $t0, test+1
is a pointer to 1 byte past the start.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.