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

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

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

Architecture II. Computer Systems Laboratory Sungkyunkwan University

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

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

Control Instructions

Thomas Polzer Institut für Technische Informatik

Machine Instructions - II. Hwansoo Han

MIPS Instruction Set Architecture (2)

COMPUTER ORGANIZATION AND DESIGN

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

Chapter 2. Instructions: Language of the Computer

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

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

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

Chapter 2A Instructions: Language of the Computer

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

CS3350B Computer Architecture MIPS Procedures and Compilation

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Procedure Call Instructions

Lecture 5: Procedure Calls

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

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

Chapter 2. Baback Izadi Division of Engineering Programs

Lecture 5: Procedure Calls

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

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

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

ECE232: Hardware Organization and Design

ECE260: Fundamentals of Computer Engineering

Chapter 2. Instructions: Language of the Computer

CENG3420 Lecture 03 Review

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

Chapter 2. Instruction Set. The MIPS Instruction Set. Arithmetic Operations. Instructions: Language of the Computer

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

Computer Architecture

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

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

Computer Architecture

Rechnerstrukturen. Chapter 2. Instructions: Language of the Computer

ECE260: Fundamentals of Computer Engineering

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

Computer Architecture

Chapter 2. Instructions: Language of the Computer

Course Administration

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

Lecture 6: Assembly Programs

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

Chapter 2. Instructions: Language of the Computer

Chapter 2. Instructions: Language of the Computer

ECE331: Hardware Organization and Design

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

Instruction Set. The MIPS Instruction Set. Chapter 2

LECTURE 2: INSTRUCTIONS

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

Computer Organization MIPS ISA

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

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

CS3350B Computer Architecture MIPS Instruction Representation

1/26/2014. Previously. CSE 2021: Computer Organization. The Load/Store Family (1) Memory Organization. The Load/Store Family (2)

ECE 30 Introduction to Computer Engineering

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

CENG3420 L03: Instruction Set Architecture

EEM870 Embedded System and Experiment Lecture 4: ARM Instruction Sets

Previously. CSE 2021: Computer Organization. Memory Organization. The Load/Store Family (1) 5/26/2011. Used to transfer data to/from DRAM.

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

Reduced Instruction Set Computer (RISC)

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

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

Reduced Instruction Set Computer (RISC)

Computer Organization and Design

Chapter 2. Instructions:

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Chapter 2. Instruction Set Architecture (ISA)

CSE Lecture In Class Example Handout

ECE260: Fundamentals of Computer Engineering

Instructions: Assembly Language

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

MIPS function continued

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

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

EE 361 University of Hawaii Fall

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

Course Administration

Chapter 2: Instructions:

COE608: Computer Organization and Architecture

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

Computer Architecture. The Language of the Machine

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

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

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

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

Lecture 4: MIPS Instruction Set

CS/COE1541: Introduction to Computer Architecture

ECE 15B Computer Organization Spring 2010

MIPS%Assembly% E155%

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

MIPS Functions and Instruction Formats

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

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

Transcription:

Branch Addressing Branch instructions specify Opcode, two registers, target address Most branch targets are near branch Forward or backward op rs rt constant or address 6 bits 5 bits 5 bits 16 bits PC-relative addressing Target address = PC + offset 4 PC already incremented by 4 by this time Chapter 2 Instructions: Language of the Computer 52 Jump Addressing Jump (j and jal) targets could be anywhere in text segment Encode full address in instruction op address 6 bits 26 bits (Pseudo)Direct jump addressing Target address = PC 31 28 : (address 4) Chapter 2 Instructions: Language of the Computer 53 Target Addressing Example Loop code from earlier example Assume Loop at location 80000 Loop: sll $t1, $s3, 2 80000 0 0 19 9 4 0 add $t1, $t1, $s6 80004 0 9 22 9 0 32 lw $t0, 0($t1) 80008 35 9 8 0 bne $t0, $s5, Exit 80012 5 8 21 2 addi $s3, $s3, 1 80016 8 19 19 1 j Loop 80020 2 20000 Exit: 80024 Chapter 2 Instructions: Language of the Computer 54 Chapter 2 Instructions: Language of the Computer 1

Compiling If Statements C code: if (i==j) f = g+h; else f = g-h; f, g, in $s0, $s1, Compiled MIPS code: i bne $s3, $s4, Else add $s0, $s1, $s2 j Exit Else: sub $s0, $s1, $s2 Exit: Assembler calculates addresses j Chapter 2 Instructions: Language of the Computer 55 Compiling Loop Statements C code: while (save[i] == k) i += 1; i in $s3, k in $s5, address of save in $s6 Compiled MIPS code: Loop: sll $t1, $s3, 2 add $t1, $t1, $s6 save[i] lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, 1 j Loop Exit: Multiply i by 4 Address of save[i] Chapter 2 Instructions: Language of the Computer 56 Basic Blocks A basic block is a sequence of instructions with No embedded branches (except at end) No branch targets (except at beginning) A compiler identifies basic blocks for optimization An advanced processor can accelerate execution of basic blocks Chapter 2 Instructions: Language of the Computer 57 Chapter 2 Instructions: Language of the Computer 2

