Comparing and Branching ifs and loops part A. JMP instruction Consider the forever loop: for ( ; ; ) { … } How can we accomplish this in Assembler?

Slides:



Advertisements
Similar presentations
Jump Condition.
Advertisements

Flow of Control Instruction/Control structure Looping structure Looping structure Branching structure Branching structure For assembly language program.
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
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.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Assembly Language for Intel-Based Computers
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.
Quiz #2 Topics Character codes Intel IA-32 architecture Mostly MASM
Chapter 5 The LC-3. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-2 Instruction Set Architecture ISA.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
Application Security Tom Chothia Computer Security, Lecture 14.
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
Chapter 5 The LC Instruction Set Architecture ISA = All of the programmer-visible components and operations of the computer memory organization.
Dr. José M. Reyes Álamo 1.  Review: ◦ of Comparisons ◦ of Set on Condition  Statement Labels  Unconditional Jumps  Conditional Jumps.
The LC-3. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-2 Instruction Set Architecture ISA = All of the.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
(Flow Control Instructions)
1 ICS 51 Introductory Computer Organization Fall 2009.
Decision Structures – Code Generation Lecture 24 Mon, Apr 18, 2005.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 5 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
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.
Chapter 5 Branching and Looping.
EEL 3801 Part V Conditional Processing. This section explains how to implement conditional processing in Assembly Language for the 8086/8088 processors.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
Assembly 06. Outline cmp (review) Jump commands test mnemonic bt mnemonic Addressing 1.
Comparing and Branching ifs and loops Part B. LOOPS.
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.
Assembly Language Wei Gao. Assembler language Instructions.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
K.K. Leung Fall 2008Introductory Pentium Programming1 Pentium Architecture: Introductory Programming Kin K. Leung
Practical Session 2 Computer Architecture and Assembly Language.
Data Transfers, Addressing, and Arithmetic
The Java Virtual Machine (JVM)
CS216: Program and Data Representation
Homework Reading Labs PAL, pp
Instruksi Set Prosesor 8088
EE3541 Introduction to Microprocessors
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Computer Architecture
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Assembly IA-32.
INSTRUCTION SET.
More on logical instruction and
Assembly Language Programming Part 2
Microprocessor and Assembly Language
Processor Processor characterized by register set (state variables)
Flags Register & Jump Instruction
Machine-Level Programming: Control Flow
Shift & Rotate Instructions)
ECE 3430 – Intro to Microcomputer Systems
Program Logic and Control
Program Logic and Control
Homework Reading Machine Projects Labs PAL, pp
Flow Control Instructions
EECE.3170 Microprocessor Systems Design I
Assembly Language for Intel 8086 Jump Condition
Chapter 7 –Program Logic and Control
Carnegie Mellon Ithaca College
Chapter 7 –Program Logic and Control
Credits and Disclaimers
Computer Architecture and Assembly Language
Presentation transcript:

Comparing and Branching ifs and loops part A

JMP instruction Consider the forever loop: for ( ; ; ) { … } How can we accomplish this in Assembler?

JMP instruction jump/branch unconditionally (always) Transfers program control to a different point in the instruction stream without recording return information. The destination (target) operand specifies the address of the instruction being jumped to. This operand can be an immediate value, a general-purpose register, or a memory location.

JMP instruction

Consider the forever loop: for ( ; ; ) { … } How can we accomplish this in Assembler?

JMP instruction Consider the forever loop: for ( ; ; ) { … } lp: … jmplp

Comparing and branching Consider a few ifs in Java: if (x == 1) { … } if (x < 5) { … } else { … } if (x == 1) { … } else if (x >= 1000) { … } else if (x >= 100) { … } else { … } We need to develop a technique to accomplish this in Assembler.

CMP instruction Compares the first source operand with the second source operand and sets the status flags in the EFLAGS register according to the results. The comparison is performed by subtracting the second operand from the first operand and then setting the status flags in the same manner as the SUB instruction. Operation: – temp ← SRC1 − SignExtend(SRC2); – ModifyStatusFlags; (* Modify status flags in the same manner as the SUB instruction*) Flags Affected: – The CF, OF, SF, ZF, AF, and PF flags are set according to the result.

