EN164: Design of Computing Systems Topic 03: Instruction Set Architecture Design

Similar documents
ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

EN164: Design of Computing Systems Lecture 09: Processor / ISA 2

Chapter 2A Instructions: Language of the Computer

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

MIPS%Assembly% E155%

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

COMPUTER ORGANIZATION AND DESIGN

Computer Organization MIPS ISA

Instructions: Language of the Computer

ECE232: Hardware Organization and Design

CENG3420 Lecture 03 Review

Chapter 2. Instructions: Language of the Computer

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Computer Architecture

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Chapter 2. Instructions: Language of the Computer. HW#1: 1.3 all, 1.4 all, 1.6.1, , , , , and Due date: one week.

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

Unsigned Binary Integers

Thomas Polzer Institut für Technische Informatik

Unsigned Binary Integers

CS3350B Computer Architecture MIPS Instruction Representation

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

Instructions: Language of the Computer

CS3350B Computer Architecture

MIPS Instruction Set Architecture (2)

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Computer Architecture

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Architecture I. Computer Systems Laboratory Sungkyunkwan University

ISA and RISCV. CASS 2018 Lavanya Ramapantulu

Lecture 4: MIPS Instruction Set

CS3350B Computer Architecture MIPS Introduction

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

CSE 141 Computer Architecture Spring Lecture 3 Instruction Set Architecute. Course Schedule. Announcements

Lecture 5: Procedure Calls

Computer Architecture. Lecture 2 : Instructions

Chapter 1. Computer Abstractions and Technology. Lesson 3: Understanding Performance

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

Chapter 6. Digital Design and Computer Architecture, 2 nd Edition. David Money Harris and Sarah L. Harris. Chapter 6 <1>

Rechnerstrukturen. Chapter 2. Instructions: Language of the Computer

Chapter 2: Instructions:

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

Architecture. Digital Computer Design

ECE 154A Introduction to. Fall 2012

EEC 581 Computer Architecture Lecture 1 Review MIPS

Chapter 2. Instruction Set Architecture (ISA)

CHW 362 : Computer Architecture & Organization

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

Chapter 2. Instruction Set. RISC vs. CISC Instruction set. The University of Adelaide, School of Computer Science 18 September 2017

CENG3420 L03: Instruction Set Architecture

All instructions have 3 operands Operand order is fixed (destination first)

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from:

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Chapter 3 MIPS Assembly Language. Ó1998 Morgan Kaufmann Publishers 1

CS222: MIPS Instruction Set

Topic Notes: MIPS Instruction Set Architecture

Computer Architecture. The Language of the Machine

Course Administration

MIPS Functions and Instruction Formats

Computer Architecture Computer Science & Engineering. Chapter 2. Instructions: Language of the Computer BK TP.HCM

Chapter 2. Baback Izadi Division of Engineering Programs

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

LECTURE 2: INSTRUCTIONS

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

Reduced Instruction Set Computer (RISC)

Chapter 2. lw $s1,100($s2) $s1 = Memory[$s2+100] sw $s1,100($s2) Memory[$s2+100] = $s1

Systems Architecture I

2.7 Supporting Procedures in hardware. Why procedures or functions? Procedure calls

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

Reduced Instruction Set Computer (RISC)

EC 413 Computer Organization

Chapter 2. Instructions:

CS 61c: Great Ideas in Computer Architecture

Chapter 2. Instructions: Language of the Computer. Jiang Jiang

Instructions: Assembly Language

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Forecast. Instructions (354 Review) Basics. Basics. Instruction set architecture (ISA) is its vocabulary. Instructions are the words of a computer

Typical Processor Execution Cycle

Chapter 6 :: Topics. Chapter 6 :: Architecture

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

Anne Bracy CS 3410 Computer Science Cornell University. See P&H Chapter: , , Appendix B

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

Transcription:

EN164: Design of Computing Systems Topic 03: Instruction Set Architecture Design Professor Sherief Reda http://scale.engin.brown.edu Electrical Sciences and Computer Engineering School of Engineering Brown University Spring 2012 1

ISA is the HW/SW interface ISA SW HW ISA choice determines: program size (& memory size) complexity of hardware (CPI and f) execution time for different applications and domains power consumption die area (cost) 2