Compiling Case Statement switch (k) { case 0: h=i+j; break; /*k=0*/ case 1: h=i+h; break; /*k=1*/ case 2: h=i-j; break; /*k=2*/ Assuming three sequential words in memory starting at the address in $t4 have the addresses of the labels L0, L1, and L2 and k is in $s2 $t4 Memory L2 L1 L0 add $t1, $s2, $s2 #$t1 = 2*k add $t1, $t1, $t1 #$t1 = 4*k add $t1, $t1, $t4 #$t1 = addr of JumpT[k] lw $t0, 0($t1) #$t0 = JumpT[k] jr $t0 #jump based on $t0 L0: add $s3, $s0, $s1 #k=0 so h=i+j j Exit L1: add $s3, $s0, $s3 #k=1 so h=i+h j Exit L2: sub $s3, $s0, $s1 #k=2 so h=i-j Exit:... Chapter 2 Instructions: Language of the Computer 58 More Conditional Operations Set dest to 1 if a condition is true Otherwise, set to 0 slt rd, rs, rt if (rs < rt) rd = 1; else rd = 0; slti rt, rs, constant if (rs < constant) rt = 1; else rt = 0; Use in combination with beq, bne slt $t0, $s1, $s2 # if ($s1 < $s2) bne $t0, $zero, L # branch to L Chapter 2 Instructions: Language of the Computer 61 Branch Instruction Design Why not blt, bge, etc? Hardware for <,, slower than =, Combining with branch involves more work per instruction, requiring a slower clock All instructions penalized! beq and bne are the common case This is a good design compromise Chapter 2 Instructions: Language of the Computer 62 Chapter 2 Instructions: Language of the Computer 3

Signed vs. Unsigned Signed comparison: slt, slti Unsigned comparison: sltu, sltui Example $s0 = 1111 1111 1111 1111 1111 1111 1111 1111 $s1 = 0000 0000 0000 0000 0000 0000 0000 0001 slt $t0, $s0, $s1 # signed 1 < +1 $t0 = 1 sltu $t0, $s0, $s1 # unsigned +4,294,967,295 > +1 $t0 = 0 Chapter 2 Instructions: Language of the Computer 63 Procedure Calling Steps required 1. Place parameters in a place where the procedure can access them 2. Transfer control to procedure 3. Acquire storage (resources) for procedure 4. Perform procedure s operations 5. Place result in a place where the caller can access them. 6. Return to place of call 2.8 Supporting Procedures in Computer Hardware Chapter 2 Instructions: Language of the Computer 64 Register Usage $a0 $a3: arguments (reg s 4 7) $v0, $v1: result values (reg s 2 and 3) $t0 $t9: temporaries Can be overwritten by callee $s0 $s7: saved Must be saved/restored by callee $gp: global pointer for static data (reg 28) $sp: stack pointer (reg 29) $fp: frame pointer (reg 30) $ra: return address (reg 31) Chapter 2 Instructions: Language of the Computer 65 Chapter 2 Instructions: Language of the Computer 4

Procedure Call Instructions Procedure call: jump and link jal ProcedureLabel Address of following instruction put in $ra Jumps to target address Procedure return: jump register jr $ra Copies $ra to program counter Can also be used for computed jumps e.g., for case/switch statements Chapter 2 Instructions: Language of the Computer 66 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 Will need $t0, and $t1 in the calculation of f Chapter 2 Instructions: Language of the Computer 67 Stack The best way to store registers is a stack A stack is a first-in-last-out data structure Stack pointer points to the last element in the stack (or the first empty place). Traditionally stack grows from higher to lower addresses $sp used empty $sp used $t1 $t0 $s0 The stack The stack after pushing $t1 $t0 and $s0 empty Chapter 2 Instructions: Language of the Computer 68 Chapter 2 Instructions: Language of the Computer 5

Procedure Call int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; } leaf_example: addi $sp, $sp, -12#adjust stack to make room for 3 items sw $t1, 8($sp) # push $t1 sw $t0, 4($sp) # push $t0?? sw $s0, 0($sp) # push $s0 Save registers Chapter 2 Instructions: Language of the Computer 69 Procedure Call add $t0, $a0, $a1 #$t0 = g+h Do calculation add $t1, $a2, $a3 #$t1 = i+j sub $s0, $t0, $t1 #$s0 = (g+h)-(i+j) add $v0, $s0, $zero #put the result in $v0 lw $s0, 0($sp) #restore $s0 add $t0, 4($sp) #restore $t0 sub $t1, 8($sp) #restore $t1 addi $sp, $sp, 12 #restore $sp put result in $v0 Clean up (remove data from the stack) jr $ra #jump back to the calling routing Return control to caller Chapter 2 Instructions: Language of the Computer 70 Leaf Procedure Example MIPS code: 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 Chapter 2 Instructions: Language of the Computer 71 Chapter 2 Instructions: Language of the Computer 6

