spr 2015, feb 9... elec 5200-001/6200-001 lecture 4 1 elec 5200-001/6200-001 computer architecture...

34
Spr 2015, Feb 9 . . . Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 ELEC 5200-001/6200-001 Lecture 4 Lecture 4 1 ELEC 5200-001/6200-001 ELEC 5200-001/6200-001 Computer Architecture and Computer Architecture and Design Design Spring 2015 Spring 2015 Compiling and Executing Compiling and Executing Programs (Chapter 2) Programs (Chapter 2) Vishwani D. Agrawal Vishwani D. Agrawal James J. Danaher Professor James J. Danaher Professor Department of Electrical and Computer Department of Electrical and Computer Engineering Engineering Auburn University, Auburn, AL 36849 Auburn University, Auburn, AL 36849 http://www.eng.auburn.edu/~vagrawal [email protected]

Upload: jasper-obrien

Post on 01-Jan-2016

226 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 11

ELEC 5200-001/6200-001ELEC 5200-001/6200-001Computer Architecture and DesignComputer Architecture and Design

Spring 2015Spring 2015 Compiling and Executing Programs Compiling and Executing Programs

(Chapter 2)(Chapter 2)

Vishwani D. AgrawalVishwani D. AgrawalJames J. Danaher ProfessorJames J. Danaher Professor

Department of Electrical and Computer EngineeringDepartment of Electrical and Computer EngineeringAuburn University, Auburn, AL 36849Auburn University, Auburn, AL 36849

http://www.eng.auburn.edu/[email protected]

Page 2: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 22

Software in a ComputerSoftware in a Computer

Application software

Programs userwrites and runs

Hardware

Systems software

Operating systemCompiler assembler

User

Page 3: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 33

SoftwareSoftware

Application software,a program in C:

swap (int v[ ], int k){int temp;

temp = v[k];v[k] = v[k+1];v[k+1] = temp;

}

MIPS binary machine code:

00000000101000010000000000011000000000000001100000011000001000011000110001100010000000000000000010001100111100100000000000000100101011001111001000000000000000001010110001100010000000000000010000000011111000000000000000001000

Compiler Assembler

Application software

Hardware

Systems software

See pages 149-150

MIPS compiler output,assembly language program:

swap;muli $2, $5, 4add $2, $4, $2lw $15, 0 ($2)lw $16, 4 ($2)sw $16, 0 ($2)sw $15, 4 ($2)jr $31

Page 4: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 44

Memory and RegistersMemory and Registers

Word 0

Word 1

Word 2

Word n

048

12 .

4n . . . . .

Word n+1

Register 0

Register 1

Register 2

Register 3

Register 4

Register 31

Register 5

Memory

byte addr.

jump addr.

zero

.

.

.

Page 5: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 55

Policy of Register Usage Policy of Register Usage (Conventions)(Conventions)

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

Register 1 ($at) reserved for assembler, 26-27 for operating system2004 © Morgan Kaufman Publishers

Page 6: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 66

Translating a Program to Translating a Program to Executable CodeExecutable Code

C Program(Data)

Compiler(Sys. Prog.)

Assembly LanguageCode (Data)

Assembler(Sys. Prog.)

Machine LanguageObject Code (Data)

Subroutine LibraryMachine LanguageObject Code (Data)

Linker(Sys. Prog.)

Machine LanguageExecutable Code (Data)

Loader(Sys. Prog.)

Program inComputerMemory

Page 7: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 77

CompilerCompilerSystem programSystem program

Inputs:Inputs:Programming language code, e.g., a C programProgramming language code, e.g., a C program

Instruction set (including pseudoinstructions)Instruction set (including pseudoinstructions)

Memory and register organizationMemory and register organization

Output: Assembly language codeOutput: Assembly language code

Compiler’s functionCompiler’s functionSpecific to ISA and machine organizationSpecific to ISA and machine organization

Assigns variables to registersAssigns variables to registers

Translates C into assembly languageTranslates C into assembly language

Saves and restores registers $s0 through $s7 when compiling a Saves and restores registers $s0 through $s7 when compiling a subroutine (or procedure) that uses themsubroutine (or procedure) that uses them

Page 8: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 88

Compiler is a Multi-Pass ProgramCompiler is a Multi-Pass ProgramProgramming language specific,machine-independent processing

High-level optimization(loop transformation, procedure

integration)

Global optimization(register allocation, code

optimization)

Code generator

C or JavaCode

MIPSCode

De

cre

asin

g p

rog

ram

de

pe

nd

en

ce

Incr

ea

sin

g m

ac

hin

e d

ep

end

en

ce

Intermediate representation

Page 9: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 99

Compiler Optimizes CodeCompiler Optimizes CodeMinimize number of machine instructionsMinimize number of machine instructions

