title: Translate C++ to MIPS Assembly
body: <p>I need help with this last part of this problem. I'm basically trying to translate C++ code into MIPS Assembly language.</p>
  
  <p>Assume a is in $s0, b in $s1, c in $s2, x in $s4, y in $s5, and z in $s6.</p>
  
  <p>I finished almost all of them but I am stuck on these two, I know some parts of it but I'm having trouble putting it together as a whole. The parts I know will be followed by hashtags with the assembly code. Thanks for any help.</p>
  
  <p>1.</p>
  
  <pre><code>for(x = 0; x &lt;= z; x++) # x = 0; is: addi $s4, $0, 0
  y = y + 1; # addi $s5, $s5, 1
  y = 0; # addi $s5, $0, 0
  </code></pre>
  
  <p>2.</p>
  
  <pre><code>if(y &gt; z)
  x = 0; # addi $s4, $0, 0
  else x = 1; # else: addi $s4, $0, 1
  </code></pre>
  
  <p>Here are the oringinal problems without the hashtags incase I am wrong: </p>
  
  <p>1.</p>
  
  <pre><code>for(x = 0; x &lt;= z; x++) 
  y = y + 1; 
  y = 0; 
  </code></pre>
  
  <p>2.</p>
  
  <pre><code>if(y &gt; z) 
  x = 0; 
  else x = 1; 
  </code></pre>
  
  <p>Thanks again.</p>
  
  <p>Attempt at 2, not sure if right.</p>
  
  <pre><code>ifLoop:
  
  add $s5, ? , $s6
  addi $s4, $0, 0
  
  ifLoop
  
  else:
  
  addi $s4, $0, 1
  
  else
  </code></pre>
  
  <p>Practice: (Assume array p is in $s7)</p>
  
  <pre><code>p[0] = 0; 
  int a = 2; 
  p[1] = a; 
  p[a] = a;     
  </code></pre>
  
  <p>My attempt:</p>
  
  <pre><code>sw $0, 0($s7) 
  addiu $s0, $0, 2 
  sw $s0, 4($s7) 
  sll $t0, $s0, 2 
  addu $t1, $t0, $s7 
  sw $s0, 0($t1)            
  </code></pre>