ECE 3430 – Intro to Microcomputer Systems

Slides:



Advertisements
Similar presentations
ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS
Advertisements

ACOE2511 Assembly Language Arithmetic and Logic Instructions.
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
CS2422 Assembly Language & System Programming October 17, 2006.
Flow Control Instructions
1 Homework Reading –PAL, pp Machine Projects –MP2 due at start of Class 12 Labs –Continue labs with your assigned section.
Conditional Processing If … then … else While … do; Repeat … until.
Chapter 5 The LC-3. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-2 Instruction Set Architecture ISA.
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
Dr. José M. Reyes Álamo 1.  Review: ◦ Statement Labels ◦ Unconditional Jumps ◦ Conditional Jumps.
Making Decision – Microprocessor
Introduction to Computer Engineering ECE/CS 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin.
1 ICS 51 Introductory Computer Organization Fall 2009.
CHAPTER 6 ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS.
Assembly Language Programming of 8085 BY Prof. U. V. THETE Dept. of Computer Science YMA.
ECE 447 Fall 2009 Lecture 4: TI MSP430 Architecture and Instruction Set.
2/20/2016CAP 2211 Flow Control Instructions. 2/20/2016CAP 2212 Transfer of Control Flow control instructions are used to control the flow of a program.
Comparison Instructions Test instruction –Performs an implied AND operation between each of the bits in 2 operands. Neither operand is modified. (Flags.
Jumps, Loops and Branching. Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in.
K.K. Leung Fall 2008Introductory Pentium Programming1 Pentium Architecture: Introductory Programming Kin K. Leung
Unit 1 Instruction set M.Brindha AP/EIE
ECE 3430 – Intro to Microcomputer Systems
Status Register Status = system byte (supervisor only) + user byte = system status + condition code register usually, it is not important to know.
ECE 382 Lesson 3 ECE 382Website:
ECE 3430 – Intro to Microcomputer Systems
Control Unit Lecture 6.
ECE 382 Lesson 6 Lesson Outline Status Register Flow of Control
Assembly Language Programming of 8085
ECE 3430 – Intro to Microcomputer Systems
Microprocessor T. Y. B. Sc..
Homework Reading Labs PAL, pp
ECE 3430 – Intro to Microcomputer Systems
3.Instruction Set of 8085 Consists of 74 operation codes, e.g. MOV
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
Morgan Kaufmann Publishers Computer Organization and Assembly Language
More on logical instruction and
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
Microprocessor and Assembly Language
Processor Processor characterized by register set (state variables)
Data Processing Instructions
Computer Science 210 Computer Organization
Chapter 5 The LC-3.
SCHOOL OF ELECTRONICS ENGINEERING Electronics and Communication
LC-3 Details and Examples
Flags Register & Jump Instruction
Computer Science 210 Computer Organization
Introduction to Assembly Chapter 2
ECE 3430 – Intro to Microcomputer Systems
Computer Science 210 Computer Organization
STACK and Stack Pointer
Introduction to Computer Engineering
ECE 3430 – Intro to Microcomputer Systems
And conditional branching
Program Logic and Control
Program Logic and Control
Homework Reading Machine Projects Labs PAL, pp
Introduction to Assembly Chapter 2
Flow Control Instructions
ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS
Assembly Language for Intel 8086 Jump Condition
Chapter 7 –Program Logic and Control
Carnegie Mellon Ithaca College
Chapter 8: Instruction Set 8086 CPU Architecture
Chapter 7 –Program Logic and Control
Branch & Call Chapter 4 Sepehr Naimi
An Introduction to the ARM CORTEX M0+ Instructions
Presentation transcript:

ECE 3430 – Intro to Microcomputer Systems ECE 3430 – Introduction to Microcomputer Systems University of Colorado at Colorado Springs Lecture #10 Agenda Today 1) Jump Instructions/Conditional Execution 2) Status register flags: N,Z,V,C 3) Compare Instructions Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014

Jump Instructions We need a way to alter the flow of program execution when specific conditions exist. This allows our programs to dynamically adjust to input. The Status Register provides the status of the CPU. V = Overflow N = Negative Result Z = Zero Result C = Carry START <op> <loop op> <cond> x == y? NO YES Only these flags affect behavior of conditional jump instructions <op> END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014

ECE 3430 – Intro to Microcomputer Systems Jump Instructions SR[8,2:0] = {V,N,Z,C} - These flags are altered upon execution of an instruction (check instruction set to see which flags). - We can use a conditional jump instruction depending on these flags to set the new value of the PC. - A jump occurs when the PC is set to different place in the code (besides the next sequential instruction). - Jumps in the MSP430 always use 10-bit relative addressing. This gives a range of –512 to +511 words relative to where the PC is starting. In other words, the PC can be changed to a value within –512 to +511 words of the current PC location. - Labels provide an easy way to mark the destination of a jump. This way, the assembler can calculate the jump instruction’s operand value for you! - Unconditional Jump – “jmp” is an instruction that will always jump when executed regardless of what bits are set in the SR. “jmp” is the only unconditional jump instructions in MSP430 instruction set. - Conditional Jump – When executed, it will check the SR and evaluate a logical condition. If the jump condition is true, it will set the PC to the new address location. If the jump condition is false, it will increment the PC by 2 as in normal operation. All jump instructions other than “jmp” are conditional. Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014

ECE 3430 – Intro to Microcomputer Systems Conditional Jumps Carry Flag Jumps jnc = jump if carry clear, C=0 jc = jump if carry set, C=1 jhs = jump if higher or same, C=1 jlo = jump if lower, C=0 ORG 0xC000 mov.b #0xF0,R4 Loop: add.b #1,R4 jnc Loop ; C = 0  jump ; C = 1  no jump mov.b R4,0x0200 END START alias alias R4 = 0xF0 R4 = R4 +1 SUM > 255? NO YES M(0x0200) = R4 END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014

ECE 3430 – Intro to Microcomputer Systems Conditional Jumps Carry Flag Jumps How many times did this loop execute? Loop1  R4=0xF1 Loop2  R4=0xF2 Loop3  R4=0xF3 : : Loop15  R4=0xFF Loop16  R4=0x00, C=1, No Jump ANSWER = 16 times START R4 = 0xF0 R4 = R4 +1 SUM > 255? NO YES M(0x0200) = R4 END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014

ECE 3430 – Intro to Microcomputer Systems Conditional Jumps Carry Flag Jumps – Add M(0x0200) and M(0x0201), 8-bit - If the result is =< 255, set P1.5 (clear all other bits in port 1) and END - If the result is > 255, set P1.4 (clear all other bits in port 1) and END ORG 0xC000 MAIN: mov.b 0x0200, R4 add.b 0x0201, R4 jc Error ; C = 1 = branch ; C = 0 = no branch bis.b #00100000b,&P1OUT jmp Finish Error: bis.b #00010000b,&P1OUT Finish: jmp Finish START M(0x0200) + M(0x0201) SUM > 255? YES P1.4 = 1 NO P1.5 = 1 END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014

ECE 3430 – Intro to Microcomputer Systems Conditional Jumps Carry Flag Branches – What does the machine code look like? Addr: IC: Operands: C000 4054 41FE mov.b 0x0200,R4 C004 5054 41FB add.b 0x0201,R4 C008 2C04 jc Error C00A D0F2 0020 0021 bis.b #00100000b,&P1OUT C00E 3C03 jmp Finish C010 D0F2 0010 0021 Error: bis.b #00010000b,&P1OUT C014 3FFF Finish: jmp Finish Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014

ECE 3430 – Intro to Microcomputer Systems Conditional Jumps How long does it take to execute if the branch IS taken? How long does it take to execute if the branch IS NOT What is the average execution time? -> In the previous example, the time is the same whether the branch is taken or not. Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014

ECE 3430 – Intro to Microcomputer Systems Conditional Jumps Zero Flag Branches jeq/jz = jump if equal to zero, Z=1 jne/jnz = jump if NOT equal to zero, Z=0 Ex) loop 10 times ORG 0xC000 mov.b #10,R4 Loop: dec.b R4 jne Loop ; Z = 0 = jump ; Z = 1 = no jump END START count = 10 count = count - 1 count = 0? NO YES END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014