Stored program concept (von Neumann model) The BIG Picture n Instructions represented in binary numbers, just like data n Instructions and data stored in memory n Programs can operate on programs n e.g., compilers, linkers, n Binary compatibility allows compiled programs to work on different computers n Standardized ISAs 3

Steps in execution of a program Fetch instruction @ PC Decode instruction Fetch Operands Execute instruction Store result What is instruction format / size? how is it decoded? Where are the operands located? What are their sizes? What are supported operations? How to determine the successor instruction? Update PC 4

Example of an instruction op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits add $t0, $s1, $s2 assembly language special $s1 $s2 $t0 0 add 0 17 18 8 0 32 000000 10001 10010 01000 00000 100000 00000010001100100100000000100000 2 = 02324020 16 machine language 5

ISA design choices Number, size (fixed/variable) and format of instructions Operations supported (arithmetic, logical, string, floating point, jump, etc) Operands supported (bytes, words, signed, unsigned, floating, etc) Operand storage (accumulator, stack, registers, memory) Addressing modes 6

Typical operations Data Movement Arithmetic Shift Logical Control (Jump/Branch) Subroutine Linkage Interrupt Synchronization Load (from memory) Store (to memory) memory-to-memory move register-to-register move input (from I/O device) output (to I/O device) push, pop (to/from stack) integer (binary + decimal) or FP Add, Subtract, Multiply, Divide shift left/right, rotate left/right not, and, or, set, clear unconditional, conditional call, return trap, return test & set (atomic r-m-w) search, translate [slide from M. Martin] 7

Classification of ISAs [Figure from D. Brooks -- Harvard] These ISAs give different characteristics in terms of size of programs, number of instructions and CPI. 8

Examples of ISA Instruction sequence for C = A + B for the four ISAs Stack Accumulator Register (registermemory) Register (load-store) Push A Push B Add Pop C Load A Add B Store C Load R1, A Add R1, B Store C, R1 Load R1, A Load R2, B Add R3, R1, R2 Store C, R3 Some architectures (e.g. x86) support hybrid ISAs for different classes of instructions and/or for backward compatibility. 9

What makes a good ISA? Efficiency of hardware implementation Convenience of programming / compiling Matches target applications (or generality) Compatibility and portability Four design principles for ISA 1. Simplicity favors regularity 2. Smaller is faster 3. Make the common case fast 4. Good design demands good compromises ISA design is an art! 10

Popular ISAs x86 from Intel (laptops, servers) ARM (mobile devices) MIPS (embedded devices) Power and PowerPC from IBM (servers, old Macs) and many others still spoken and dead ISAs 11

MIPS architecture: Example of Reduced Instruction Set Computer (RISC) architecture Used as the example throughout the course Stanford MIPS commercialized by MIPS Technologies (www.mips.com) Large share of embedded core market Applications in consumer electronics, network/storage equipment, cameras, printers, Typical of many modern ISAs 32 bit and 64 bit available Will implement and boot a subset MIPS processor in lab 12

Arithmetic operations High-level code a = b + c; a = b - c; MIPS assembly code # $s0 = a, $s1 = b, $s2 = c add $s0, $s1, $s2 sub $s0, $s1, $s2 add: mnemonic indicates what operation to perform $s1, $s1: source operands on which the operation is performed $s0: destination operand to which the result is written n Design Principle 1: Simplicity favors regularity n n Regularity makes implementation simpler Simplicity enables higher performance at lower cost 13

Register operands Memory is slow. Most architectures have a small set of (fast) registers MIPS has a 32 32-bit register file n n n Use for frequently accessed data n Numbered 0 to 31 n 32-bit data called a word n Written with a $ before their name Assembler names n $0 always hold value 0 n $t0, $t1,, $t9 for temporary values n $s0, $s1,, $s7 for saved variables Design Principle 2: Smaller is faster n c.f. main memory: millions of locations 14

MIPS registers Name Register Number Usage $0 0 the constant value 0 $at 1 assembler temporary $v0-$v1 2-3 procedure return values $a0-$a3 4-7 procedure arguments $t0-$t7 8-15 temporaries $s0-$s7 16-23 saved variables $t8-$t9 24-25 more temporaries $k0-$k1 26-27 OS temporaries $gp 28 global pointer $sp 29 stack pointer $fp 30 frame pointer $ra 31 procedure return address 15

