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

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

CS3350B Computer Architecture MIPS Procedures and Compilation

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

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

Computer Systems Laboratory Sungkyunkwan University

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

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

LECTURE 2: INSTRUCTIONS

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

Chapter 2. Baback Izadi Division of Engineering Programs

COMPUTER ORGANIZATION AND DESIGN

COE608: Computer Organization and Architecture

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

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

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

Two processors sharing an area of memory. P1 writes, then P2 reads Data race if P1 and P2 don t synchronize. Result depends of order of accesses

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

Chapter 2. Instructions: Language of the Computer

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

Chapter 2A Instructions: Language of the Computer

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

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

Chapter 2. Instructions: Language of the Computer

EEC 581 Computer Architecture Lecture 1 Review MIPS

Communicating with People (2.8)

Computer Architecture

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

Chapter 2. Instruction Set Architecture (ISA)

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Rechnerstrukturen. Chapter 2. Instructions: Language of the Computer

ECE 486/586. Computer Architecture. Lecture # 8

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

Lecture Topics. Branch Condition Options. Branch Conditions ECE 486/586. Computer Architecture. Lecture # 8. Instruction Set Principles.

Chapter 2. Instructions: Language of the Computer

Lecture 5: Procedure Calls

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

Computer Architecture. The Language of the Machine

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

Thomas Polzer Institut für Technische Informatik

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

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

Chapter 2. Instructions: Language of the Computer

COMPUTER ORGANIZATION AND DESIGN. 5 th Edition. The Hardware/Software Interface. Chapter 2. Instructions: Language of the Computer

Architecture II. Computer Systems Laboratory Sungkyunkwan University

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

Chapter 2. Instructions: Language of the Computer

Chapter 3. Instructions:

COMPUTER ORGANIZATION AND DESIGN. 5 th Edition. The Hardware/Software Interface. Chapter 2. Instructions: Language of the Computer

CENG3420 Lecture 03 Review

Instruction Set. The MIPS Instruction Set. Chapter 2

MIPS Instruction Set Architecture (2)

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

Control Instructions

Chapter 2. Instructions: Language of the Computer. Baback Izadi ECE Department

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

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

Computer Architecture

Course Administration

ECE232: Hardware Organization and Design

CS/COE1541: Introduction to Computer Architecture

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

CS3350B Computer Architecture MIPS Introduction

Instructions: Language of the Computer. Euiseong Seo Computer Systems Laboratory Sungkyunkwan University

Machine Instructions - II. Hwansoo Han

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

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

Reduced Instruction Set Computer (RISC)

CS3350B Computer Architecture

1 5. Addressing Modes COMP2611 Fall 2015 Instruction: Language of the Computer

Instruction Set Architectures

MIPS ISA-II: Procedure Calls & Program Assembly

Assembly Language Programming. CPSC 252 Computer Organization Ellen Walker, Hiram College

Chapter 2. Instructions:

Computer Organization and Design

CS3350B Computer Architecture MIPS Instruction Representation

Chapter 2: Instructions:

Reduced Instruction Set Computer (RISC)

Character Is a byte quantity (00~FF or 0~255) ASCII (American Standard Code for Information Interchange) Page 91, Fig. 2.21

Systems Architecture I

Lecture 3: The Instruction Set Architecture (cont.)

Lecture 3: The Instruction Set Architecture (cont.)

Lecture 5: Procedure Calls

Lecture 6: Assembly Programs

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

MIPS (SPIM) Assembler Syntax

ECE260: Fundamentals of Computer Engineering

Computer Organization MIPS ISA

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Instruction Set Architecture. "Speaking with the computer"

CS 61c: Great Ideas in Computer Architecture

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

EE 361 University of Hawaii Fall

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

Review of instruction set architectures

Computer Architecture

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Lecture 3: Instruction Set Architecture

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

ECE369. Chapter 2 ECE369

Transcription:

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