CMP instruction

Jcc instructions (jump/branch conditionally) Checks the state of one or more of the status flags in the EFLAGS register (CF, OF, PF, SF, and ZF) and, if the flags are in the specified state (condition), performs a jump to the target instruction specified by the destination operand.

Jcc instructions (jump/branch conditionally) A condition code (cc) is associated with each instruction to indicate the condition being tested for. If the condition is not satisfied, the jump is not performed and execution continues with the instruction following the Jcc instruction.

Jcc instructions (jump/branch conditionally) Notes: – The terms “less” and “greater” are used for comparisons of signed integers. – The terms “above” and “below” are used for unsigned integers.

Jcc instructions (jump/branch conditionally) Note: je and jz are exactly the same but are provided for readability.

Note: JGE and JNL are exactly the same but are provided for readability.

Most useful Jcc’s JE / JZ JG JGE JL JLE JNE / JNZ

Comparing and branching Consider a few ifs in Java: if (x == 1) { … } if (x < 5) { … } else { … } if (x == 1) { … } else if (x >= 1000) { … } else if (x >= 100) { … } else { … } So how can we code each of these in assembler?

Comparing and branching Consider a few ifs in Java: if (x == 1) { … }

Comparing and branching Consider a few ifs in Java: if (x == 1) { … } cmpx, 1 jnemore … more:

Comparing and branching Consider a few ifs in Java: if (x == 1) { … } cmpx, 1 jnemore … more: Jumps (takes the branch) only when ZF=0.

Comparing and branching Consider a few ifs in Java: if (x == 1) { … } cmpx, 1 jnemore … more: Jumps (takes the branch) only when ZF=0. jnz also takes the branch when ZF=0.

Comparing and branching Consider a few ifs in Java: if (x == 1) { … } cmpx, 1 jnemore … more: Avoid this: cmpx, 1 jedoIt jnemore doIt: … more:

Comparing and branching Consider a few ifs in Java: if (x < 5) { … } else { … }

Comparing and branching Consider a few ifs in Java: if (x < 5) { … } else { … } One possible solution: cmpx, 5 jnlelse1 … jmpend1 else1: … end1:

Comparing and branching Consider a few ifs in Java: if (x < 5) { … } else { … } Another possible solution: cmpx, 5 jgeelse1;same! … jmpend1 else1: … end1:

Comparing and branching Consider a few ifs in Java: if (x == 1) { … } else if (x >= 1000) { … } else if (x >= 100) { … } else { … }

Comparing and branching Consider a few ifs in Java: if (x == 1) { … } else if (x >= 1000) { … } else if (x >= 100) { … } else { … } cmpx, 1 jneelif1 … jmpdone elif1: cmpx, 1000 jlelif2; or jnge … jmpdone elif2: cmpx, 100 jlel1;or jnge … jmpdone el1: … done:

Comparing and branching Consider a few ifs in Java: if (x > 2 && x <= 5) { … } else { … }

Comparing and branching Consider a few ifs in Java: if (x > 2 && x <= 5) { … } else { … } cmpx, 2 jleel cmpx, 5 jgel … jmpdone el: … done:

Comparing and branching Consider a few ifs in Java: if (x 2) { … } else { … }

Comparing and branching Consider a few ifs in Java: if (x 2) { … } else { … } cmpx, 5 jlyes cmpy, 2 jleno yes: … jmpdone no: … done:

Comparing and branching Consider a few ifs in Java: if (x != y) { … } else { … } (Hint: We don’t have cmp m32, m32!)

Comparing and branching Consider a few ifs in Java: if (x != y) { … } else { … } moveax, x cmpeax, y jeel … jmpdone el: … done:

