Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

c - MIPS passing two arguments and saving it

I have the following code in C

int bitIndex = hash_str(input, i);

hash_str is just a function that I call. input is a pointer to a char and i a standard integer.

I'd like to implement this function in MIPS. I've determined that input = $a0 and i = $s0 (I've saved $s0 on the stack already).

Conventional wisdom tells me when it comes to calling functions I'd just

ARG I
ARG II
jal LABEL

My dilemma here is that $a0 and $s0 already 'exist', so how do I call the hash_str function with them? Furthermore, how do I store that in an integer all in MIPS?

question from:https://stackoverflow.com/questions/65862286/mips-passing-two-arguments-and-saving-it

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The C compiler will follow the standard calling conventions, though may have some options for different behavior.

Generally speaking first int or pointer type parameter goes in $a0, second in $a1, third in $a2, fourth in $a3.? int or pointer type return values goes into $v0.

The order of populating the registers doesn't matter as long as the right values have been placed in the right registers before the call instruction (jal).? So, you can first put argument 2 in $a1, then next put argument 1 in $a0, and it will still work the same.

For more information see here: https://www.dyncall.org/docs/manual/manualse11.html and scroll down to your architecture & ABI.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...