ECE 3430 – Intro to Microcomputer Systems Conditional Jumps Zero Flag Branches How many loops? Loop1  A=9 Loop2  A=8 : Loop10  A=0 (Z=1, jump NOT taken) How long to execute? Loop  dec.b R4  2 cycles jne  2 cycles (4 cycles)(10 loops) = 40 cycles + mov.b #10,R4  2 cycles TOTAL = (42 cycles)(<period of MCLK>) = <execution time> START count = 10 count = count - 1 count = 0? NO YES END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014

ECE 3430 – Intro to Microcomputer Systems Conditional Jumps Negative Flag Branches jn = jump if negative, N=1 (jump if minus) Can be used during subtraction to indicate the value was negative. Ex) M(0x0020) – M(0x0021) if negative or zero, END if positive, set P1.4 (clear all other bits on port 1) ORG 0xC000 mov.b 0x0200, R4 sub.b 0x0201, R4 jn DONE mov.b #00010000b, &P1OUT DONE: jmp $ ;  $ is a quick way to jump back to same line (trap PC) END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014

ECE 3430 – Intro to Microcomputer Systems Conditional Jumps Two’s Complement Overflow Flag Jumps jge = jump if greater than or equal to ; N  V = 0 jl = jump if less than ; N  V = 1 None that test only the V flag! Notice that a logic equation is evaluated for correctness! Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014

