Lecture 4 - Part 1
Electrical Engineering 370 with Mahlke/narayanasamy at University of Michigan - Ann Arbor
About this note
By: Anonymous
Textbook:
Computer Organization and Design: The Hardware/Software Interface. Third Edition, Revised
Created: 2008-05-30
File Size: 31 page(s)
Views: 2
Textbook:
Computer Organization and Design: The Hardware/Software Interface. Third Edition, RevisedCreated: 2008-05-30
File Size: 31 page(s)
Views: 2
About StudyBlue
STUDYBLUE makes things that make you better at school.
Things like online flashcards with photos and audio.
Things like personalized quizzes and friendly reminders about when (and what) to study next.
Think of it as a digital backpack™: access to all of your study materials online and on your phone.
STUDYBLUE exists to make studying efficient and effective for every student, for free. Join us.
“I have been getting MUCH better grades on all my tests for school. Flash cards, notes, and quizzes are great on here. Thanks!”
Kathy
Kathy
Sign up (free) to study this.
© Mahlke & Narayanasamy, 2008 The material in this presentation cannot be copied in any form without our written permission Prof. Scott Mahlke & Prof. Satish Narayanasamy EECS 370 ? Introduction to Computer Organization ? Winter 2008 EECS Department University of Michigan in Ann Arbor, USA 4. Instruction Set Architecture ? from C to assembly ? Part 1 EECS 370: Introduction to Computer Organization 2/31 The University of Michigan © 2008 From Last Time - MIPS Memory Instructions ? Load instructions ? lb \\ load byte signed (load 8 bits, sign extend) ? lbu \\ load byte unsigned (load 8 bits, zero extend) ? lh \\ load halfword (load 16 bits, sign extend) ? lhu \\ load halfword unsigned (load 16 bits, zero extend) ? lw \\ load word (load 32 bits, no extension) ? Store instructions (No sign/zero extension for stores) ? sb $3, 1000($4) \\ store 8 LSBs of $3 to M[$4+1000] ? sh $3, 1000($4) \\ store 16 LSBs of $3 to M[$4+1000] ? sw $3, 1000($4) \\ store all 32 bits of $3 to M[$4+1000] EECS 370: Introduction to Computer Organization 3/31 The University of Michigan © 2008 Big Endian vs Little Endian ? Endianness: ordering of bytes within a word ? Little - increasing numeric significance with increasing memory addresses ? Big ? The opposite, most significant byte first ? MIPS is Big endian EECS 370: Introduction to Computer Organization 4/31 The University of Michigan © 2008 From Last Time - Class Problem 3 What is the final state of memory once you execute the following instruction sequence? lhu $3, 100($0) lb $4, 102($0) sw $3, 100($0) sh $4, 102($0) 0xFF 0xFF 0x77 0xFF 100 101 102 103 $3 $4 register file Memory (each location is 1 byte) EECS 370: Introduction to Computer Organization 5/31 The University of Michigan © 2008 Instruction Set Architecture (ISA) Design Lectures ? Lecture 2: Storage types and addressing modes ? Lecture 3: LC-2K and MIPS architecture ? Lecture 4 (2 parts): Converting C to assembly: ? Data structures ? Branch instructions ? Calling functions / passing arguments ? Caller/callee save registers ? Lecture 5: Translation software; libraries, VMs EECS 370: Introduction to Computer Organization 6/31 The University of Michigan © 2008 Converting C to Assembly ? Memory layout ?memory addresses ? Branches ? Procedure calls ? Expression trees ? Register allocation EECS 370: Introduction to Computer Organization 7/31 The University of Michigan © 2008 Converting C to assembly ? example 1 Write MIPS Assembly Code for the following C Expression: C: a = b + names[ i ]; Assume that a is in $1, b is in $2, i is in $3, and the array names starts at address 1000 and holds 32-bit integers. lw $4, 1000 ($5) // load names[i] add $1, $2, $4 // calculate b + names[i] multi $5, $3, 4 // calculate array offset D A T A L A Y O U T EECS 370: Introduction to Computer Organization 8/31 The University of Michigan © 2008 Converting C to assembly ? example 2 Write MIPS Assembly Code for the following C Expression: class { int a; char b, c; } y; y.a = y.b + y.c; Assume that a pointer to y is in $1. lb $3, 5 ($1) // load y.c add $4, $2, $3 // calculate y.b+y.c lb $2, 4 ($1) // load y.b sw $4, 0 ($1) // store y.a D A T A L A Y O U T EECS 370: Introduction to Computer Organization 9/31 The University of Michigan © 2008 Calculating Load/Store Addresses for Variables short a[100]; char b; int c; double d; short e; struct { char f; int g[1]; char h; } i; Problem: Assume data memory starts at address 100, calculate the total amount of memory needed. a = 2 bytes * 100 = 200 b = 1 byte c = 4 bytes d = 8 bytes e = 2 bytes i = 1 + 4 + 1 = 6 bytes total = 221, right or wrong? D A T A L A Y O U T EECS 370: Introduction to Computer Organization 10/31 The University of Michigan © 2008 Memory layout of variables ? Cannot arbitrarily pack variables into memory ?Need to worry about alignment ? ?Golden? rule ? Address of a variable is aligned based on the size of the variable ? char is byte aligned (any addr is fine) ? short is half-word aligned (LSB of addr must be 0) ? int is word aligned (2 LSBs of addr must be 0) D A T A L A Y O U T EECS 370: Introduction to Computer Organization 11/31 The University of Michigan © 2008 Structure alignment ? Each field is laid out in the order it is declared using the Golden Rule for aligning ? Identify largest field ? Starting address of overall struct is aligned based on the largest field ? Size of overall struct is a multiple of the largest field ? Reason for this is so can have an array of structs D A T A L A Y O U T EECS 370: Introduction to Computer Organization 12/31 The University of Michigan © 2008 Structure Example struct { char w; int x[3] char y; short z; } The largest field is int (4 bytes), hence: struct size is multiple of 4 struct?s starting addr is word aligned Assume struct starts at location 1000, char w ?1000 x[0] ?1004-1007, x[1] ?1008 ? 1011, x[2] ?1012 ? 1015 char y ?1016 short z ?1018 ? 1019 Total size = 20 bytes! D A T A L A Y O U T EECS 370: Introduction to Computer Organization 13/31 The University of Michigan © 2008 Earlier Example ? 2 nd Try short a[100]; ?200 bytes ?100-299 char b; ? 1 byte ?300-300 int c; ? 4 bytes ? 304-307 double d; ?8 bytes ? 312-319 short e; ?2 bytes ?320-321 struct { ?largest field is 4 bytes ?start at 324 char f; ?1 byte ?324-324 int g[1]; ?4 bytes ?328 - 331 char h; ? 1 byte ?332-332 } i; ?struct size is 12 bytes, spanning 324 ? 335 236 bytes total!! Assume data memory starts at address 100 D A T A L A Y O U T EECS 370: Introduction to Computer Organization 14/31 The University of Michigan © 2008 Class Problem 1 ? How much memory is required for the following data, assuming that the data starts at address 200? int a; struct {double b, char c, int d} e; char *f; short g[20]; D A T A L A Y O U T EECS 370: Introduction to Computer Organization 15/31 The University of Michigan © 2008 MIPS Sequencing Instructions ? Sequencing instructions change the flow of instructions that are executed. ? This is achieved by modifying the program counter. ? Conditional branches ? If (condition_test) goto target_address - condition_test compares two operands (registers) - target_address is a 16-bit displacement on current PC+4 ? beq $x, $y, val - if ($x == $y) then PC = PC + 4 + val else PC = PC + 4 B R A N C H EECS 370: Introduction to Computer Organization 16/31 The University of Michigan © 2008 Setting the Displacement Field ? Target_address is a 16-bit displacement on current PC+4 ? Target = PC + 4 + displacement - beq $4, $0, 8 // branch 3 instr ahead if $4 = = 0 - beq $4, $0, -8 // branch 1 instruction back if $4 == 0 - beq $4, $0, -4 // Infinite loop if $4 == 0 add sub mult beq lw sw sll Example code sequence B R A N C H EECS 370: Introduction to Computer Organization 17/31 The University of Michigan © 2008 Other branching instructions ? bne $2, $3, offset // branch to offset+PC+4 if $2 ? $3 ? blt $2, $3, offset // branch to offset+PC+4 if $2 < $3 ? bge $2, $3, offset // pseudo-instruction $2 ? $3 ? j target // jump to target (pseudo-direct addressing mode) // uses a 26-bit target address concatenated to top 6 // bits of PC ? jr $3 // jump to address in $3 -- when is this useful? ? jal target // put PC+4 into register $31 and jump to target address B R A N C H EECS 370: Introduction to Computer Organization 18/31 The University of Michigan © 2008 Branch - example Convert the following C code into MIPS assembly (assume x is in $1, y in $2): int x, y; if (x == y) x++; else y++; ? bne $1, $2, 8 addi $1, $1, 1 beq $0, $0, 4 addi $2, $2, 1 Without Labels bne $1, $2, L1 addi $1, $1, 1 beq $0, $0, L2 L1: addi $2, $2, 1 L2: ? Using Labels Assemblers must deal with labels and assign displacements (L1) (L2) B R A N C H EECS 370: Introduction to Computer Organization 19/31 The University of Michigan © 2008 Loop - example // assume all variables are integers // i is in $1, start of a is 500, sum is in $2 for (i=0 ; i < 100 ; i++) { if (a[i] > 0) { sum += a[i]; } } add $1, $0, $0 addi $4, $0, 400 Loop1: bge $1, $4, endLoop lw $5, 500($1) ble $5, $0, endIf add $2, $2, $5 endIf: addi $1, $1, 4 j Loop1 endLoop: B R A N C H EECS 370: Introduction to Computer Organization 20/31 The University of Michigan © 2008 Converting function calls to assembly code C: printf(?hello world\n?); ? Need to pass parameters to the called function (printf) ? Need to save return address of caller ? Need to save register values ? Need to jump to printf ? Need to get return value (if used) ? Restore register values Execute instructions for printf() Jump to to return address F U N C T I O N C A L L S EECS 370: Introduction to Computer Organization 21/31 The University of Michigan © 2008 Task 1: Passing parameters ? Where should you put all of the parameters? ? Registers? - Fast access but few in number and wrong size for some objects ? Memory? - Good general solution but where? ? MIPS answer: ? Registers and memory. - Put the first few parameters in registers (if they fit) ($4 - $7) - Put the rest in memory on the call stack ? Example: add $4, $0, 1000 // put address of char array ?hello world? in $4 F U N C T I O N C A L L S EECS 370: Introduction to Computer Organization 22/31 The University of Michigan © 2008 Call stack ? MIPS conventions (and most other processors) allocate a region of memory for the call stack ? This memory is used to manage all the storage requirements to simulate function call semantics - Parameters (that were not passed through registers) - Local variables - Temporary storage (when you run out of registers and need somewhere to save a value) - Return address - Etc. ? Sections of memory on the call stack [a.k.a. stack frames] are allocated when you make a function call, and de-allocated when you return from a function. F U N C T I O N C A L L S EECS 370: Introduction to Computer Organization 23/31 The University of Michigan © 2008 MIPS Memory Map Text (instructions) Static data stack heap 7fff ffff 1000 0000 1000 8000 0040 0000 Stack: starts at 0x7fff ffff and grows down to lower addresses. Bottom of the stack resides in the $sp register Heap: starts at 0x1000 8000 and grows up to higher addresses. Allocation done explicitly with malloc(). Deallocation with free(). Runtime error if no free mem before running into $sp address. Static: starts at 0x1000 0000. Holds all global variables and those locals explicitly declared as ?static?. Text: starts at 0x0040 0000. Holds all instructions in the program (except for Dynamically linked library routines DLLs) frames F U N C T I O N C A L L S EECS 370: Introduction to Computer Organization 24/31 The University of Michigan © 2008 The MIPS Stack Frame Return Address Incoming parameters Spilled Registers Local Vars Outgoing parameters $SP $FP static text stack heap Frame $SP $FP F U N C T I O N C A L L S EECS 370: Introduction to Computer Organization 25/31 The University of Michigan © 2008 Allocating space to local variables ? Local variables (by default) are created when you enter a function, and disappear when you leave ? Technical terminology: local variables are placed in the automatic storage class (as opposed to the static storage class used for globals). ? Automatics are allocated on the call stack ? How? by incrementing (or decrementing) the pointer to the top of the call stack ? sub $sp, $sp, 12 // allocate space for 3 integer locals add $sp, $sp, 12 // de-allocate space for locals F U N C T I O N C A L L S EECS 370: Introduction to Computer Organization 26/31 The University of Michigan © 2008 The stack grows as functions are called void foo() { int x, y[2]; bar(x); } void bar(int x) { int a[3]; printf(); } foo?s stack frame inside foo foo calls bar foo?s stack frame bar?s stack frame bar calls printf foo?s stack frame bar?s stack frame printf?s stack frame F U N C T I O N C A L L S EECS 370: Introduction to Computer Organization 27/31 The University of Michigan © 2008 The stack shrinks as functions return void foo() { int x, y[2]; bar(x); } void bar(int x) { int a[3]; printf(); } foo?s stack frame bar returns printf returns foo?s stack frame bar?s stack frame F U N C T I O N C A L L S EECS 370: Introduction to Computer Organization 28/31 The University of Michigan © 2008 Stack frame contents x return addr to main y[0] spilled regs in foo y[1] inside foo ? foo?s stack frame void foo() { int x, y[2]; bar(x); } void bar(int x) { int a[3]; printf(); } F U N C T I O N C A L L S EECS 370: Introduction to Computer Organization 29/31 The University of Michigan © 2008 Stack frame contents (2) x return addr to foo a[0] a[1] a[2] spilled regs in bar foo?s f rame bar?s frame void foo() { int x, y[2]; bar(x); } void bar(int x) { int a[3]; printf(); } x return addr to main y[0] spilled regs in foo y[1] foo calls bar F U N C T I O N C A L L S EECS 370: Introduction to Computer Organization 30/31 The University of Michigan © 2008 Stack frame contents (3) printf local vars return addr to bar printf?s frame void foo() { int x, y[2]; bar(x); } void bar(int x) { int a[3]; printf(); } x return addr to foo a[0] a[1] a[2] spilled regs in bar foo?s f rame bar?s frame x return addr to main y[0] spilled regs in foo y[1] bar calls printf F U N C T I O N C A L L S EECS 370: Introduction to Computer Organization 31/31 The University of Michigan © 2008 Assigning variables to memory spaces int w; void foo(int x) { static int y[4]; char *p; p = malloc(10); ? printf(?%s\n?, p); } static text stack heap w goes in static, as it?s a global x goes on the stack, as it?s a parameter y goes in static, 1 copy of this!! p goes on the stack allocate 10 bytes on heap, ptr set to the address string goes in static, pointer to string on stack, p goes on stack Gary Tyson 370 lecture 4 MIPS ISA concl, vars in C, function calls (begin)
Back
Next
About this note
By: Anonymous
Textbook:
Computer Organization and Design: The Hardware/Software Interface. Third Edition, Revised
Created: 2008-05-30
File Size: 31 page(s)
Views: 2
Textbook:
Computer Organization and Design: The Hardware/Software Interface. Third Edition, RevisedCreated: 2008-05-30
File Size: 31 page(s)
Views: 2
About StudyBlue
STUDYBLUE makes things that make you better at school.
Things like online flashcards with photos and audio.
Things like personalized quizzes and friendly reminders about when (and what) to study next.
Think of it as a digital backpack™: access to all of your study materials online and on your phone.
STUDYBLUE exists to make studying efficient and effective for every student, for free. Join us.
“I have been getting MUCH better grades on all my tests for school. Flash cards, notes, and quizzes are great on here. Thanks!”
Kathy
Kathy