简体   繁体   中英

MIPS Assembly - String to Binary Representation

I want to convert a string to its binary representation in C to fit for MIPS assembly (I am writing a MIPS assembler in C).

For the string "The sum", MIPS does it the following way (I extracted MIPS's .data section):

00100000011001010110100001010100
00100000011011010111010101110011
01101110001000000110011001101111
01100101011000100110110101110101
01101001001000000111001101110010
01110010011000010010000001101110
00100000011110010110000101110010

The letter T is 1010100 in binary. I can see 1010100 at the end of the first line, but shouldn't the rest be zero's (so it is sign extended to 32-bit since MIPS deals with 32-bits)?

h is 1101000, and the second line does not even have 1101000. Why is that?

Can someone please explain that to me?

Thank you,

$ echo "00100000011001010110100001010100
00100000011011010111010101110011
01101110001000000110011001101111
01100101011000100110110101110101
01101001001000000111001101110010
01110010011000010010000001101110
00100000011110010110000101110010
"| perl -pe '$_=pack"B*",$_'
 ehT musn foebmui srra n yar

Somehow, the characters of that string are in a weird order. Are you grouping 4 characters in an int ?

ASCII data is not "sign extended." Using MIPS Mars simulator, I typed in:

.data 
.asciiz "The sum"

.text
main:

This generated the data segment:

Address:   Value:
0x10010000 0x20656854
0x10010004 0x006d7573

BUT WAIT, isn't the hex value of "The sum" just 54:68:65:20:73:75:6d ? Yup, notice the order they are actually stored in. Or rather, look at the Endianness . The lowest value has the highest address (or, the most significant byte is the first address). ASCII strings are stored using Big Endian format.

So, when you are storing your strings, calculate the hex value, split the string into words, and then store each character in reverse order. Then convert to binary.

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.

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