Procedures A major program structuring mechanism Calling & returning from a procedure requires a protocol. The protocol is a sequence of conventions. RetVal = SomeProcedure(arg1, arg2,, argn);... Caller Callee int SomeProcedure(int arg1, char arg2,, float argn) { int LocalVar, TempVar; } OtherProcedure(par1,, parm);... return RetVal; 2

Procedure Call Requirements (Caller/Callee) Caller 1. must pass parameters to the callee. 2. must pass to the callee the return address. 3. must save whatever is volatile (i.e., registers that can be used by the callee). Callee 1. must save its own return address. 2. must provide local storage for its own use. 3. should support recursive calls. 3

Program Stack Stacks are a natural structure to allocate dynamic data for procedures (as well as call/return linkage information) in a Last-In- First-Out fashion. $sp (register 29) is automatically loaded to point to the last used slot on top of the stack. By convention, the stack grows towards lower addresses. 4

Stack MIPS stack push addi $sp, $sp, -4 sw pop lw $t0, 0($sp) $t0, 0($sp) addi $sp, $sp, 4 $sp old new high addr low addr 5

Procedure Call Mechanisms Registers are used for: passing a small number of parameters (up to 4, $a0 - $a3) Q: more than 4 parameters? Passing the return address ($ra) jal ProcedureAddress returning function values ($v0, $v1): Why two? keeping track of stack ($sp) Stack is used for: saving registers to be used by callee saving information about the caller (i.e., return address) passing extra parameters allocating local data for the called procedure 6

Procedure Call Convention 0 $0 zero constant 0 1 $at reserved for assembler 2 $v0 expression evaluation & 3 $v1 function results 4 $a0 arguments (caller saves) 5 $a1 6 $a2 7 $a3 8 $t0 temporary: caller saves... 15 $t7 16 $s0 callee saves... 23 $s7 24 $t8 temporary (cont d) 25 $t9 26 $k0 reserved for OS kernel 27 $k1 28 $gp global pointer 29 $sp stack pointer 30 $fp frame pointer 31 $ra return address (HW) 7

Register Saving Convention Preserved on Call Not preserved on Call Saved Registers: $s0-$s7 Argument Registers: $a0-$a3 Stack Pointer: $sp Return Value Regs: $v0-$v1 Frame Pointer: $fp Temporaries: $t0-$t9 Return Address: $ra Global Pointer: $gp (for static (vs. automatic) variables) Preserved registers must be saved and restored by called procedure (callee) if modified. Unpreserved registers must be saved by caller if needed after call complete. Similar convention for Floating-Point registers 8

MIPS Procedure Calls caller $ra jal loc loc: callee jr $ra What if the callee calls another procedure? 9

MIPS Procedure Call (2) caller callee & caller $ra jal loc loc: subi $sp,$sp,4 sw $ra,0($sp) jal loc2 loc2: another callee $ra lw $ra,0($sp) addi $sp,$sp, 4 jr $ra jr $ra 10

Example: Call Sequence Two arguments in $t0 and $t3 Want to save $t6 and $t7 add $a0,$t0,$zero move $a0, $t0 # first arg in $a0 (move is a pseudoinstruction) move $a1, $t3 # second arg in $a1 addi $sp, $sp, -8 # adjust stack pointer sw $t6, 4($sp) # save $t6 on stack sw $t7, 0($sp) # save $t7 on stack jal Function # call function Callee will do: Function: addi $sp, $sp, -4 # adjust stack pointer sw $ra, 0($sp) # save return address 11

Example: Return from Call Before returning the callee will put results in $v0-$v1 if needed then: lw $ra, 0($sp) # restore the return address addi $sp, $sp, 4 # adjust stack jr $ra # return The caller will restore $t6-$t7: lw $t6, 4($sp) # restore $t6 lw $t7, 0($sp) # restore $t7 addi $sp, $sp, 8 # adjust stack pointer 12

Leaf Procedure Example C code: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; } Arguments g,, j in $a0,, $a3 f in $s0 (hence, need to save $s0 on stack) Result in $v0 leaf_example: addi $sp, $sp, -4 sw $s0, 0($sp) add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0, $zero lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra Save $s0 on stack Procedure body Result Restore $s0 Return 13