Example: x[i] = x[i] + 4, memory address for x[i] is Example: x[i] = x[i] + 4, memory address for x[i] is generated only once, saved in a register, and used by lw generated only once, saved in a register, and used by lw and sw – and sw – Common subexpression eliminationCommon subexpression elimination..

Local optimization within a block of code.Local optimization within a block of code.

Global optimization across blocks.Global optimization across blocks.

Global register allocation.Global register allocation.

Strength reduction. Example: replace integer Strength reduction. Example: replace integer multiply by 2multiply by 2kk with shift left. with shift left.

Unnecessary instructions. A value not used in Unnecessary instructions. A value not used in the later part of program may not be stored in the later part of program may not be stored in memory (eliminate sw).memory (eliminate sw).

Page 10: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 1010

MIPS Compiler ExampleMIPS Compiler ExampleC code: f = (g+h) – (i+j)C code: f = (g+h) – (i+j)Compiler assigns variables f, g, h, i and j to Compiler assigns variables f, g, h, i and j to registers $s0, $s1, $s2, $s3 and $s4registers $s0, $s1, $s2, $s3 and $s4Uses two temporary registers, $t0 and $t1, to Uses two temporary registers, $t0 and $t1, to produce the following MIPS assembly code:produce the following MIPS assembly code:add $t0, $s1, $s2add $t0, $s1, $s2 # reg $t0 contains g+h# reg $t0 contains g+h

add $t1, $s3, $s4add $t1, $s3, $s4 # reg $t1 contains i+j# reg $t1 contains i+jsub $s0, $t0, $t1sub $s0, $t0, $t1 # reg $s0 # reg $s0 = $t0 – $t1= $t0 – $t1

= (g+h) – (i+j)= (g+h) – (i+j)

Page 11: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 1111

Registers $sp and $gpRegisters $sp and $gp

0

pc

$gp

$spStack

Dynamic data heap

Static data

Machine code (text)

Reserved

Temp. registers saved before procedure call

$s0-$s7 saved in stack byprocedure

Global variablesin program

Page 12: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 1212

Register $fp (Frame Pointer)Register $fp (Frame Pointer)

0

pc

$gp

$fp

Dynamic data heap

Static data

Machine code (text)

Reserved

$spProcedure frame

0

pc

$gp

$fp

Dynamic data heap

Static data

Machine code (text)

Reserved

$sp

Old procedure frame

Procedure frame

Page 13: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 1313

Compiling a Procedure CallCompiling a Procedure CallConsider a program that uses register $t0 and calls a Consider a program that uses register $t0 and calls a procedure procedure procprocAssembly code of program with procedure call:Assembly code of program with procedure call:

..

..addiaddi $sp, $sp, - 4 $sp, $sp, - 4 # adjust stack pointer# adjust stack pointerswsw $t0, 0($sp)$t0, 0($sp) # save $t0# save $t0jaljal procproc # call # call procproclwlw $t0, 0($sp)$t0, 0($sp) # restore $t0# restore $t0addiaddi $sp, $sp, 4$sp, $sp, 4 # pop 1 word off stack# pop 1 word off stack

..

..

Page 14: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 1414

Compiling a ProcedureCompiling a ProcedureConsider a procedure Consider a procedure procproc that uses register $s0 that uses register $s0Assembly code:Assembly code:proc proc ::addiaddi $sp, $sp, - 4 $sp, $sp, - 4 # adjust stack pointer# adjust stack pointerswsw $s0, 0($sp)$s0, 0($sp) # save $s0# save $s0

..Assembly code of Assembly code of procproc

..lwlw $s0, 0($sp)$s0, 0($sp) # restore $s0# restore $s0addiaddi $sp, $sp, 4$sp, $sp, 4 # pop 1 word off stack# pop 1 word off stackjrjr $ra$ra # return# return

Page 15: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 1515

Dragon Books on CompilersDragon Books on Compilers

Addison-Wesley 1986 Pearson Education 2006

Page 16: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Beyond Textbooks: Papers Beyond Textbooks: Papers Available at Course WebsiteAvailable at Course Website

M. Hall, D. Padua and K. Pingali, “Compiler Research: M. Hall, D. Padua and K. Pingali, “Compiler Research: The Next 50 Years,” The Next 50 Years,” CACMCACM, vol. 52, no. 2, pp. 60-67, , vol. 52, no. 2, pp. 60-67, Feb. 2009.Feb. 2009.

J. Larus and G. Hunt, “The Singularity System,” J. Larus and G. Hunt, “The Singularity System,” CACMCACM, , vol. 53, no. 8, pp. 72-79, Aug. 2010.vol. 53, no. 8, pp. 72-79, Aug. 2010.

