Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal...

46
Computation Computation 5JJ70 5JJ70 5JJ70 5JJ70 ‘Implementatie van rekenprocessen’ ‘Implementatie van rekenprocessen’ Translate C into MIPS assembly Translate C into MIPS assembly Henk Corporaal Henk Corporaal December 2009 December 2009

Transcript of Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal...

Page 1: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

ComputationComputation

5JJ70 5JJ70 5JJ70 5JJ70 ‘Implementatie van rekenprocessen’‘Implementatie van rekenprocessen’

Translate C into MIPS assemblyTranslate C into MIPS assembly

Henk CorporaalHenk CorporaalDecember 2009December 2009

Page 2: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

WelcomeSo far we covered almost all of C,and a large part of C++and a large part of C++We also introduced the MIPS processor and its instructions

C++ topics not treated:copy constuctorpyabstract classesvirtual destructorsdefine your own operators (overloading existing ones)f y u w p ( g g )exceptions, using catch and throwformatted stream i/otemplates (we'll come back on this)

@HC Computation 5JJ70 pg 2

templates (we ll come back on this)Refer to the online manuals and tutorials, or the C++ bible

Page 3: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Topics of today Final lecture of first semester

Recap of MIPS instruction set and formatsMIPS addressing modesR i t ll tiRegister allocation

graph coloringspillingp g

Translating C statements into Assemblerif statement

h l

MIPS_3000

while statementswitch statementprocedure / function (leaf and non-leaf)

@HC Computation 5JJ70 pg 3

p ( )stack save and restore mechanism

Page 4: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Actions to be performed for an instruction

let's take a load instruction at address 0x800: 0x800 lw $s1, 8($s2)

1. Cycle 1Fetch instruction to which PC pointsFetch instruction to which PC pointsNew PC = PC+4

2. Cycle 2Decode instructionDecode instructionFetch $s2 from register file

3. Cycle 3Execution: Add 8 to $s2Execution: Add 8 to $s2

4. Cycle 4Fetch data from address (8+$s2)

C l 5

@HC Computation 5JJ70 pg 4

5. Cycle 5Write data back to register file location $s1

Page 5: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Pipelined MIPS

@HC Computation 5JJ70 pg 5

Page 6: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Main Types of Instructions

ArithmeticIntegerIntegerFloating Point

Memory access instructionsLoad & Store

Control flowJumpConditional BranchCall & Return

@HC Computation 5JJ70 pg 6

Page 7: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

MIPS arithmetic

C code: A = B + C + D;C code: A = B + C + D;E = F - A;

MIPS dMIPS code: add $t0, $s1, $s2add $s0, $t0, $s3sub $s4, $s5, $s0, ,

Operands must be registers, only 32 registers provided

Design Principle: smaller is faster. Why?

@HC Computation 5JJ70 pg 7

Page 8: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Registers vs. Memory

Arithmetic instructions operands must be registers, only 32 registers provided— only 32 registers provided

Compiler associates variables with registersWhat about programs with lots of variables ?What about programs with lots of variables ?

CPU Memory

register file

@HC Computation 5JJ70 pg 8

IO

Page 9: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Register allocation

Compiler tries to keep as many variables in registers as possible: graph coloringpossible: graph coloring

Some variables can not be allocatedSome variables can not be allocatedlarge arrays (too few registers)aliased variables (variables accessible through pointers in C)d i ll t d i bldynamic allocated variables

heapstack

Compiler may run out of registers => spilling

@HC Computation 5JJ70 pg 9

Page 10: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Register allocation using graph coloringRegister allocation using graph coloring

Example:

Program:b d

Live Ranges

Example:

g

a := c :=

a b c da define

(def)b :=

:= bd :=

:= a:= c:= da consume

(cons)

@HC Computation 5JJ70 pg 10

( )

Page 11: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Register allocation using graph coloringg g g p g

Inf n G ph

a

Inference Graph

a

b cb cColoring:

a = redb

dd

b = greenc = blued = green

Graph needs 3 colors => program needs 3 registers

@HC Computation 5JJ70 pg 11

p p g g

Page 12: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Register allocation: spilling

Spill/ Reload code Spill/ Reload code is needed when there are not enough colors (registers) to color the interference graph

Program:a b c d

Live Ranges

Example: What if only tworegisters available ?

a := c := store c

a b c d

g store cb :=

:= bd :=

:= aload c

:= c

@HC Computation 5JJ70 pg 12

:= d

Page 13: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Memory layout: Alignment

this word is aligned; the others are not!031 071523

ress

48

12

addr 12

16202024

Question: If words are aligned, what are then the least 2 i ifi t bit f d dd ?

@HC Computation 5JJ70 pg 13

significant bits of a word address?

Page 14: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Instructions: Load and storeExample:

C code: A[8] = h + A[8];

MIPS dMIPS code: lw $t0, 32($s3)add $t0, $s2, $t0sw $t0, 32($s3), ( )

Store word operation has no destination (reg) operandRemember arithmetic operands are registers, not memory!

@HC Computation 5JJ70 pg 14

Page 15: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Machine LanguageInstructions, like registers and words of data, are also 32 bits longg

Example: add $t0, $s1, $s2Registers have numbers: $t0=9, $s1=17, $s2=18

I t ti F tInstruction Format:

000000 10001 10010 01001 00000 100000

op rs rt rd shamt funct

6 bits 5 bits 5 bits 5 bits 6 bits5 bits

Question:

@HC Computation 5JJ70 pg 15

Can you guess what the field names stand for?

Page 16: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Machine LanguageConsider the load-word and store-word instructions,

What would the regularity principle have us do?N i i l : G d d si d ds isNew principle: Good design demands a compromise

Introduce a new type of instruction formatI t p f d t t nsf inst u ti nsI-type for data transfer instructionsother format was R-type for register

Example: lw $t0, 32($s2)

35 18 9 32

op rs rt 16 bit number

@HC Computation 5JJ70 pg 16

Where's the compromise?

Page 17: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Control

Decision making instructionsalter the control flowalter the control flow,i.e., change the "next" instruction to be executed

MIPS diti l b h i st ti s:MIPS conditional branch instructions:

bne $t0, $t1, Label beq $t0, $t1, Label

Example: if (i==j) h = i + j;Example: if (i==j) h = i + j;

bne $s0, $s1, Label

@HC Computation 5JJ70 pg 17

add $s3, $s0, $s1Label: ....

Page 18: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Control

MIPS unconditional branch instructions:j labelj label

Example:Example

if (i!=j) beq $s4, $s5, Lab1h=i+j; add $s3 $s4 $s5h=i+j; add $s3, $s4, $s5

else j Lab2h=i-j; Lab1: sub $s3, $s4, $s5

Lab2: ...

@HC Computation 5JJ70 pg 18

Page 19: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

So far:

Instruction Meaning

add $s1,$s2,$s3 $s1 = $s2 + $s3sub $s1,$s2,$s3 $s1 = $s2 – $s3l $ 1 100($ 2) $ 1 M [$ 2+100]lw $s1,100($s2) $s1 = Memory[$s2+100] sw $s1,100($s2) Memory[$s2+100] = $s1bne $s4,$s5,L Next instr. is at Label if $s4 ° $s5beq $s4 $s5 L Next instr is at Label if $s4 = $s5beq $s4,$s5,L Next instr. is at Label if $s4 = $s5j Label Next instr. is at Label

F tFormats:op rs rt rd shamt funct

op rs rt 16 bit address

R

I

@HC Computation 5JJ70 pg 19

op 26 bit addressJ

Page 20: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Used MIPS compiler conventions

Name Register number Usage$zero 0 the constant value 0$v0-$v1 2-3 values for results and expression evaluation$ 0 $ 3 4 7 arguments$a0-$a3 4-7 arguments$t0-$t7 8-15 temporaries$s0-$s7 16-23 saved (by callee)$s0 $s7 16 23 saved (by callee)$t8-$t9 24-25 more temporaries$gp 28 global pointer$sp 29 stack pointer$fp 30 frame pointer$ 31 t dd

@HC Computation 5JJ70 pg 20

$ra 31 return address

Page 21: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

ConstantsSmall constants are used quite frequently (50% of operands) e.g., A = A + 5;

B = B + 1;;C = C - 18;

Solutions? Why not?put 'typical constants' in memory and load them?put typical constants in memory and load them?create hard-wired registers (like $zero) for small constants?. . . ???

MIPS Instructions:

addi $29 $29 4addi $29, $29, 4slti $8, $18, 10andi $29, $29, 6ori $29, $29, 4

@HC Computation 5JJ70 pg 21

, ,

How do we make this work?

Page 22: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

How about larger constants?We'd like to be able to load a 32 bit constant into a registerMust use two instructions new "load upper immediate" Must use two instructions, new load upper immediate instruction

lui $t0, 1010101010101010 filled with zeros

1010101010101010 0000000000000000

Then must get the lower order bits right, i.e.,ori $t0, $t0, 1010101010101010

1010101010101010 0000000000000000

0000000000000000 1010101010101010ori

@HC Computation 5JJ70 pg 22

1010101010101010 1010101010101010

ori

Page 23: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Addresses in Branches and JumpsInstructions:

bne $t4,$t5,Label Next instruction is at Label if $t4 ≠ $t5, ,beq $t4,$t5,Label Next instruction is at Label if $t4 = $t5j Label Next instruction is at Label

FFormats:

op rs rt 16 bit address

op 26 bit address

I

J

Questions:Above addresses are not 32 bits !!

How do we extend them to 32 bits?H d h dl hi i h l d d i i

@HC Computation 5JJ70 pg 23

How do we handle this with load and store instructions?

Page 24: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Addresses in Branches

Instructions:bne $t4,$t5,Label Next instruction is at Label if $t4 ≠ $t5bne $t4,$t5,Label Next instruction is at Label if $t4 ≠ $t5beq $t4,$t5,Label Next instruction is at Label if $t4 = $t5

Formats:

op rs rt 16 bit addressI

Could specify a register (like lw and sw) and add it to addressuse Instruction Address Register (PC = program counter)most branches are local (principle of locality)

Jump instructions just use high order (4) bits of PC

@HC Computation 5JJ70 pg 24

p j g ( )32-bit Jump address = PC[31..28} + Instr[25..0] + [00]Address boundaries of 256 MB

Page 25: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Overview of MIPS

simple instructions, all 32 bits widevery structured no unnecessary baggagevery structured, no unnecessary baggageonly three instruction formats

op rs rt rd shamt funct

op rs rt 16 bit address

R

I

op 26 bit addressJ

@HC Computation 5JJ70 pg 25

Page 26: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

To summarize:MIPS assembly language

C t I t ti E l M i C tCategory Instruction Example Meaning Commentsadd add $s1, $s2, $s3 $s1 = $s2 + $s3 Three operands; data in registers

Arithmetic subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3 Three operands; data in registers

add immediate addi $s1, $s2, 100 $s1 = $s2 + 100 Used to add constantsload word lw $s1, 100($s2) $s1 = Memory[$s2 + 100] Word from memory to registerstore word sw $s1, 100($s2) Memory[$s2 + 100] = $s1 Word from register to memory

Data transfer load byte lb $s1 100($s2) $s1 = Memory[$s2 + 100] Byte from memory to registerData transfer load byte lb $s1, 100($s2) $s1 = Memory[$s2 + 100] Byte from memory to registerstore byte sb $s1, 100($s2) Memory[$s2 + 100] = $s1 Byte from register to memoryload upper immediate lui $s1, 100 $s1 = 100 * 216 Loads constant in upper 16 bits

branch on equal beq $s1, $s2, 25 if ($s1 == $s2) go to Equal test; PC-relative branchbranch on equal beq $s1, $s2, 25 if ($s1 $s2) go to PC + 4 + 100

Equal test; PC-relative branch

Conditional

branch on not equal bne $s1, $s2, 25 if ($s1 != $s2) go to PC + 4 + 100

Not equal test; PC-relative

b h t l th slt $s1 $s2 $s3 if ($s2 < $s3) $s1 1 C l th f b bbranch set on less than slt $s1, $s2, $s3 if ($s2 < $s3) $s1 = 1; else $s1 = 0

Compare less than; for beq, bne

set less than immediate

slti $s1, $s2, 100 if ($s2 < 100) $s1 = 1; else $s1 = 0

Compare less than constant

@HC Computation 5JJ70 pg 26

jump j 2500 go to 10000 Jump to target addressUncondi- jump register jr $ra go to $ra For switch, procedure returntional jump jump and link jal 2500 $ra = PC + 4; go to 10000 For procedure call

Page 27: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Floating point instructions

Mips has separate floating point registers $f0, $f1, $f2, …And special instructions that operate on them:

single ( s) or double ( d)add.s add.dsub.s sub.dmul s mul d

single (.s) or double (.d)

mul.s mul.ddiv.s div.d

dCompare two registers

c.x.s c.x.dbc1t bc1f Branch if true, branch if false

@HC Computation 5JJ70 pg 27

lwc1swc1

Load and save fp words

Page 28: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Floating point instructions examples

add.s $f2, $f3, $f4

add.d $f2, $f4, $f6 // adds pairs of float registers

// $f1 M [$ 2 100]lwc1 $f1, 100($s2) // $f1 = Memory[$s2+100]

c.lt.s $f2, $f3 // if ($f2 < $f3) cond = 1; else cond = 0

bclt 25 // if (cond==1) goto PC+4+100

@HC Computation 5JJ70 pg 28

Page 29: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Assembly Language vs. Machine LanguageAssembly provides convenient symbolic representationrepresentat on

much easier than writing down numberse.g., destination register first

Machine language is the underlying realitye.g., destination register is no longer first

Assembly can provide 'pseudoinstructions'e.g., “move $t0, $t1” exists only in Assembly would be implemented using “add $t0,$t1,$zero”

When considering performance you should count l i t ti !

@HC Computation 5JJ70 pg 29

real instructions!

Page 30: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

1. Im m e diate a dd re ssing

o p rs r t Im m ed ia te

MIPS addressing modes

R eg is te rs

R eg is ter

2. R eg is ter ad dre ss in g

o p rs r t rd . . . fu nc t

M e m o r y

R eg is ter

3. B ase ad dress in g

o p rs r t A d dress

B y te H a lfw ord W o rdR e gister +

M e m o r y

W o rd

4. P C -re la tive a dd re ss in g

o p rs r t A d dress

P C +

5. P se ud o dire ct ad dress in g

@HC Computation 5JJ70 pg 30

M e m o r y

W o rd

o p A d d re ss

P C

Page 31: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

More complex stuff

InequalitiesWhil t t tWhile statementCase/Switch statementProcedureProcedure

leafnon-leaf / recursive

StackMemory layoutCh t St iCharacters, StringsArrays versus Pointers

@HC Computation 5JJ70 pg 31

Page 32: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Inequalities

We have: beq, bne, what about Branch-if-less-than?New instruction:New instruction:

if $s1 < $s2 then$t0 = 1

slt $t0, $s1, $s2 else $t0 = 0

Can use this instruction to build "blt $s1, $s2, Label"blt is pseudo instructionyou can now build general control structuresy g

Note that the assembler needs a register to do this,— use conventions for registers

@HC Computation 5JJ70 pg 32

use conventions for registers

Page 33: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

While statement

while (save[i] == k)Registers allocation:• i $s3

i=i+j;i $s3

• base of save[] $s6• k $s5

Loop: muli $t1,$s3,4# calculate address of# save[i]

add $t1,$t1,$s6lw $t0,0($t1)bne $t0 $s5 Exit

# save[i]

sll $t1,$s3,2bne $t0,$s5,Exitadd $s3,$s3,$s4j Loop

sll $t1,$s3,2

l i

@HC Computation 5JJ70 pg 33

Exit: Faster alternative

Page 34: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Case/Switch statement

switch (k) {

C Code

case 0: f=i+j; …. break;case 1: ...............;case 2: ;case 2: ...............;case 3: ...............;

}

1. test if k inside 0-3 address L0

Assembler Code (see book CD for real MIPS code): jump table

2. calculate address of jump table location3. fetch jump address and jump4. code for all different cases (with labels L0-L3)

address L1address L2address L3

@HC Computation 5JJ70 pg 34

4. code for all different cases (with labels L0 L3) address L3

Note: earlier we showed a different solution for a Pentium/AMD

Page 35: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

MIPS machine code for function calls$sp is one of the 32 registers. It is designated to be the stack pointer. It contains the memory address of the top of the stack of p . m m y f p f fused addresses.The stack is inverted: it grows from high to low. Before a subroutine call the arguments and registers that need to Before a subroutine call, the arguments, and registers that need to be saved are pushed on the stack:

sub $sp, $sp, 12 # adjust the stack to make room for 3 words$t1 8($ ) # th t i i t $t1 t t ksw $t1, 8($sp) # copy the argument in register $t1 to stack

sw $t0, 4($sp) # copy the argument in register $t0 to stack sw $s0, 0($sp # copy the argument in register $s0 to stack

The special instruction jal jumps to the subroutine (function), and p j p ( )at the same time stores the return address in register $raThe return address in $ra is the current value of the program counter + 4.

@HC Computation 5JJ70 pg 35

jal my_function # store PC+4 in $a, jump to routine my_functionj $ra # return from function calladd $sp, $sp, 12 # adjust the stack pointer, freeing memory.

Page 36: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Using a Stack

low address Save $s0 and $s1:m

pty subi $sp,$sp,8

sw $s0,4($sp)

$sp

em

, ( p)

sw $s1,0($sp)

fille

d Restore $s0 and $s1:

lw $s0,4($sp)high address lw $s1,0($sp)

addi $sp,$sp,8

@HC Computation 5JJ70 pg 36

Convention: $ti registers do not have to be saved and restored by callee;They are scratch registers.

Page 37: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Compiling a leaf ProcedureC code

int leaf_example (int g, int h, int i, int j){{int f;f = (g+h)-(i+j);ret rn freturn f;

}

Assembler code:

leaf_example: - save registers changed by callee

code for e pression ‘f ’- code for expression ‘f = ....’

(g is in $a0, h in $a1, etc.)

- put return value in $v0

@HC Computation 5JJ70 pg 37

- restore saved registers

- return: jr $ra

Page 38: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Compiling a non-leaf procedureFor non-leaf procedure the callee should:

save arguments registers (if used)g g ( )save return address ($ra)save callee used registers (from $s0-$s7 set) create stack space for local arrays and structures (if any)restore registers saved at beginning before returnrestore registers, saved at beginning, before return(jr $ra)

The caller should:save and restore caller life registers (from $t0-$t9)

d f ti ll ( )

@HC Computation 5JJ70 pg 38

around function call (jal label)

Page 39: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Compiling a non-leaf procedure

Factorial: n! = n* (n-1)!0! = 1

C code of ‘recursive’ factorial:

int fact (int n)int fact (int n){if (n<1)

t 1return 1;else

return (n*fact(n-1));

@HC Computation 5JJ70 pg 39

}

Page 40: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Compiling a non-leaf procedureAssembler (callee) code for ‘fact’

fact: subi $sp,$sp,8 # save return address$ 4($ ) # d i 0sw $ra,4($sp) # and arg.register a0

sw $a0,0($sp)slti $t0,$a0,1 # test for n<1beq $t0,$zero,L1 # if n>= 1 goto L1addi $v0,$zero,1 # return 1addi $sp,$sp,8 #jr $ra

L1: subi $a0,$a0,1jal fact # call fact with (n-1)lw $a0,0($sp) # restore return addresslw $ra,4($sp) # and a0 (in right order!)addi $sp,$sp,8

@HC Computation 5JJ70 pg 40

mul $v0,$a0,$v0 # return n*fact(n-1)jr $ra

Page 41: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

How does the stack look for fact(2) ?

low address

100 addi $a0,$zero,2104 jal fact$ra =

$a0 = 1$ra = ...$a0 = 0 Caller:

$sp

104 jal fact108 ....$ra = 108

$a0 = 2$ra = ...

fille

d

high address

@HC Computation 5JJ70 pg 41

Note: no caller save regs ($ti) are used

Page 42: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Beyond numbers: Characters

Characters are often represented using the ASCII standardstandardASCII = American Standard COde for Information Interchangeg

Note: value(a) - value(A) = 32value(z) - value(Z) = 32

@HC Computation 5JJ70 pg 42

Page 43: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Beyond numbers: StringsA string is a sequence of characters

Representation alternatives for “aap”:including length field: 3’a’’a’’p’separate length fieldseparate length fielddelimiter at the end: ‘a’’a’’p’0 (Choice of language C !!)

Example: procedure ‘strcpy’: void strcpy (char x[], char y[]){{int i;i=0;while ((x[i]=y[i]) != 0) /* copy and test byte */

@HC Computation 5JJ70 pg 43

while ((x[i] y[i]) ! 0) / copy and test byte /i=i+1;

}

Page 44: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

strcpy: MIPS assembly

strcpy: subi $sp,$sp,4sw $s0 0($sp)sw $s0,0($sp)add $s0,$zero,$zero # i=0

L1: add $t1,$a1,$s0 # address of y[i]lb $t2 0($t1) # load byte y[i] in $t2lb $t2,0($t1) # load byte y[i] in $t2add $t3,$a0,$s0 # similar address for x[i]sb $t2,0($t3) # store byte y[i] into x[i]addi $s0 $s0 1addi $s0,$s0,1bne $t2,$zero,L1 # if y[i]!=0 go to L1lw $s0,0($sp) # restore old $s0dd1 $ $ 4add1 $sp,$sp,4

jr $ra

@HC Computation 5JJ70 pg 44

Note: strcpy is a leaf-procedure; no saving of args and return address required

Page 45: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Arrays versus pointers

clear1 (int array[], int size)

Array version (initializing array to 0):

{

int i;

for (i=0; i<size; i=i+1)

array[i]=0;

}

clear2 (int *array, int size)

Pointer version:

{

int *p;

for (p=&array[0]; p<&array[size]; p=p+1)

@HC Computation 5JJ70 pg 45

*p=0;

}

Page 46: Translate C into MIPS assembly - TU/e · PDF fileTranslate C into MIPS assembly Henk Corporaal December 2009. Welcome ... N iil: New principle: Gd di s d sd siGood design demands a

Arrays versus pointers

Compare the assembly results in your bookN t th i f th l b dNote the size of the loop body:

Array version: 7 instructionsPointer version: 4 instructions

Pointer version much faster !

Clever compilers perform pointer conversion themselves

@HC Computation 5JJ70 pg 46