Decision Structures – Code Generation Lecture 24 Mon, Apr 18, 2005.

Slides:



Advertisements
Similar presentations
Jump Condition.
Advertisements

Instruction Set of 8086 Engr. M.Zakir Shaikh
Departemen Ilmu Komputer FMIPA IPB Conditional Jump Instruction A conditional jump instruction transfer control to a destination address when a.
Floating Point Unit. Floating Point Unit ( FPU ) There are eight registers, namely ST(0), ST(1), ST(2), …, ST(7). They are maintained as a stack.
Decision Structures – The Syntax Tree Lecture 22 Fri, Apr 8, 2005.
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
NEG Instruction Change operand content into two’s complement (negative value) and stored back into its operand mov bl, b neg bl; bl = mov.
Intel Computer Architecture Presented By Jessica Graziano.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Conditional Loop Instructions LOOPZ and LOOPE LOOPNZ.
Assembly Language :CSC 225 (Lec#4: Flag Register and Conditional Statements) By Dr. Syed Noman.
Gursharan Singh Tatla 21-Nov-20101www.eazynotes.com.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Assembly Language for Intel-Based Computers
CS2422 Assembly Language & System Programming October 17, 2006.
Flow Control Instructions
Conditional Processing If … then … else While … do; Repeat … until.
Quiz #2 Topics Character codes Intel IA-32 architecture Mostly MASM
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#5) By Dr. Syed Noman.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Arithmetic Flags and Instructions
(Flow Control Instructions)
Procedures – Generating the Code Lecture 21 Mon, Apr 4, 2005.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
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.
The x86 Instruction Set Lecture 16 Mon, Mar 14, 2005.
Chapter 5 Branching and Looping.
Conditional Loop Instructions, Conditional Structures
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.
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.
Improvements to the Compiler Lecture 27 Mon, Apr 26, 2004.
CSC 221 Computer Organization and Assembly Language Lecture 20: Conditional and Block Structures.
Selection and Iteration Chapter 8 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Assembly Language for Intel-Based Computers, 4 th Edition Lecture 22: Conditional Loops (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 4 th Edition Week 10: Conditional Processing Slides modified by Dr. Osama Younes.
K.K. Leung Fall 2008Introductory Pentium Programming1 Pentium Architecture: Introductory Programming Kin K. Leung
CS-401 Computer Architecture & Assembly Language Programming
Assembly תרגול 7 תכנות באסמבלי, המשך. Condition Codes Single bit registers  CF – carry flag  ZF – zero flag  SF – sign flag  OF – overflow flag Relevant.
1 Conditional Processing Chapter 6 Adapted with many modifications from slides prepared by Professor Mario Marchand Computer Science Dept. University.
Conditional Branch Example
Homework Reading Labs PAL, pp
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Assembly IA-32.
INSTRUCTION SET.
More on logical instruction and
Assembly Language Programming Part 2
CS 301 Fall 2002 Assembly Instructions
Computer Organization and Assembly Language
Machine-Level Programming 2 Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming 2 Control Flow
Program Logic and Control
Program Logic and Control
Homework Reading Machine Projects Labs PAL, pp
Flow Control Instructions
Computer Organization and Assembly Languages Yung-Yu Chuang 2005/12/22
X86 Assembly Review.
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:

Decision Structures – Code Generation Lecture 24 Mon, Apr 18, 2005

Conditional Branches Branching on a conditional expression will be done in two parts. Evaluate the expression, leaving either true (1) or false (0) on the stack. Test the value on the stack and branch on true. It would be more efficient to test the original expression and branch immediately. However, by separating the two tests, it will be simpler to write the code.

Conditional Branches Recall that the if statement is of the form IF ( expr ) stmt where expr is either zero (false) or nonzero (true). Thus, we must compare expr to 0. First, we generate code for a CMPNE tree that will leave 1 on the stack if expr is nonzero.

The CMPNE Tree CMPNE INT NUM 0 e INT Integer Expression Floating-point Expression CMPNE DOUBLE CAST DOUBLE e DOUBLE NUM 0

Integer Comparisons To compare two integers, we must Pop them off the stack into registers. Compare the registers. Push 0 or 1 onto the stack. There is only one compare instruction: cmp. It sets various flags, depending on the result of the comparison.

Flags CF, ZF, SF, and OF The relevant flags are CF – carry flag ZF – zero flag SF – sign flag OF – overflow flag

Conditional Jumps We then do a condition jump. A conditional jump checks the flags to decide whether or not to jump. MnemonicMeaningFlags jejump ==ZF = 1 jnejump !=ZF = 0 jljump < SF  OF jgejump >=SF = OF jgjump >ZF = 0 and SF = OF jlejump <=ZF = 1 or SF = OF

Integer Comparison The code for the integer comparison !=. mov $1,%ecx # Move true to ecx pop %eax # Pop right operand pop %edx # Pop left operand cmp %eax,%edx # Compare operands jne L0n # Jump if left != right dec %ecx # Change ecx to false L0n: push %ecx # Push T/F result

Floating-Point Comparisons When we compare two floating-point numbers, they are already on the FPU stack, in st(0) and st(1). The instruction fucompp Compares st(0) to st(1). Sets condition codes C0 and C3 in the FPU status word. Pops both operands off the stack.

Flags C0 and C3 The result of the comparison is determined by C0 and C3. ResultC0C3 st(0) > st(1)00 st(0) < st(1)10 st(0) = st(1)01

Floating-Point Comparisons To perform a conditional jump based on a floating-point comparison, we must first move the FPU status word to the EFLAGS register. The instruction fnstsw %ax stores the FPU status word in ax (16 bits). The instruction sahf stores ah in the EFLAGS regsiter.

Floating-Point Comparisons The sahf instruction maps C0  CF. C3  ZF. Thus, we want to use conditional jumps that check CF and ZF.

Conditional Jumps MnemonicMeaningFlags jejump equalZF = 1 jnejump not equalZF = 0 jbjump belowCF = 1 jaejump above or equalCF = 0 jajump aboveCF = 0 and ZF = 0 jbejump below or equalCF = 1 or ZF = 1

Floating-Point Comparisons The code for the floating-point comparison !=. mov $1,%ecx # Move true to ecx fucompp # Pop left operand fnstsw %ax # Store status word sahf # Move ah to eflags jne L0n # Jump if left != right dec %ecx # Change ecx to false L0n: push %ecx # Push T/F result

Floating-Point Comparisons One little complication is that fucompp compares st(0) and st(1) in the reverse order from what we would expect. Therefore, the meanings if are reversed and the meanings of = are reversed. So we reverse the roles of ja and jb, and jae and jbe.

Jump Trees There are JUMP trees and JUMPT trees. JUMP BLABEL 6 JUMPT BLABEL 6 CMPNE e.mode NUM 0 e e.mode

JUMP Trees A JUMP tree generates the following code. jmp B6 jmp L8

JUMPT Trees A JUMPT tree must pop a boolean value off the stack and jump only if it is 1. pop %eax cmp $1,%eax je B6 pop %eax cmp $1,%eax je L6