Format of R-type instructions R-Type op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits Register-type 3 register operands: rs, rt: source registers rd: destination register Other fields: op: the operation code or opcode (0 for R-type instructions) funct: the function together, the opcode and function tell the computer what operation to perform shamt: the shift amount for shift instructions, otherwise it s 0 16

R-Type examples Assembly Code add $s0, $s1, $s2 sub $t0, $t3, $t5 Field Values op rs rt rd shamt funct 0 17 18 16 0 32 0 11 13 8 0 34 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits Machine Code op rs rt rd shamt funct 000000 10001 10010 10000 00000 100000 000000 01011 01101 01000 00000 100010 (0x02328020) (0x016D4022) 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits Note the order of registers in the assembly code: add rd, rs, rt 17

Memory operands Too much data to fit in only 32 registers Memory is large, but slow Commonly used variables kept in registers Using a combination of registers and memory, a program can access a large amount of data fairly quickly To apply arithmetic operations n n Load values from memory into registers Store result from register to memory 18

Word-addressable memory Each 32-bit data word has a unique address Word Address Data 00000003 4 0 F 3 0 7 8 8 00000002 0 1 E E 2 8 4 2 00000001 F 2 F 1 A C 0 7 00000000 A B C D E F 7 8 Word 3 Word 2 Word 1 Word 0 19

Reading word-addressable memory Memory reads are called loads Mnemonic: load word (lw) Example: read a word of data at memory address 1 into $s3 Memory address calculation: add the base address ($0) to the offset (1) address = ($0 + 1) = 1 Any register may be used to store the base address. $s3 holds the value 0xF2F1AC07 after the instruction completes. Assembly code lw $s3, 1($0) # read memory word 1 into $s3 Word Address Data 00000003 4 0 F 3 0 7 8 8 00000002 0 1 E E 2 8 4 2 00000001 F 2 F 1 A C 0 7 00000000 A B C D E F 7 8 Word 3 Word 2 Word 1 Word 0 20

Writing word-addressable memory Memory writes are called stores Mnemonic: store word (sw) Example: Write (store) the value held in $t4 into memory address 7 Offset can be written in decimal (default) or hexadecimal Memory address calculation: add the base address ($0) to the offset (0x7) address: ($0 + 0x7) = 7 Any register may be used to store the base address Assembly code sw $t4, 0x7($0) # write the value in $t4 # to memory word 7 Word Address Data 00000003 4 0 F 3 0 7 8 8 00000002 0 1 E E 2 8 4 2 00000001 F 2 F 1 A C 0 7 00000000 A B C D E F 7 8 Word 3 Word 2 Word 1 Word 0 21

Byte-addressable memory Each data byte has a unique address Load/store words or single bytes: load byte (lb) and store byte (sb) Each 32-bit words has 4 bytes, so the word address increments by 4 Word Address Data 0000000C 00000008 00000004 00000000 4 0 F 3 0 7 8 8 0 1 E E 2 8 4 2 F 2 F 1 A C 0 7 A B C D E F 7 8 width = 4 bytes Word 3 Word 2 Word 1 Word 0 22

Reading byte-addressable memory The address of a memory word must now be multiplied by 4. For example, the address of memory word 2 is 2 4 = 8 the address of memory word 10 is 10 4 = 40 (0x28) Load a word of data at memory address 4 into $s3. $s3 holds the value 0xF2F1AC07 after the instruction completes. MIPS is byte-addressed, not word-addressed. MIPS assembly code lw $s3, 4($0) # read word at address 4 into $s3 Word Address Data 0000000C 00000008 00000004 00000000 4 0 F 3 0 7 8 8 0 1 E E 2 8 4 2 F 2 F 1 A C 0 7 A B C D E F 7 8 Word 3 Word 2 Word 1 Word 0 width = 4 bytes 23

Writing byte-addressed memory Example: stores the value held in $t7 into memory address 0x2C (44) MIPS assembly code sw $t7, 44($0) # write $t7 into address 44 Word Address Data 0000000C 00000008 00000004 00000000 4 0 F 3 0 7 8 8 0 1 E E 2 8 4 2 F 2 F 1 A C 0 7 A B C D E F 7 8 width = 4 bytes Word 3 Word 2 Word 1 Word 0 24