Signed vs. Unsigned Jump Instructions (Summary) Unsigned branch instructions: Equation: jhi = jump if higher than ---- jhs = jump if higher than or the same  same as jc C ?= 1 jlo = jump if lower than  same as jnc C ?= 0 jls = jump if lower than or the same ---- Signed branch instructions: jge = jump if greater than or equal (*) N xor V ?= 0 jgt = jump if greater than ---- jle = jump if less than or equal ---- jl = jump if less than (*) N xor V ?= 1 jn = jump if negative flag is set (jump if minus) N ?= 1 Neither: jnc = jump if carry flag is clear  same as jlo C ?= 0 jc = jump if carry flag is set  same as jhs C ?= 1 jeq/jz = jump if equal (zero flag is set) Z ?= 1 jne/jnz = jump if not equal (zero flag is clear) Z ?= 0 jmp = jump always (unconditional jump) ---- = DOES NOT EXIST = UNCONDITIONAL * = checks N and V flags Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014

ECE 3430 – Intro to Microcomputer Systems Conditional Jumps Compare Instructions Jump instructions do not modify SR bits. They simply read the current state of the N,V,C,Z flags. At times we want to alter the SR but not change the contents of the registers. By altering the state of the SR bits, we can control what value the jump condition is relative to --rather than always zero. Compare instructions allow us to do this: cmp <src>,<dst> = compare two values (destination not written), <dst> - <src> tst <dst> = test for zero or minus condition Typically, the above compare/test instructions should immediately precede a jump instruction! Ex: Consider ‘a’ and ‘b’ to both be unsigned. The expression “if (a >= b) then <label>” is evaluated: cmp b, a or cmp a, b NOTE: In MSP430, “jls” does jhs <label> jls <label> not exist! Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014

Using Compare Instructions to Influence Jump Instruction Behavior ORG 0xC000 Start: mov.b #120,R4 cmp.b 0x0200,R4 jl Set4 Clr4: mov.b &P1OUT,R4 and.b #11101111b,R4 jmp Rejoin Set4: mov.b &P1OUT,R4 bis.b #00010000b,R4 Rejoin: mov.b R4,&P1OUT START R4 = 120 R4 < M(0x0200)? YES P1.4 = 1 NO P1.4 = 0 What could be done to eliminate the redundant “mov.b &P1OUT,R4” instruction? How many other ways could this code be written? END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014

Using Compare Instructions to Influence Jump Instruction Behavior JGE (signed) takes branch if “N  V = 0” evaluates as true. JHS (unsigned) takes branch if “C = 1” evaluates as true. Assume M(0x0200) = 128 N Z V C 1 0 0 0 mov.b 0x0200,R4 1 0 0 0 cmp.b #120,R4 0 0 1 1 jge Set4 0  1 = 0 1 = 0 no jump jhs Set4 1 = 1 1 = 1 jump 128-120 = negative – positive = positive (overflow, V = 1). Subtracted smaller number from larger (no borrow, C = 1). Result not negative (N = 0). Two values were not the same (Z = 0). Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014

Using Compare Instructions to Influence Jump Instruction Behavior JGE (signed) takes branch if “N  V = 0” evaluates as true. JHS (unsigned) takes branch if “C = 1” evaluates as true. Assume M(0x0200) = 100 N Z V C 0 0 0 0 mov.b 0x0200,R4 0 0 0 0 cmp.b #120,R4 1 0 0 0 jge Set4 1  0 = 0 1 = 0 no jump jhs Set4 0 = 1 0 = 1 no jump 100-120 = positive – positive = negative (no overflow, V = 0). Subtracted larger number from smaller (borrow, C = 0). Result is negative (N = 1). Two values were not the same (Z = 0). Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014