Avoid double jumps! if (x>10){ i = 12; } else { i = 9; } correct: cmpx, 10 jleno movi, 12 jmpdone no: movi, 9 done: wrong: cmpx, 10 jgyes jmpno yes: movi, 12 jmpdone no: movi, 9 jmpdone done:

ADVANCED TOPIC: DISJUNCTION Danger, Will Robinson!

Disjunction Java (and other languages as well) support a variety of similar boolean operators: if (a && b)… if (a & b)… if (a || b)…Are they exactly the same, or are they different? if (a | b)…

Disjunction ion/html/expressions.html# ion/html/expressions.html#15.23 – “The && operator is like & (§ ), but evaluates its right-hand operand only if the value of its left-hand operand is true.” – “The || operator is like | (§ ), but evaluates its right-hand operand only if the value of its left- hand operand is false.”

Disjunction 1.How can we demonstrate that this is true in Java? 2.How can we implement this in Assembler?

Disjunction public static void main ( String[] s ) { if ( true || false ) { System.out.println( "main: in first if" ); } if ( true | false ) { System.out.println( "main: in second if" ); } These are the cases we wish to test. But we need more.

Disjunction private static boolean T ( ) { System.out.println( "T()" ); return true; } private static boolean F ( ) { System.out.println( "F()" ); return false; } public static void main ( String[] s ) { if ( true || false ) { System.out.println( "main: in first if" ); } if ( true | false ) { System.out.println( "main: in second if" ); } How can these functions help?

Disjunction private static boolean T ( ) { System.out.println( "T()" ); return true; } private static boolean F ( ) { System.out.println( "F()" ); return false; } public static void main ( String[] s ) { if ( T() || F() ) { System.out.println( "main: in first if" ); } if ( T() | F() ) { System.out.println( "main: in second if" ); } How can these functions help?

DISASSEMBLY, DISJUNCTION, AND THE JVM

Disjunction Let’s look at some JVM (Java Virtual Machine) code. javap (see run on a.class file will disassemble it for us into JVM code. javap –c test The JVM spec can be found here:

Disjunction public static void main(java.lang.String[]); Code: 0: invokestatic #6;//Method T:()Z 3: ifne 12//br if true 6: invokestatic #7;//Method F:()Z 9: ifeq 20//br if false 12: getstatic #2;//Field java/lang/System.out:Ljava/io/PrintStream; 15: ldc #8;//String main: in first if 17: invokevirtual #4;//Method java/io/PrintStream.println:(Ljava/lang/String;)V 20: invokestatic #6;//Method T:()Z 23: invokestatic #7;//Method F:()Z 26: ior 27: ifeq 38//br if false 30: getstatic #2;//Field java/lang/System.out:Ljava/io/PrintStream; 33: ldc #9;//String main: in second if 35: invokevirtual #4;//Method java/io/PrintStream.println:(Ljava/lang/String;)V 38: return

Disjunction private static boolean T ( ) { System.out.println( "T()" ); return true; } private static boolean F ( ) { System.out.println( "F()" ); return false; } public static void main ( String[] s ) { if ( T() || F() ) { System.out.println( "main: in first if" ); } if ( T() | F() ) { System.out.println( "main: in second if" ); } public static void main(java.lang.String[]); Code: 0: invokestatic #6;//Method T:()Z 3: ifne 12 6: invokestatic #7;//Method F:()Z 9: ifeq 20 12: getstatic #2;//Field java/lang/System.out:Ljava/io/PrintStream; 15: ldc #8;//String main: in first if 17: invokevirtual #4;//Method java/io/PrintStream.println:(Ljava/lang/String;)V 20: invokestatic #6;//Method T:()Z 23: invokestatic #7;//Method F:()Z 26: ior 27: ifeq 38 30: getstatic #2;//Field java/lang/System.out:Ljava/io/PrintStream; 33: ldc #9;//String main: in second if 35: invokevirtual #4;//Method java/io/PrintStream.println:(Ljava/lang/String;)V 38: return

NEXT TOPIC: LOOPS