S. V. Adve and H.-J. Boehm, “Memory Models: A Case S. V. Adve and H.-J. Boehm, “Memory Models: A Case for Rethinking Parallel Languages and Hardware,” for Rethinking Parallel Languages and Hardware,” CACMCACM, vol. 53, no. 8, pp. 90-101, Aug. 2010., vol. 53, no. 8, pp. 90-101, Aug. 2010.

G. L. Steele Jr., “An Interview with Frances E. Allen,” G. L. Steele Jr., “An Interview with Frances E. Allen,” CACMCACM, vol. 54, no. 1, pp. 39-45, Jan. 2011., vol. 54, no. 1, pp. 39-45, Jan. 2011.

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 1616

Page 17: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 1717

AssemblerAssemblerSystem programSystem programInput: Assembly language codeInput: Assembly language codeOutput: Machine language code (Output: Machine language code (binarybinary or or object codeobject code) contains:) contains:– Machine instructionsMachine instructions– DataData– Symbol tableSymbol table

Converts pseudoinstructions into core Converts pseudoinstructions into core instructions using register $atinstructions using register $atConverts assembly code into machine codeConverts assembly code into machine codeCreates a symbol table – labels and locations in Creates a symbol table – labels and locations in codecode

Page 18: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 1818

MIPS Assembler ExampleMIPS Assembler ExampleUse register convention:Use register convention:– Registers 16-23 are $s0 through $s7Registers 16-23 are $s0 through $s7– Registers 8-15 are $t0 through $t7Registers 8-15 are $t0 through $t7

Machine code: three 32-bit words from Machine code: three 32-bit words from assembly code of slide 10assembly code of slide 10

addadd 000000 10001 10010 01000 00000 100000000000 10001 10010 01000 00000 100000addadd 000000 10011 10100 01001 00000 100000000000 10011 10100 01001 00000 100000subsub 000000 01000 01001 10000 00000 100010000000 01000 01001 10000 00000 100010

Page 19: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 1919

Assembler and LinkerAssembler and Linker

Assembler

Assembler

Assembler

Main

Proc A

Proc B

Object file

Object file

Object file

Linker

ProgramLibrary

ExecutableFile

Assembly code

Machine code(binary)

(binary)

(binary)

Page 20: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 2020

SymbolsSymbols

Program names and labels are symbols.Program names and labels are symbols.

In executable code, a symbol is a memory In executable code, a symbol is a memory address.address.

In object code, a symbol’s final value (address) In object code, a symbol’s final value (address) has not been determined:has not been determined:

Internal symbols (statement labels) have relative addresses.Internal symbols (statement labels) have relative addresses.

External symbols (called subroutine names, library External symbols (called subroutine names, library procedures) are unknown (unresolved).procedures) are unknown (unresolved).

See example on page 143 of textbook (page See example on page 143 of textbook (page 110 in old version).110 in old version).

Page 21: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 2121

LinkerLinkerSystem program.System program.Inputs: Program and procedure libraries, Inputs: Program and procedure libraries, all in machine code.all in machine code.Output: Executable machine code.Output: Executable machine code.Linker functions:Linker functions:– Links the machine code of procedures from a Links the machine code of procedures from a

library.library.– Sets memory addresses of data variables for Sets memory addresses of data variables for

procedures.procedures.– Sets procedure addresses in the calling Sets procedure addresses in the calling

program code.program code.

Page 22: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 2222

LoaderLoaderSystem programSystem programInputs: Machine code and data from discInputs: Machine code and data from discOutput: Set up program and data in Output: Set up program and data in memorymemoryLoader functions:Loader functions:– Read executable code and data from disc to Read executable code and data from disc to

computer memorycomputer memory– Initialize registers and set stack pointer to first Initialize registers and set stack pointer to first

free locationfree location– Transfer control to a Transfer control to a start-upstart-up routine that calls routine that calls

the main routine of the programthe main routine of the program

Page 23: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 2323

Recursive and Nested ProgramsRecursive and Nested Programs

The following convention is understood and used by all calling (caller) and called (callee) programs.

Preserved Not preserved

Saved reg. $s0 - $s7 Temp. reg. $t0 - $t9Stack pointer reg. $sp Argument reg. $a0 - $a3Return addr. Reg. $ra Return value reg. $v0 - $v1Stack above the stack pointer Stack below the stack pointer

Page 24: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 2424

When Calee becomes a CallerWhen Calee becomes a Caller

Saving and restoring of saved and Saving and restoring of saved and temporary registers is done same as temporary registers is done same as described before.described before.

May reuse argument registers ($a0 - $a3); May reuse argument registers ($a0 - $a3); they are saved and restored as necessary.they are saved and restored as necessary.

Must reuse $ra; its content is saved in Must reuse $ra; its content is saved in memory and restored on return.memory and restored on return.

