Jump to content

Assembly code explain MIPS


Sayuri2009
 Share

Recommended Posts

Hi everybody

Unfortunately, I dont understand the following code. Can pls someone explain how to solve this?

 

How do I do shift less than unsigned without knowing the value of a1? How can I translate this code in java to understand?

 

Thanks,

image.png.d27a0b921bc0b6b3de46bfbe85e772e6.png

Edited by Sayuri2009
more infos
Link to comment
Share on other sites

there were people that did a tone of reverse engineering on PS2 mips architecture, i don't know too much about this topic let alone translating but its interesting to hear about these topics and usages.. hopefully someone follows through...!

Link to comment
Share on other sites

Without even knowing this particular language, the code seems simple enough to be readable.

sltu is not a shift instruction if my guess is correct.  It think it is 'set less than unsigned' which just sets condition bits without changing any other register value.

srl is a shift instruction

To get more help, you need to show what effort you've made to understand it. Translating to java is perhaps not necessary. Try reading it directly.

Edited by Halc
Link to comment
Share on other sites

1 hour ago, Sayuri2009 said:

How can I translate this code in java to understand?

You can learn machine code without actually having any manual or tutorial.

Basically open debugger, in disassembly mode, and step-by-step execute instructions.

Watch the registers change. Observe memory and how it changes.

1 hour ago, Sayuri2009 said:

How do I do shift less than unsigned ...

slt means "Set on Less Than".

sltu means "Set on Less Than Unsigned".

1 hour ago, Sayuri2009 said:

... without knowing the value of a1?

Because it is an argument to the function?

Edited by Sensei
Link to comment
Share on other sites

45 minutes ago, Sensei said:

You can learn machine code without actually having any manual or tutorial.

Basically open debugger, in disassembly mode, and step-by-step execute instructions.

Watch the registers change. Observe memory and how it changes.

slt means "Set on Less Than".

sltu means "Set on Less Than Unsigned".

Because it is an argument to the function?

Thanks for your reply and thanks for the hint with the assembly editor. 

1 hour ago, Halc said:

Without even knowing this particular language, the code seems simple enough to be readable.

sltu is not a shift instruction if my guess is correct.  It think it is 'set less than unsigned' which just sets condition bits without changing any other register value.

srl is a shift instruction

To get more help, you need to show what effort you've made to understand it. Translating to java is perhaps not necessary. Try reading it directly.

What I understand so far is the following comments that I made. But from this I don't get it how to interpret. 

image.png.f193d93fd2571fadf5ba640eaaafac89.png

Link to comment
Share on other sites

OK so far. It's already practically natural language.

What structure do you see?  What's the control flow? You give no indication if you see it or not.

What are the inputs and outputs?  I can see them without know the convention used for passed and return variables.

I don't see a stack being set up or utilized as is common with large functions, so this isn't a complex task. I also don't see any other functions or library calls being used.

Link to comment
Share on other sites

Thanks for your reply.

What I see is there is one return value v0 because its always the case in MIPS (I think) and what I also know from MIPS is that a0, a1 are parameters.

What I also know from this code is there are two loops one is the inner and the other is the outer loop. t4 = 32 is set and starts in inner loop subi decrements the counter if its 0 then it will jump to the next label. Here is my understanding from the code in green but I am not sure if I understand it correctly. 

why is there andi? its a mask?

srl means I am doing a multiplication by 2.

start: here v0 = 0 and t0 = 0 is the initialization.

two loops: outer and inner

outer

t3 = I am loading the value from a0 into t3.

t4 is set to 32

inner

if(t4 == 0 ) go to next

else

do mask with and

sum up v0 with t3 in v0

multiply t3 by 2

decrement the counter t4 by 1

jump to the label inner and do it until the condition beq is true, that means if t4 == 0 then jump to next label

next

t0 = 1

a0 = 4

jump to outer loop

 

Link to comment
Share on other sites

43 minutes ago, Sayuri2009 said:

What I see is there is one return value v0 because its always the case in MIPS (I think) and what I also know from MIPS is that a0, a1 are parameters.

Right. I don't know MIPS, but I could see that a0 and a1 were the only things referenced without setting them first, and v0 is never used, only written to. So I figured it out that way. Return in v0 is probably a convention, not anything made necessary by the chip hardware. I could be wrong.

I've programmed in Z80, IBM 360 (anybody remember Tuggle?), MC6809, 68020, and Pentium.

Quote

What I also know from this code is there are two loops one is the inner and the other is the outer loop.

Right. I didn't want to talk about that until you saw it yourself.  It helps that the code seems written by a human, giving those obvious label names. A compiler would not have chosen those.

Quote

t4 = 32 is set and starts in inner loop subi decrements the counter if its 0 then it will jump to the next label.

Right.  Iterate a fixed 32 times through inner loop.

Quote

why is there andi? its a mask?

Yep

Quote

srl means I am doing a multiplication by 2.

sll means that. You're going the wrong way.  Right shift divides the number by 2 (or more if by more than 1 bit)

Quote

t3 = I am loading the value from a0 into t3.

No.  That would be something like mov $t3 $a0.  What do the parentheses mean?":  0($a0)

 

When do we exit the outer loop?  In words, not just a literal interpretation of the instruction.

Edited by Halc
Link to comment
Share on other sites

That was the 'literal interpretation' I was hoping to avoid. We're making only small steps this way.

What is the purpose of the inner loop? What is the purpose of the outer loop?

What is the purpose of the function (in one sentence)?

You have all the information you need to answer that now, and it's only the first question. It's probably best to answer that before considering the other questions. The rest of the questions seem to concern finding potential bugs which is hard to do if you don't know the purpose of the routine.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.