Non-Leaf Procedure: Factorial Example fact: # procedure entry addi $sp, $sp, -8 # adjust sp sw $ra, 4($sp) # save ret. addr. sw $a0, 0($sp) # save arg. # procedure body slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 # if n 1, goto L1 addi $v0, $zero, 1 # return 1 addi $sp, $sp, 8 # pop 2 items jr $ra # return to after jal L1: addi $a0, $a0, -1 # if n 1: gets (n-1) jal fact # call fact w/ (n-1) lw $a0, 0($sp) # restore n lw $ra, 4($sp) # restore ret. addr. addi $sp, $sp, 8 mul $v0, $a0, $v0 # pop 2 items # return n*fact(n-1) jr $ra # return to caller int fact (int n) { if (n < 1) return (1); else return (n*fact(n-1)); } 14

Local Data on the Stack 15

Procedure Frame/Stack Frame/Activation Record Higher Addresses FP SP might change during the procedure SP Lower Addresses Arguments in reverse order Saved return address ($ra) Callee-saved registers including old FP, SP Local arrays and structures Caller-saved registers Arguments for next procedure Access arguments at fixed offset from FP. Allow variable number of argument Access local variables and saved registers at fixed (positive) offset from FP. Frame is the memory between $fp and $sp. Can be built in many different ways. We describe the most common convention on most MIPS machines. 16

MIPS Memory Layout Text: program code Static data: global variables e.g., static variables in C, constant arrays and strings $sp ox7ffffffc Stack $gp Dynamic data: heap E.g., malloc in C, new in Java Stack: automatic storage $gp ox10008000 ox10000000 Dynamic Data Static Data $pc ox00400000 Text 0 Reserved 17

Variables local valid within the function or block {} global valid within a file can be used by other files static local static: retain its value global static: protected from other files volatile int a = 0x10; void funca (void) { int a1 = 3; int b1 = 4;... return; } char b = 0x20; void funcb (void) {... return; } static int d = 0x10; void func(void) { static int a = 3; static int b = 4; a++; b++; return; } 18

Symbol Symbol function global variable static variable.text.constdata.bss.data RO ZI RW int x1 = 5; int y1[100]; int const z1[3] = {1,2,3}; char *s3 = "abc"; // in.data // in.bss // in.constdata // s3 in.data, "abc" in.constdata z2[5], z3 z3 ZI-stack ZI-heap int main(int x) { static int x2; static int y2 = 10; char z2[5]; char z3; // in.text // in.bss // in.data // stack // stack y1[100], x2 x1,s3,y2 z1, abc ZI RW RO } z3 = (char *)malloc(suzeof(char)*200); // heap main RO 19

Byte and Halfword lb $t0, 0($sp) sb $t0, 0($gp) # read byte # write byte lh $t0, 0($sp) sh $t0, 0($gp) # read halfword # write halfword while ((x[i]=y[i])!= \0 ) i++; x[]:$a0, y[]:$a1, i:$s0 L1: add $t1,$s0,$a1 # $t1 = addr. of y[i] lb $t2,0($t1) # $t2 = y[i] add $t3,$s0,$a0 # $t3 = addr. Of x[i] sb $t2,0($t3) # x[i] = y[i] beq $t2,$zero,l2 # if y[i]==0, goto L2 addi $s0,$s0,1 # i++ j L1 L2: 20

Assembly Language vs. Machine Language Assembly provides convenient symbolic representation much easier than writing down numbers e.g., destination first Machine language is the underlying reality e.g., destination 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 real instructions 21