Non-Leaf Procedures Procedures that call other procedures For nested call, caller needs to save on the stack: Its return address Any arguments and temporaries needed after the call Restore from the stack after the call Chapter 2 Instructions: Language of the Computer 72 Non-Leaf Procedure Example C code: int fact (int n) { if (n < 1) return f; else return n * fact(n - 1); } Argument n in $a0 Result in $v0 Chapter 2 Instructions: Language of the Computer 73 Non-Leaf Procedure Example MIPS code: fact: addi $sp, $sp, -8 # adjust stack for 2 items sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save argument slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 addi $v0, $zero, 1 # if so, result is 1 addi $sp, $sp, 8 # pop 2 items from stack jr $ra # and return L1: addi $a0, $a0, -1 # else decrement n jal fact # recursive call lw $a0, 0($sp) # restore original n lw $ra, 4($sp) # and return address addi $sp, $sp, 8 # pop 2 items from stack mul $v0, $a0, $v0 # multiply to get result jr $ra # and return Chapter 2 Instructions: Language of the Computer 74 Chapter 2 Instructions: Language of the Computer 7

Local Data on the Stack Points to the 1 st word in the procedure frame Local data allocated by callee e.g., C automatic variables Procedure frame (activation record) Used by some compilers to manage stack storage Fixed, does not change during the function execution A stable base register to address for local memory reference Chapter 2 Instructions: Language of the Computer 75 Memory Layout Text: program code Static data: global variables e.g., static variables in C, constant arrays and strings $gp initialized to address allowing ±offsets into this segment Dynamic data: heap E.g., malloc in C, new in Java Stack: automatic storage Chapter 2 Instructions: Language of the Computer 76 Character Data Byte-encoded character sets ASCII: 128 characters 95 graphic, 33 control Latin-1: 256 characters ASCII, +96 more graphic characters Unicode: 32-bit character set Used in Java, C++ wide characters, Most of the world s alphabets, plus symbols UTF-8, UTF-16: variable-length encodings 2.9 Communicating with People Chapter 2 Instructions: Language of the Computer 77 Chapter 2 Instructions: Language of the Computer 8

String Copy Example C code (naïve): Null-terminated string void strcpy (char x[], char y[]) { int i; i = 0; while ((x[i]=y[i])!='\0') i += 1; } Addresses of x, y in $a0, $a1 i in $s0 Chapter 2 Instructions: Language of the Computer 78 String Copy Example MIPS code: strcpy: addi $sp, $sp, -4 # adjust stack for 1 item sw $s0, 0($sp) # save $s0 add $s0, $zero, $zero # i = 0 L1: add $t1, $s0, $a1 # addr of y[i] in $t1 lbu $t2, 0($t1) # $t2 = y[i] add $t3, $s0, $a0 # addr of x[i] in $t3 sb $t2, 0($t3) # x[i] = y[i] beq $t2, $zero, L2 # exit loop if y[i] == 0 addi $s0, $s0, 1 # i = i + 1 j L1 # next iteration of loop L2: lw $s0, 0($sp) # restore saved $s0 addi $sp, $sp, 4 # pop 1 item from stack jr $ra # and return Chapter 2 Instructions: Language of the Computer 79 32-bit Constants Most constants are small 16-bit immediate is sufficient For the occasional 32-bit constant lui rt, constant Copies 16-bit constant to left 16 bits of rt Clears right 16 bits of rt to 0 Load 4,000,000 in $s0 lui $s0, 61 0000 0000 0011 1101 0000 0000 0000 0000 ori $s0, $s0, 2304 0000 0000 0111 1101 0000 1001 0000 0000 Zero extended 2.10 MIPS Addressing for 32-Bit Immediates and Addresses Chapter 2 Instructions: Language of the Computer 80 Chapter 2 Instructions: Language of the Computer 9

Branching Far Away If branch target is too far to encode with 16-bit offset, assembler rewrites the code Example beq $s0,$s1, L1 bne $s0,$s1, L2 j L1 L2: Chapter 2 Instructions: Language of the Computer 81 Addressing Mode Summary Chapter 2 Instructions: Language of the Computer 82 Chapter 2 Instructions: Language of the Computer 10