Big-Endian and little-endian How to number bytes within a word? Word address is the same for big- or little-endian Little-endian: byte numbers start at the little (least significant) end Big-endian: byte numbers start at the big (most significant) end Big-Endian Little-Endian Byte Address Word Address Byte Address C D E F 8 9 A B 4 5 6 7 0 1 2 3 MSB LSB C 8 4 0 F E D C B A 9 8 7 6 5 4 3 2 1 0 MSB LSB 25

Big- and little- Endian example Suppose $t0 initially contains 0x23456789. After the following program is run on a big-endian system, what value does $s0 contain? In a little-endian system? sw $t0, 0($0) lb $s0, 1($0) Big-endian: 0x00000045 Little-endian: 0x00000067 Byte Address Data Value Big-Endian 0 1 2 3 23 45 67 89 Word Address 0 Little-Endian 3 2 1 0 23 45 67 89 MSB LSB MSB LSB Byte Address Data Value 26

Design principle 4 in action Good design demands good compromises - Multiple instruction formats allow flexibility - add, sub: use 3 register operands - lw, sw: use 2 register operands and a constant - Number of instruction formats kept small - to adhere to design principles 1 and 3 (simplicity favors regularity and smaller is faster). 27

Constant/Immediate operands lw and sw illustrate the use of constants or immediates Called immediates because they are immediately available from the instruction Immediates don t require a register or memory access. The add immediate (addi) instruction adds an immediate to a variable (held in a register). An immediate is a 16-bit two s complement number. Is subtract immediate (subi) necessary? High-level code a = a + 4; b = a 12; MIPS assembly code # $s0 = a, $s1 = b addi $s0, $s0, 4 addi $s1, $s0, -12 28

I-Type format instructions Immediate-type 3 operands: rs, rt: register operands imm: 16-bit two s complement immediate Other fields: op: the opcode Simplicity favors regularity: all instructions have opcode Operation is completely determined by the opcode I-Type op rs rt imm 6 bits 5 bits 5 bits 16 bits 29

I-Type examples Assembly Code addi $s0, $s1, 5 addi $t0, $s3, -12 lw $t2, 32($0) Field Values op rs rt imm 8 17 16 5 8 19 8-12 35 0 10 32 sw $s1, 4($t1) 43 9 17 4 6 bits 5 bits 5 bits 16 bits Note the differing order of registers in the assembly and machine codes: addi rt, rs, imm lw sw rt, imm(rs) rt, imm(rs) Machine Code op rs rt imm 001000 10001 10000 0000 0000 0000 0101 001000 10011 01000 1111 1111 1111 0100 100011 00000 01010 0000 0000 0010 0000 101011 01001 10001 0000 0000 0000 0100 6 bits 5 bits 5 bits 16 bits (0x22300005) (0x2268FFF4) (0x8C0A0020) (0xAD310004) 30

Logical operations n Instructions for bitwise manipulation Operation C Java MIPS Shift left << << sll Shift right >> >>> srl Bitwise AND & & and, andi Bitwise OR or, ori Bitwise NOT ~ ~ nor n Useful for extracting and inserting groups of bits in a word 31

Shift operations op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits n shamt: how many positions to shift n Shift left logical n Shift left and fill with 0 bits n sll by i bits multiplies by 2 i n Shift right logical n Shift right and fill with 0 bits n srl by i bits divides by 2 i (unsigned only) 32

AND operations n Useful to mask bits in a word n Select some bits, clear others to 0 and $t0, $t1, $t2 $t2 $t1 $t0 0000 0000 0000 0000 0000 1101 1100 0000 0000 0000 0000 0000 0011 1100 0000 0000 0000 0000 0000 0000 0000 1100 0000 0000 33

OR operations n Useful to include bits in a word n Set some bits to 1, leave others unchanged or $t0, $t1, $t2 $t2 $t1 $t0 0000 0000 0000 0000 0000 1101 1100 0000 0000 0000 0000 0000 0011 1100 0000 0000 0000 0000 0000 0000 0011 1101 1100 0000 34

NOT operations n Useful to invert bits in a word n Change 0 to 1, and 1 to 0 n MIPS has NOR 3-operand instruction n a NOR b == NOT ( a OR b ) nor $t0, $t1, $zero Register 0: always read as zero $t1 $t0 0000 0000 0000 0000 0011 1100 0000 0000 1111 1111 1111 1111 1100 0011 1111 1111 35

