05.08.2014 Views

Lecture Notes for Computer Architecture II - St. Cloud State University

Lecture Notes for Computer Architecture II - St. Cloud State University

Lecture Notes for Computer Architecture II - St. Cloud State University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Translation of Procedures<br />

Problem<br />

HLL procedure MIPS<br />

swap(int v[], int k)<br />

{ int temp<br />

temp = v[k];<br />

v[k]=v[k+1];<br />

v[k+1]=temp;<br />

}<br />

Page | 53<br />

Complete Solution<br />

- allocate registers to variables<br />

- generate code <strong>for</strong> the body of the procedure<br />

- reserve contents of registers across the procedure invocation<br />

<strong>St</strong>ep1 Assign variables to registers<br />

temp v k<br />

<br />

$t 0 $a 0 $a 1<br />

<strong>St</strong>ep 2 Do simple translations<br />

mult $a 1 $a 1 4<br />

add $t 1 $a 0 $a 1<br />

lw $t 0 0($t 1 )<br />

lw $t 2 4($t 1 )<br />

sw $t 2 0($t 1 )<br />

sw $t 0 4($t 1 )<br />

jr $ra jump back to calling procedure<br />

in MIPS – mult $a 1 $a 1 4<br />

Adjust the stack to make room <strong>for</strong> 2 items<br />

Sub $sp, $sp, 8<br />

<strong>St</strong>ep 3 save the content of registers on stack<br />

sw $t 1 4($SP)<br />

sw $t 2 0($SP)<br />

<strong>St</strong>ep 4 load the registers back after completion of the procedure<br />

lw $t 2 0($sp)<br />

lw $t 1 4($sp)<br />

Adjust the stack to delete 2 items<br />

Add $sp, $sp, 8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!