## Help with converting machine language instruction into MIPS assembly instruction

I think it's easy, but I don't get it.

Given the machine language instruction 0x00221820. What MIPS assembly instruction does it represent?

I know the solution. It's add $3,$1, $2 I just don't know how to get there. I know I have to convert the numbers into binary first. So you have 0000 0000 0010 0010 0001 etc.. Then you take the first 6 0's, and that's how you determine what type it is ( R-type) ? How do I do this? And then how do I know it's add$3, $1,$2 using the numbers that are left after the first 6 0's ? I know that there's a lsit where you can look it up, but you have 00221820, which is 8 segments in total. But it's just 4 commands ( add $3,$1, $2 ). Dosen't make any sense.  PhysOrg.com science news on PhysOrg.com >> Front-row seats to climate change>> Attacking MRSA with metals from antibacterial clays>> New formula invented for microscope viewing, substitutes for federally controlled drug Mentor  Quote by XodoX I think it's easy, but I don't get it. Given the machine language instruction 0x00221820. What MIPS assembly instruction does it represent? I know the solution. It's add$3, $1,$2 I just don't know how to get there. I know I have to convert the numbers into binary first. So you have 0000 0000 0010 0010 0001 etc..
If you have a number in hex already, converting to binary is pretty easy. Each hex digit is 4 binary bits.
0x00221820 = 0000 0000 0010 0010 0001 1000 0010 0000 (base 2)

From a reference I found on the MIPS instruction set (http://www.mrc.uidaho.edu/mrc/people...al/MIPSir.html), here is the encoding for add d$, s$, t$, which means add the values in s$ and t$and store the result in d$.

0000 00|ss sss|t tttt| dddd d|000 0010 0000

0000 00|00 001|0 0010| 0001 1|000 0010 0000

Can you match things up to figure out what s$, t$, and d$are?  Quote by XodoX Then you take the first 6 0's, and that's how you determine what type it is ( R-type) ? How do I do this? And then how do I know it's add$3, $1,$2 using the numbers that are left after the first 6 0's ? I know that there's a lsit where you can look it up, but you have 00221820, which is 8 segments in total.
That would be 8 bytes in total.
 Quote by XodoX But it's just 4 commands ( add $3,$1, $2 ). Dosen't make any sense.  Quote by XodoX I think it's easy, but I don't get it. Given the machine language instruction 0x00221820. What MIPS assembly instruction does it represent? I know the solution. It's add$3, $1,$2 I just don't know how to get there. I know I have to convert the numbers into binary first. So you have 0000 0000 0010 0010 0001 etc.. Then you take the first 6 0's, and that's how you determine what type it is ( R-type) ? How do I do this? And then how do I know it's add $3,$1, $2 using the numbers that are left after the first 6 0's ? I know that there's a lsit where you can look it up, but you have 00221820, which is 8 segments in total. But it's just 4 commands ( add$3, $1,$2 ). Dosen't make any sense.

I'll assume you are writing your instruction has an increasing number from right to left. In the example you are working with its probably a good idea to write the contents of memory in byte form as they appear in increasing order of memory location.

The other thing is to know how data is arranged by your architecture. There are things called the "endianess" of your data. You can have architectures that orient your words in the normal way (A,B,C,D) = 0xABCD or in the weird way (A,B,C,D) = 0xCDAB. So thats the first thing you should check.

The next is pretty easy. For a lot of architectures you usually a byte for the instruction type so fetching the first byte will sort out that. The rest is obviously dependent on what instruction it is (register,register), (register,memory) and so on.

Again find out how data is stored with the "endianess" of the architecture and you will always get the right result.

## Help with converting machine language instruction into MIPS assembly instruction

Ah, got it now! Thanks guys! However, I have been trying to to the reverse now.

You have MIPS add $t0,$t0, $zero and lw$t1, 4, ($s3) and it needs to be converted to the machine code binary form and MIPS instruction in hex. I suppose you first convert it to Hex 0x00000 ( whatever number) like above and then to the binary form. How'd I do this? I thought the hex would be on the reference sheet, but I don't see anything. Once I have that, I just convert it to binary.  Mentor No, you have it backwards. Figure out what the binary code is going to be for each instruction, then convert each one to hex. Use the instruction reference whose link I gave earlier to figure out where things need to go.  No, I don't have it backwards. It was hex to binary to mips. I also need mips to hex/binary. I have to use the reference sheet given to me and that's not on there. This reference sheet has it already listed, it need to be converted by me. http://www.megafileupload.com/en/fil...nager-pdf.html Couldn't upload it here, it was to big. I think it's on page 3, but it only says if it's add, sub etc. I suppose 1st colum "mips opcode" and second column "binary", but that dosen't include registers etc. I don't know. Mentor Your question in post #4 was  Quote by XodoX You have MIPS add$t0, $t0,$zero and lw $t1, 4, ($s3) and it needs to be converted to the machine code binary form and MIPS instruction in hex. I suppose you first convert it to Hex 0x00000 ( whatever number) like above and then to the binary form. How'd I do this? I thought the hex would be on the reference sheet, but I don't see anything. Once I have that, I just convert it to binary.
For the instruction add $t0,$t0, $zero, look up the add instruction in the reference I gave you. This will show you the binary form of this op code, and will show you where the various operands need to go. When you have everything in place, it's an easy matter to convert to hex. Starting from the least-significant bit (LSB), divide the binary number into groups of four bits. Convert each group into its hex equivalent. For example, if you had these three groups - 1011 0011 0100, the hex equivalent is 0xA34. Do the same for the lw instruction. When you said "I thought the hex would be on the reference sheet" the writer who wrote the documentation for the op codes assumed that anyone who wanted to know the format of the machine code would be able to convert back and forth between binary and hex.  No, that's not what I meant. I thought the hex of the MIPS instruction was on the sheet. Again, I can only use the sheet I posted the link to. Yours seems a little bit more clear, but I can't use it. I thought you'd have to get the Hex first. Ok, I guess I was wrong. It's the binary code you'll have to get and then convert it. Bottom line: I can't figure out how to get the bianry code for it. And I don't know what you're looking at, but your sheet has "0000 00ss ssst tttt dddd d000 0010 0000" for add. Is that what you mean? Mentor  Quote by XodoX No, that's not what I meant. I thought the hex of the MIPS instruction was on the sheet. Again, I can only use the sheet I posted the link to. Yours seems a little bit more clear, but I can't use it. I thought you'd have to get the Hex first. Ok, I guess I was wrong. It's the binary code you'll have to get and then convert it. Bottom line: I can't figure out how to get the bianry code for it. And I don't know what you're looking at, but your sheet has "0000 00ss ssst tttt dddd d000 0010 0000" for add. Is that what you mean? This is the binary code. The s, t, and d letters show which bits are used for the source, target (?), and destination operands. For example, if the instruction were add$t1, $t2,$t3, the registers involved are $9,$10, and $11, with$t1 being the destination register and the other two the s$and t$ registers.

I should say again that I haven't done any MIPS programming, but this is how it seems to work.

The five bits for sssss would be the bit pattern for 9, which is 10001. The five bits for ttttt would be the bit pattern for 10, which is 10010. The five bits for ddddd would be the bit pattern for 11, which is 10011. I've split these groups up with different colors to make them easier to comprehend.
0000 0010 0011 0010 1001 1000 0010 0000

0000 00ss ssst tttt dddd d000 0010 0000

Is that clear?

 Ok, got it! Thank you very much for your help!