To summarize: MIPS operands Name Example Comments $s0-$s7, $t0-$t9, $zero, Fast locations for data. In MIPS, data must be in registers to perform 32 registers $a0-$a3, $v0-$v1, $gp, arithmetic. MIPS register $zero always equals 0. Register $at is $fp, $sp, $ra, $at reserved for the assembler to handle large constants. Memory[0], Accessed only by data transfer instructions. MIPS uses byte addresses, so 2 30 memory Memory[4],..., sequential words differ by 4. Memory holds data structures, such as arrays, words Memory[4294967292] and spilled registers, such as those saved on procedure calls. MIPS assembly language Category Instruction Example Meaning Comments add 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 constants load word lw $s1, 100($s2) $s1 = Memory[$s2 + 100] Word from memory to register store 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 register store byte sb $s1, 100($s2) Memory[$s2 + 100] = $s1 Byte from register to memory load upper immediate lui $s1, 100 $s1 = 100 * 2 16 Loads constant in upper 16 bits branch on equal beq $s1, $s2, 25 if ($s1 == $s2) go to PC + 4 + 100 branch on not equal bne $s1, $s2, 25 if ($s1!= $s2) go to Conditional PC + 4 + 100 branch set on less than slt $s1, $s2, $s3 if ($s2 < $s3) $s1 = 1; else $s1 = 0 Equal test; PC-relative branch Not equal test; PC-relative 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 jump j 2500 go to 10000 Jump to target address Uncondi- jump register jr $ra go to $ra For switch, procedure return tional jump jump and link jal 2500 $ra = PC + 4; go to 10000 For procedure call 22

Translation Hierarchy Many compilers produce object modules directly Static linking 23

Alternative Architectures Design alternative: provide more powerful operations goal is to reduce number of instructions executed danger is a slower cycle time and/or a higher CPI The path toward operation complexity is thus fraught with peril. To avoid these problems, designers have moved toward simpler instructions 24

The Intel x86 ISA Evolution with backward compatibility 8080 (1974): 8-bit microprocessor, Accumulator, plus 3 index-register pairs 8086 (1978): 16-bit extension to 8080, Complex instruction set (CISC) 8087 (1980): floating-point coprocessor, Adds FP instructions and register stack 80286 (1982): 24-bit addresses, MMU, Segmented memory mapping and protection 80386 (1985): 32-bit extension (now IA-32) Additional addressing modes and operations, Paged memory and segments i486 (1989): pipelined, on-chip caches and FPU Pentium (1993): superscalar, 64-bit datapath Later versions added MMX (Multi-Media extension) instructions Pentium Pro (1995), Pentium II (1997) New microarchitecture Pentium III (1999): Added SSE (Streaming SIMD Extensions) and associated registers Pentium 4 (2001): New microarchitecture, Added SSE2 instructions AMD64 (2003): extended architecture to 64 bits EM64T Extended Memory 64 Technology (2004) AMD64 adopted by Intel (with refinements), Added SSE3 instructions Intel Core (2006): Added SSE4 instructions, virtual machine support AMD64 (2007): SSE5 instructions Advanced Vector Extension (2008): Longer SSE registers, more instructions 25

IA-32 Overview Complexity: Instructions from 1 to 17 bytes long one operand must act as both a source and destination one operand can come from memory complex addressing modes e.g., base or scaled index with 8 or 32 bit displacement Saving grace: the most frequently used instructions are not too difficult to build compilers avoid complex instructions what the 80x86 lacks in style is made up in quantity, making it beautiful from the right perspective 26

Fallacies Powerful instruction higher performance Fewer instructions required But complex instructions are hard to implement May slow down all instructions, including simple ones Compilers are good at making fast code from simple instructions Use assembly code for high performance But modern compilers are better at dealing with modern processors More lines of code more errors and less productivity 27

What is Common Case? Measure MIPS instruction executions in benchmark programs Consider making the common case fast Consider compromises Instruction class MIPS examples SPEC2006 Int SPEC2006 FP Arithmetic add, sub, addi 16% 48% Data transfer Logical Cond. Branch lw, sw, lb, lbu, lh, lhu, sb, lui and, or, nor, andi, ori, sll, srl beq, bne, slt, slti, sltiu 35% 36% 12% 4% 34% 8% Jump j, jr, jal 2% 0% 28

Summary Instruction complexity is only one variable lower instruction count vs. higher CPI / lower clock rate Design Principles: simplicity favors regularity smaller is faster good design demands compromise make the common case fast Instruction set architecture a very important abstraction indeed! MIPS: typical of RISC ISAs c.f. x86 29