Flow of Control -- Conditional branch instructions

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

Lecture 5: MIPS Instruction Set
Branches Two branch instructions:
Chapter 2 Instructions: Language of the Computer
The University of Adelaide, School of Computer Science
Comp Sci instruction encoding 1 Instruction Encoding MIPS machine language Binary encoding of instructions MIPS instruction = 32 bits Three instruction.
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 6: Logic/Shift Instructions Partially adapted from Computer Organization and Design, 4.
IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education.
Computer Organization and Design Instruction Sets Montek Singh Wed, Sep 12, 2012 Lecture 5.
CSE378 Instr. encoding.1 Instruction encoding The ISA defines –The format of an instruction (syntax) –The meaning of the instruction (semantics) Format.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
Lecture 4: MIPS Instruction Set
True Assembly Language Part I. The Assembly Process The process of translating a MAL code (an assembly language program) into machine code (a sequence.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
EET 4250 Instruction Representation & Formats Acknowledgements: Some slides and lecture notes for this course adapted from Prof. Mary Jane Penn.
MIPS Assembly Language Chapter 13 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 9 Binary Representation and Logical Operations.
CDA 3101 Spring 2016 Introduction to Computer Organization
CS Computer Organization Numbers and Instructions Dr. Stephen P. Carl.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Yaohang Li.
Instructor: Prof. Hany H Ammar, LCSEE, WVU
Computer Architecture & Operations I
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
CS 230: Computer Organization and Assembly Language
Prof. Hsien-Hsin Sean Lee
COMPUTER ARCHITECTURE & OPERATIONS I
Computer Organization and Design Instruction Sets - 2
MIPS Coding Continued.
Lecture 4: MIPS Instruction Set
Computer Organization and Design Instruction Sets - 2
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Conditional Branches What distinguishes a computer from a simple calculator is its ability to make decisions Decisions are made using the if statement,
32-bit MIPS ISA.
Computer Organization and Design Instruction Sets
ECE/CS 552: Instruction Sets – MIPS
CSCI206 - Computer Organization & Programming
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
Control Flow and Arrays
CSCI206 - Computer Organization & Programming
MPIS Instructions Functionalities of instructions Instruction format
Solutions Chapter 2.
Computer Architecture & Operations I
MIPS Instruction Encoding
ECE232: Hardware Organization and Design
MIPS Instruction Encoding
MIPS History MIPS is a computer family
Instruction encoding The ISA defines Format = Encoding
The University of Adelaide, School of Computer Science
MIPS History MIPS is a computer family
Part II Instruction-Set Architecture
/ Computer Architecture and Design
Instruction encoding The ISA defines Format = Encoding
UCSD ECE 111 Prof. Farinaz Koushanfar Fall 2018
Instruction encoding The ISA defines Format = Encoding
MIPS Assembly.
Flow of Control -- Conditional branch instructions
Instruction encoding The ISA defines Format = Encoding
MIPS History MIPS is a computer family
MIPS Coding Continued.
MIPS assembly.
CS352H Computer Systems Architecture
Reduced Instruction Set Computer (RISC)
MIPS Arithmetic and Logic Instructions
MIPS Arithmetic and Logic Instructions
Control Flow and Arrays
Presentation transcript:

Flow of Control -- Conditional branch instructions You can compare directly Equality or inequality of two registers One register with 0 (>, <, , ) and branch to a target specified as a signed displacement expressed in number of instructions (not number of bytes) from the instruction following the branch in assembly language, it is highly recommended to use labels and branch to labeled target addresses because: the computation above is too complicated some pseudo-instructions are translated into two real instructions 2/18/2019 CSE378 Instr. encoding. (ct’d)

Examples of branch instructions Beq rs,rt,target #go to target if rs = rt Beqz rs, target #go to target if rs = 0 Bne rs,rt,target #go to target if rs != rt Bltz rs, target #go to target if rs < 0 etc. but note that you cannot compare directly 2 registers for <, > … 2/18/2019 CSE378 Instr. encoding. (ct’d)

Comparisons between two registers Use an instruction to set a third register slt rd,rs,rt #rd = 1 if rs < rt else rd = 0 sltu rd,rs,rt #same but rs and rt are considered unsigned Example: Branch to Lab1 if $5 < $6 slt $10,$5,$6 #$10 = 1 if $5 < $6 otherwise $10 = 0 bnez $10,Lab1 # branch if $10 =1, i.e., $5<$6 There exist pseudo instructions to help you! blt $5,$6,Lab1 # pseudo instruction translated into # slt $1,$5,$6 # bne $1,$0,Lab1 Note the use of register 1 by the assembler and the fact that computing the address of Lab1 requires knowledge of how pseudo-instructions are expanded 2/18/2019 CSE378 Instr. encoding. (ct’d)

Unconditional transfer of control Can use “beqz $0, target” Very useful but limited range (± 32K instructions) Use of Jump instructions j target #special format for target byte address (26 bits) jr $rs #jump to address stored in rs (good for switch #statements and transfer tables) Call/return functions and procedures jal target #jump to target address; save PC of #following instruction in $31 (aka $ra) jr $31 # jump to address stored in $31 (or $ra) Also possible to use jalr rs,rd #jump to address stored in rs; rd = PC of # following instruction in rd with default rd = $31 2/18/2019 CSE378 Instr. encoding. (ct’d)

Branch addressing format Need Opcode, one or two registers, and an offset No base register since offset added to PC When using one register (i.e., compare to 0), can use the second register field to expand the opcode similar to function field for arith instructions beq $4,$5,1000 bgtz $4,1000 Opc rs rt/func target offset 2/18/2019 CSE378 Instr. encoding. (ct’d)

How to address operands The ISA specifies addressing modes MIPS, as a RISC machine has very few addressing modes register mode. Operand is in a register base or displacement or indexed mode Operand is at address “register + 16-bit signed offset” immediate mode. Operand is a constant encoded in the instruction PC-relative mode. As base but the register is the PC 2/18/2019 CSE378 Instr. encoding. (ct’d)

Some interesting instructions. Multiply Multiplying 2 32-bit numbers yields a 64-bit result Use of HI and LO registers Mult rs,rt #HI/LO = rs*rt Multu rs,rt Then need to move the HI or LO or both to regular registers mflo rd #rd = LO mfhi rd #rd = HI Once more the assembler can come to the rescue with a pseudo inst mul rd,rs,rt #generates mult and mflo #and mfhi if necessary 2/18/2019 CSE378 Instr. encoding. (ct’d)

Some interesting instructions. Divide Similarly, divide needs two registers LO gets the quotient HI gets the remainder If an operand is negative, the remainder is not specified by the MIPS ISA. 2/18/2019 CSE378 Instr. encoding. (ct’d)

CSE378 Instr. encoding. (ct’d) Logic instructions Used to manipulate bits within words, set-up masks etc. A sample of instructions and rd,rs,rt #rd=AND(rs,rt) andi rd,rs,immed or rd,rs,rt xor rd,rs,rt Immediate constant limited to 16 bits (zero-extended). If longer mask needed, use Lui. There is a pseudo-instruction NOT not rt,rs #does 1’s complement (bit by bit #complement of rs in rt) 2/18/2019 CSE378 Instr. encoding. (ct’d)

Example of use of logic instructions Create a mask of all 1’s for the low-order byte of $6. Don’t care about the other bits. ori $6,$6,0x00ff #$6[7:0] set to 1’s Clear high-order byte of register 7 but leave the 3 other bytes unchanged lui $5,0x00ff #$5 = 0x00ff0000 ori $5,$5,0xffff #$5 = 0x00ffffff and $7,$7,$5 #$7 =0x00…… (…whatever was #there before) 2/18/2019 CSE378 Instr. encoding. (ct’d)

CSE378 Instr. encoding. (ct’d) Shift instructions Logical shifts -- Zeroes are inserted sll rd,rt,shm #left shift of shm bits; inserting 0’s on #the right srl rd,rt,shm #right shift of shm bits; inserting 0’s #on the left Arithmetic shifts (useful only on the right) sra rd,rt,shm # Sign bit is inserted on the left Example let $5 = 0xff00 0000 sll $6,$5,3 #$6 = 0xf800 0000 srl $6,$5,3 #$6 = 0x1fe0 0000 sra $6,$5,3 #$6 = 0xffe0 0000 2/18/2019 CSE378 Instr. encoding. (ct’d)

Example -- High-level language int a[100]; int i; for (i=0; i<100; i++){ a[i] = 5; } 2/18/2019 CSE378 Instr. encoding. (ct’d)

Assembly language version Assume: start address of array a in r15. We use r8 to store the value of i and r9 for the value 5 add $8,$0,$0 #initialize i li $9,5 #r9 has the constant 5 Loop: mul $10,$8,4 #r10 has i in bytes #could use a shift left by 2 addu $14,$10,$15 #address of a[i] sw $9,0($14) #store 5 in a[i] addiu $8,$8,1 #increment i blt $8,100,Loop #branch if loop not finished #taking lots of liberty here! 2/18/2019 CSE378 Instr. encoding. (ct’d)

Machine language version (generated by SPIM) [0x00400020] 0x00004020 add $8, $0, $0 ; 1: add $8,$0,$0 [0x00400024] 0x34090005 ori $9, $0, 5 ; 2: li $9,5 [0x00400028] 0x34010004 ori $1, $0, 4 ; 3: mul $10,$8,4 [0x0040002c] 0x01010018 mult $8, $1 [0x00400030] 0x00005012 mflo $10 [0x00400034] 0x014f7021 addu $14, $10, $15 ; 4: addu $14,$10,$15 [0x00400038] 0xadc90000 sw $9, 0($14) ; 5: sw $9,0($14) [0x0040003c] 0x25080001 addiu $8, $8, 1 ; 6: addiu $8,$8,1 [0x00400040] 0x29010064 slti $1, $8, 100 ; 7: blt $8,100,Loop [0x00400044] 0x1420fff9 bne $1, $0, -28 ;[Loop-0x00400044] 2/18/2019 CSE378 Instr. encoding. (ct’d)