Conditional operators n Branch to a labeled instruction if a condition is true n Otherwise, continue sequentially n beq rs, rt, L1 n if (rs == rt) branch to instruction labeled L1; n bne rs, rt, L1 n if (rs!= rt) branch to instruction labeled L1; n j L1 n unconditional jump to instruction labeled L1 36

Compiling if statement n C code: if (i==j) f = g+h; else f = g-h; n f, g, in $s0, $s1, n Compiled MIPS code: bne $s3, $s4, Else add $s0, $s1, $s2 j Exit Else: sub $s0, $s1, $s2 Exit: 37

Compiling loop statements n C code: while (save[i] == k) i += 1; n i in $s3, k in $s5, address of save in $s6 n Compiled MIPS code: Loop: sll $t1, $s3, 2 add $t1, $t1, $s6 lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, 1 j Loop Exit: 38

More conditional operations n Set result to 1 if a condition is true n Otherwise, set to 0 n slt rd, rs, rt n if (rs < rt) rd = 1; else rd = 0; n slti rt, rs, constant n if (rs < constant) rt = 1; else rt = 0; n Use in combination with beq, bne slt $t0, $s1, $s2 # if ($s1 < $s2) bne $t0, $zero, L # branch to L 39

Branch instruction design n Why not blt, bge, etc? n Hardware for <,, slower than =, n Combining with branch involves more work per instruction, requiring a slower clock n All instructions penalized! n beq and bne are the common case n This is a good design compromise 40

Summary of instruction formats R-Type op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits I-Type op rs rt imm 6 bits 5 bits 5 bits 16 bits J-Type op addr 6 bits 26 bits 41

Procedure calls Procedure calling conventions: Caller: passes arguments to callee. jumps to the callee Callee: performs the procedure returns the result to caller returns to the point of call must not overwrite registers or memory needed by the caller MIPS conventions: Call procedure: jump and link (jal) Return from procedure: jump register (jr) Argument values: $a0 - $a3 Return value: $v0 -$v1 42

Procedure calls High-level code int main() { simple(); a = b + c; } void simple() { return; } MIPS assembly code 0x00400200 main: jal simple 0x00400204 add $s0, $s1, $s2... 0x00401020 simple: jr $ra void means that simple doesn t return a value. jal: jumps to simple and saves PC+4 in the return address register ($ra). In this case, $ra = 0x00400204 after jal executes. jr $ra: jumps to address in $ra, in this case 0x00400204. 43

Input arguments and return values High-level code int main() { } int y;... y = diffofsums(2, 3, 4, 5); // 4 arguments... int diffofsums(int f, int g, int h, int i) { } int result; result = (f + g) - (h + i); return result; // return value 44

Input arguments and return values MIPS assembly code MIPS conventions: Argument values: $a0 - $a3 Return value: $v0 main:... addi $a0, $0, 2 # argument 0 = 2 addi $a1, $0, 3 # argument 1 = 3 addi $a2, $0, 4 # argument 2 = 4 addi $a3, $0, 5 # argument 3 = 5 jal diffofsums # call procedure add $s0, $v0, $0 # y = returned value... # $s0 = result diffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller 45

Potential problems in procedural calling MIPS assembly code # $s0 = result diffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller diffofsums overwrote 3 registers: $t0, $t1, and $s0 diffofsums can use the stack to temporarily store registers 46

The stack Memory used to temporarily save variables Like a stack of dishes, last-in-first-out (LIFO) queue Expands: uses more memory by growing down (from higher to lower memory addresses) when more space is needed Contracts: uses less memory when the space is no longer needed Stack pointer: $sp, points to top of the stack Address Data Address Data 7FFFFFFC 12345678 $sp 7FFFFFFC 12345678 7FFFFFF8 7FFFFFF8 AABBCCDD 7FFFFFF4 7FFFFFF4 11223344 $sp 7FFFFFF0 7FFFFFF0 47

How procedures use the stack Called procedures must have no other unintended side effects. But diffofsums overwrites 3 registers: $t0, $t1, $s0 # MIPS assembly # $s0 = result diffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller 48