Page 25: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 2525

Example: Program→Callee A→Calee BExample: Program→Callee A→Calee BMain program Procedure A Procedure B

. .addi $a0, $zero, 3jal A . .

. .addi $sp, $sp, -8sw $ra, 4($sp)sw $a0, 0($sp)addi $a0, $zero, 7jal Blw $a0, 0($sp)lw $ra, 4($sp)addi $sp, $sp, 8 . .jr $ra

.

.

.jr $ra

Page 26: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 2626

Example: A Recursive ProcedureExample: A Recursive Procedure

Int fact (n){

if (n < 1) return (1);else return (n * fact (n-1));

}

This procedure returns factorial of integer n, i.e.,

n! = n × (n-1) × (n-2) × . . . × 2 × 1 = n × (n-1)!

Boundary case, 0! = 1

Page 27: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 2727

Flowchart of Flowchart of factfact

n < 1 v(n) = 1

n ≥ 0

v(n) = n * v(n-1)

Yes

No

Return v(n)

Page 28: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 2828

Example: Compute 4!Example: Compute 4! n = 4

v(4) = 4 * v(3)

v(3) = 3 * v(2)

v(2) = 2 * v(1)

v(1) = 1 * v(0)

v(0) = 1

Main program calls fact, $a0 = 4

fact calls fact, $a0 = 3

fact calls fact, $a0 = 2

fact calls fact, $a0 = 1

fact calls fact, $a0 = 0

Returns $v0 = $v0 * $a0 = 24

Returns $v0 = $v0 * $a0 = 6

Returns $v0 = $v0 * $a0 = 2

Returns $v0 = $v0 * $a0 = 1

Returns $v0 = 1

Page 29: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 2929

What to Save?What to Save?

Recursive procedure will call itself. So,Recursive procedure will call itself. So,– will reuse return address register $rawill reuse return address register $ra– will change argument register $a0will change argument register $a0

These two registers must be saved.These two registers must be saved.

Page 30: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 3030

Saved Registers for 4! ExampleSaved Registers for 4! Example

Initial $sp

$sp

fact calls fact, $a0 = 3

fact calls fact, $a0 = 2

fact calls fact, $a0 = 1

fact calls fact, $a0 = 0

Return address within main programArgument register, $a0 = 4Return address within factArgument register, $a0 = 3Return address within factArgument register, $a0 = 2Return address within factArgument register, $a0 = 1

}

}

}

}

Page 31: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 3131

Assembly Code for Assembly Code for factfact

fact:addi $sp, $sp, -8 # adjust stack for two itemssw $ra, 4($sp) # save return address of callersw $a0, 0($sp) # save caller supplied argument n

slti $t0, $a0, 1 # $t0 = 1, if n ≤ 1, i.e., n = 0beq $t0, $zero, L1 # go to L1, if n ≥ 1

addi $v0, $zero, 1 # return $v0 = 1 to caller

addi $sp, $sp, 8 # no need to restore registers, sincenone was changed, but mustrestore stack pointer

jr $ra # return to caller instruction after jal

Page 32: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 3232

Assembly Code ContinuedAssembly Code Continued

L1: addi $a0, $a0, -1 # set $a0 to n-1jal fact # call fact with argument n-1

lw $a0, 0($sp) # on return from fact, restore nlw $ra, 4($sp) # restore return addressaddi $sp, $sp, 8 # adjust stack pointer

mul $v0, $a0, $v0 # n! = n × (n-1)!

jr $ra # return to caller

Page 33: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 3333

Execution of Execution of factfact for n ≥ 1 for n ≥ 1

Call Caller #a0 $ra Returned $v0Sequence

1 Main Program n PC+4 n×(n-1)!2 fact n-1 L1+8 (n-1)×(n-2)!3 fact n-2 L1+8 (n-2)×(n-3)!

fact .fact .fact .

n-2 fact 3 L1+8 3×2 = 6n-1 fact 2 L1+8 2×1 = 2n fact 1 L1+8 1×1 = 1n+1 fact 0 L1+8 1

Page 34: Spr 2015, Feb 9... ELEC 5200-001/6200-001 Lecture 4 1 ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2015 Compiling and Executing Programs

Spr 2015, Feb 9 . . .Spr 2015, Feb 9 . . . ELEC 5200-001/6200-001 Lecture 4ELEC 5200-001/6200-001 Lecture 4 3434

SummarySummary

A user’s program is processed by several A user’s program is processed by several system programs:system programs:– CompilerCompiler– AssemblerAssembler– LinkerLinker– LoaderLoader– Start-up routine: begins program execution Start-up routine: begins program execution

and at the end of the program issues an and at the end of the program issues an exitexit system call.system call.