CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 1 Flow Control.

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

Goal: Write Programs in Assembly
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Lecture 5: MIPS Instruction Set
Chapter 3 Introduction to the 68000
Branches Two branch instructions:
Assembly Language Programming
Control Structures in ARM Implementation of Decisions Similar to accumulator instructions One instruction sets the flags, followed by another instruction.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
CEG 320/520: Computer Organization and Assembly Language Programming1 Assembly Language Programming Machine Code Hand Assembly.
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 6 – Introduction to MIPS Data Transfer & Decisions I Pieter Abbeel’s recent.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Instruction Representation II (1) Fall 2007 Lecture 10: Instruction Representation II.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
1 Homework Reading –PAL, pp Machine Projects –MP2 due at start of Class 12 Labs –Continue labs with your assigned section.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
CEG 320/520: Computer Organization and Assembly Language Programming1 Assembly Language Programming Assembler Directives and The Symbol Table.
Chapter 5 The LC-3. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-2 Instruction Set Architecture ISA.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
ECE 265 – LECTURE 8 The M68HC11 Basic Instruction Set The remaining instructions 10/20/ ECE265.
Making Decision – Microprocessor
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
Chapter 5 The LC Instruction Set Architecture ISA = All of the programmer-visible components and operations of the computer memory organization.
Execution of an instruction
Addressing Modes1 Addressing modes are concerned with how the CPU accesses the operands used by its instructions.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
COMPUTER ORGANISATION Sri.S.A.Hariprasad Sr.Lecturer R.V.C.E Bangalore.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
Elements of Datapath for the fetch and increment The first element we need: a memory unit to store the instructions of a program and supply instructions.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
MIPS Processor.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
F453 Module 8: Low Level Languages 8.1: Use of Computer Architecture.
Lecture 6: Decision and Control CS 2011 Spring 2016, Dr. Rozier.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
CS2100 Computer Organisation
Homework Reading Labs PAL, pp
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
Decision Making.
Conditional Branches What distinguishes a computer from a simple calculator is its ability to make decisions Decisions are made using the if statement,
Microcomputer Programming
Pick up the handout on your way in!!
Week 6 The darkness, the loop of negative thoughts on repeat, clamours and interferes with the music I hear in my head. Lady Gaga.
Single-Cycle CPU DataPath.
Lecture 4: MIPS Instruction Set
Computer Science 210 Computer Organization
CSC 3210 Computer Organization and Programming
MIPS Processor.
Homework Reading Machine Projects Labs PAL, pp
Write a program to calculate x**y, given x and y.
The University of Adelaide, School of Computer Science
MIPS Assembly.
Assembly Language Programming
MIPS assembly.
Branch & Call Chapter 4 Sepehr Naimi
9/27: Lecture Topics Memory Data transfer instructions
ECE511: Digital System & Microprocessor
MIPS Processor.
An Introduction to the ARM CORTEX M0+ Instructions
MIPS instructions.
Presentation transcript:

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 1 Flow Control

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 2 Flow Control: Goals Branching –Condition code register –Branch instructions Conditional branches Unconditional branches Know how to implement: –For loops –While loops –if/then/else statements Assigned Reading: –HVZ: 3.11 Program Flow Control

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 3 Flow Control: The Condition Code Register The Condition Code Register (CCR) holds information about the result of the most recently executed arithmetic instruction. There are five bits that are set by the ALU. –N – 1 if the last result was Negative –Z – 1 if the last result was Zero –V – 1 if the last operation resulted in arithmetic overflow –C – 1 if the last operation produced a carry addition – 1 if there was a carry out from most significant bit subtraction – 1 if there was a borrow required –X – special operand for multiprecision computations. We won’t be using this bit. ALU … N Z V C …

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 4 Table C.5

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 5 Table C.4

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 6 Flow Control: Branch Instructions The mnemonics for the branch instructions assume that you are following a SUB or a CMP instruction: –BEQ (branch when equal) Z=1 SUB.W D3,D4 BEQ LOOP –when does Z=1? When [D3] and [D4] are equal! Remember that CMP and SUB compute [dest] – [src]Remember that CMP and SUB compute [dest] – [src]

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 7 CMP & Bcc instructions CMP Operations performed: [d] – [s] –Result doesn’t go anywhere! Why? Bcc represents many instructions: –BEQ – branch when equal (Z=1) –BNE – branch when not equal (Z=0) –BVS – branch when overflow set (V=1) –BPL – branch when result positive (N=0) –BRA – always branch –See table C.6

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 8 Flow Control: Conditional Branch Instructions - Bcc CMP src, dest –BEQ – Branch to TARGET if src = dest –BNE – Branch to TARGET if src != dest –BLT – Branch to TARGET if dest is less than src –BLE – Branch to TARGET if dest is less than or equal to src –BGT – Branch to TARGET if dest is greater than src –BGE – Branch to TARGET if dest is greater than or equal to src

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 9 Flow Control: Bcc You can also think of Bcc as comparing the result of the last operation to zero: MOVE.W#-3,D0; D0 is a counter, starting LEAARRAY,A0; … at the value -12 LOOPADD.W(A0)+,D1 ADDQ.W#1,D0;Add 1 to the counter BLTLOOP;Loop while result < 0 ARRAYDC.W12,4,8

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 10 Flow Control: for loops for(i=0; i<5; i++) {…} CLR.BD0 LOOP… ADDQ.B#1,D0 CMPI.B#5,D0 BLTLOOP This is easy to read, and necessary if you want to use the value of i. However, you have to get the immediate value #5 from memory repeatedly. There is a more efficient way to loop 5 times… D0 - #5

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 11 Flow Control: Fixed loops MOVEI.B#5,D0 LOOPdo something… SUBQ.B#1,D0 BNELOOP; BRA if Z=0 move on… Using a down counter. A more efficient way to loop 5 times:

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 12 Flow Control: while loops while (j < 5) {…} Test at beginning. condition: (j < 5) opposite: (j  5) MOVE.Wj,D0;get j from memory LOOPCMPI.W#5,D0 BGENEXT; exit loop if …; condition false ADDQ#1,D0 BRALOOP NEXT… jDC.W2

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 13 Flow Control: Other ways to use branch You don’t have to follow the mnemonics The best thing to do is to look at the branch condition in Table C.6 EXAMPLE: for(j=-5; j!=0; j++){…} MOVE.B#-5,D0 LOOPBEQDONE do something… ADDQ.B#1,D0 BRALOOP DONEmove on… BRA if Z=1 BRA doesn’t affect the CCR We’ll use D0 for j

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 14 Flow Control: Conditionals if (x == 5) {…} The most efficient way to code this is to skip the code {…} if the condition is not true. MOVE.Wx,D2 CMPI.W#5,D2 BNESKIP …; {…} … SKIPnext instruction…

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 15 Flow Control: If…then…else if (y > 3) { true-code } else { false-code }; MOVE.WY,D0 CMPI.W#3,D0 BLEELSE true-code BRANEXT ELSEfalse-code NEXTnext-instruction Again we test for the opposite of the if condition, and skip the true-code if necessary. At the end of the true-code, we use a BRA to avoid also executing the false code.

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 16 MEMORY $10007N $10023NUM $10045 $10068 $ $100A5 $100C12 $100E14 $1010?SUM Example – Adding n integers MOVE.W N,D1 MOVE.W #NUM,A2 CLR.W D0 LOOPADD.W (A2)+,D0 SUB.W #1,D1 BGT LOOP MOVE.W D0,SUM END ORG $ NDC.W 7 NUM DC.W 3,5,8,10,5,12,14 SUMDS1

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 17 Branches and Overflow In the the V bit is set on 2’s complement overflow for the operand size (B, W, L) –BGE (branch when greater or equal) Branch when N  V = 0 –Example: SUB.B D1, D2 (DEST – SRC) N=0 when D2  D1 What if [D1] = 1, and [D2] = –128? Can we represent –129 in an 8-bit byte in 2’s complement? ( ) The result is 127 (positive), N=0, V=1 We don’t branch, which is good since –128 < 1 !

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 18 Operation sizes and Overflow In the 68000, the V-bit is set when there is a 2’s complement overflow for the size of operand specified in the instruction! In other words, suppose D0 = $ –ADD.B #$60,D0 sets V=1 and N=1 –ADD.W #$60,D0 sets V=0 and N=0 Same thing goes for the carry bit –If a byte operation would produce a carry into bit 8, the C bit is set, and bit 8 retains its old value.

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 19 Machine Code: Branches 0110 CCCC PPPP PPPP or 0110 CCCC PPPP PPPP PPPP PPPP Displacement can be an 8-bit or 16-bit value. –Determined by assembler –Dependent on the size of the jump

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 20 Machine Code: Branches - example LOOPADD.WD2,D3 1 word SUB.WNUM, D1 3 words BGELOOP 1 word Assuming LOOP is at address 1000, then the PC contains 1008 after fetching the BGE instruction, so the offset is –10 or $F6 The entire instruction is: = $6CF6

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 21 Putting it together: Summing an array ORG $1000 CLR.W D3 ; The sum will go into D3 MOVEA.L #NUMS,A1 ; A1 -> current array element MOVE.W LEN,D2 ; D2 = number of array elements remaining LOOP ADD.W (A1)+,D3 ; Add the next element SUBQ.W #1,D2 ; Now there is one less remaining BNE LOOP ; Continue if D2 != 0 MOVE.B #EXIT,D7 ; TRAP #14 ; Exit back to the simulator ; EXIT EQU 228 ; LEN DC.W 5 ; LEN = Size of the array NUMS DC.W 123,-56,453,-1045,765 ; NUMS = the array End

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 22 Flow Control: In-Class Exercises

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 23 Did you know? You can give C a “hint” about which variables to keep in registers? register int counter; int i, j; counter = 0; for (i=0; i<100; i++) { for (j=0; j<100; j++) { counter += 3; }

CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 24 Flow Control: You Should Know… Review –Condition code register –LEA instructions –Branch instructions Know how to implement: –For loops –While loops –if/then/else statements –Unconditional branches Next Topic: –The Stack and Subroutines –HVZ: 3.13 Stacks and Subroutines