Storing register values on the stack # $s0 = result diffofsums: addi $sp, $sp, -12 # make space on stack # to store 3 registers sw $s0, 8($sp) # save $s0 on stack sw $t0, 4($sp) # save $t0 on stack sw $t1, 0($sp) # save $t1 on stack add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 lw $t1, 0($sp) # restore $t1 from stack lw $t0, 4($sp) # restore $t0 from stack lw $s0, 8($sp) # restore $s0 from stack addi $sp, $sp, 12 # deallocate stack space jr $ra # return to caller Address Data Address Data Address Data FC? $sp FC? FC? $sp F8 F4 F0 stack frame F8 F4 F0 $s0 $t0 $t1 $sp F8 F4 F0 (a) (b) (c) 49

Protocol for preserving registers Preserved Nonpreserved Callee-Saved Caller-Saved $s0 - $s7 $t0 - $t9 $ra $a0 - $a3 $sp $v0 - $v1 stack above $sp stack below $sp 50

Multiple procedure calls proc1: addi $sp, $sp, -4 # make space on stack sw $ra, 0($sp) # save $ra on stack jal proc2... lw $ra, 0($sp) # restore $s0 from stack addi $sp, $sp, 4 # deallocate stack space jr $ra # return to caller 51

Storing saved registers on the stack # $s0 = result diffofsums: addi $sp, $sp, -4 # make space on stack to sw $s0, 0($sp) # store one register # save $s0 on stack add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i # no need to save $t0 or $t1 sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra # restore $s0 from stack # deallocate stack space # return to caller 52

Recursive procedure calls High-level code int factorial(int n) { } if (n <= 1) else return 1; return (n * factorial(n-1)); 53

Recursive procedure calls MIPS assembly code 0x90 factorial: addi $sp, $sp, -8 # make room 0x94 sw $a0, 4($sp) # store $a0 0x98 sw $ra, 0($sp) # store $ra 0x9C addi $t0, $0, 2 0xA0 slt $t0, $a0, $t0 # a <= 1? 0xA4 beq $t0, $0, else # no: go to else 0xA8 addi $v0, $0, 1 # yes: return 1 0xAC addi $sp, $sp, 8 # restore $sp 0xB0 jr $ra # return 0xB4 else: addi $a0, $a0, -1 # n = n - 1 0xB8 jal factorial # recursive call 0xBC lw $ra, 0($sp) # restore $ra 0xC0 lw $a0, 4($sp) # restore $a0 0xC4 addi $sp, $sp, 8 # restore $sp 0xC8 mul $v0, $a0, $v0 # n * factorial(n-1) 0xCC jr $ra # return 54

Stack during recursive calls Address Data Address Data Address Data FC $sp FC $sp FC $sp $v0 = 6 F8 F4 F0 F8 F4 F0 $a0 (0x3) $ra $a0 (0x2) $sp F8 F4 F0 $a0 (0x3) $ra $a0 (0x2) $sp $a0 = 3 $v0 = 3 x 2 EC E8 E4 E0 EC E8 E4 E0 $ra (0xBC) $a0 (0x1) $ra (0xBC) $sp $sp EC E8 E4 E0 $ra (0xBC) $a0 (0x1) $ra (0xBC) $sp $sp $a0 = 2 $v0 = 2 x 1 $a0 = 1 $v0 = 1 x 1 DC DC DC 55

Procedural call summary Caller Put arguments in $a0-$a3 Save any registers that are needed ($ra, maybe $t0-t9) jal callee Restore registers Look for result in $v0 Callee Save registers that might be disturbed ($s0-$s7) Perform procedure Put result in $v0 Restore registers jr $ra 56

x86 ISA: Example of Complex Instruction Set Computer (CISC) architecture n Backward compatibility instruction set doesn t change n But they do accumulate more instructions Rank instruction Integer Average Percent total executed 1 load 22% 2 conditional branch 20% 3 compare 16% 4 store 12% 5 add 8% 6 and 6% 7 sub 5% 8 move register-register 4% 9 call 1% 10 return 1% Total 96% Simple instructions dominate instruction frequency x86 instruction set 57

x86 registers 58

x86 assembly language examples # move the 4 bytes in memory at the address contained in EBX into EAX mov eax, [ebx] # move the 4 bytes of data at address ESI+4*EBX into EDX mov edx, [esi+4*ebx] # eax = eax + ebx add eax, ebx # add ebx to memory content at address memory_location add memory_location, ebx # add 10 the data at address EAX add [eax], 10 # push content of eax into top of stack push eax # compare two registers, register memory or memory memory cmp eax, ebx cmp [eax], [ebx] # conditional jumps jl, jeq, jg, jge, jle, jne 59