4 Replies - 269 Views - Last Post: 12 February 2019 - 10:42 PM

#1 TonyRymond   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 09-February 19

Convert code to assemply

Posted 09 February 2019 - 11:13 AM

Hello !
I'm doing homework and didn't understand why the "sw" in MIPS used at all ...what I claim that there's no need to use sw..so why he used it ?!
void initial()
{
int i;
i=0;
}


converted to
addi $sp, $sp, -4
sw $s0, 0($sp)
add $s0, $zero, $zero


I may not understand the function "sw" but I hope I get an explanation, why did he use store word before adding? store word mean to get the value of s0 which is "trash" and will put it into *(sp+0); so after adding the value of s0 is 0 but didn't store that in the memory, I mean the value of $sp is still trash in the memory and didn't get updated to zero... any clue why he used sw at all?!

Is This A Good Question/Topic? 0
  • +

Replies To: Convert code to assemply

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6772
  • View blog
  • Posts: 23,077
  • Joined: 05-May 12

Re: Convert code to assemply

Posted 09 February 2019 - 07:49 PM

Search for the term "calling conventions".

The code you presented above is out of context. I'm willing to bet that if you show us the rest of the generated code, it'll show that the so-called "trash" that was saved to the stack is restored to $s0. The calling convention being used by that compiler seems to be that it must preserve the value of the $s0 register by the time control is returned back to whoever called initial().
Was This Post Helpful? 0
  • +
  • -

#3 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2312
  • View blog
  • Posts: 4,419
  • Joined: 30-May 10

Re: Convert code to assemply

Posted 10 February 2019 - 11:16 AM

Have fun -> http://reliant.colab.duke.edu/c2mips/
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6772
  • View blog
  • Posts: 23,077
  • Joined: 05-May 12

Re: Convert code to assemply

Posted 10 February 2019 - 11:54 AM

I love the big bold text from that site:

Quote

Do not attempt to use this tool to "automate" homework questions involving MIPS assembly programming. Your code will not work in SPIM for the reasons above and you'll spend more time adapting it than if you'd just written it from scratch. Also, code generated by compiler has a detectable structure, which we'll check for.

Was This Post Helpful? 0
  • +
  • -

#5 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,107
  • Joined: 03-April 12

Re: Convert code to assemply

Posted 12 February 2019 - 10:42 PM

In some heavily pipelined processors that could actually store the result in stack. The idea is that storing to memory takes so much longer that the next instruction is finished by the time the register contente is moved to the data bus. For instance C6000-series DSPs work that way.
I don't know if there are such variants of MIPS, so I, too, put my money on restoring the register before returning.

The code didn't store the value (zero), because it isn't used or returned.
I'd guess the 'add' and 'sw' change places if you tried with similar function returning the result.

This post has been edited by turboscrew: 12 February 2019 - 10:46 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1