Intel x86 Jump Instructions. Part 6. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Similar documents
Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

6.041SC Probabilistic Systems Analysis and Applied Probability, Fall 2013 Transcript Lecture 3

Artificial Intelligence Prof. Deepak Khemani Department of Computer Science and Engineering Indian Institute of Technology, Madras

MITOCW ocw f99-lec18_300k

PRESENT REAL GENERAL TRUTHS (ZERO CONDITIONAL) If you add two and two, you get four. PRESENT HABITS

MITOCW MITRES18_006F10_26_0703_300k-mp4

Artificial Intelligence Prof. P. Dasgupta Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

CONDITIONAL SENTENCES CONDITIONAL SENTENCES

UCB CS61C : Machine Structures

Digital Logic Lecture 5 Boolean Algebra and Logic Gates Part I

MITOCW ocw f99-lec19_300k

Surveying Prof. Bharat Lohani Department of Civil Engineering Indian Institute of Technology, Kanpur. Module - 7 Lecture - 3 Levelling and Contouring

MITOCW Lec 2 MIT 6.042J Mathematics for Computer Science, Fall 2010

Artificial Intelligence. Clause Form and The Resolution Rule. Prof. Deepak Khemani. Department of Computer Science and Engineering

Math 10 Lesson 1 4 Answers

Lesson 09 Notes. Machine Learning. Intro

Module 02 Lecture - 10 Inferential Statistics Single Sample Tests

Deduction by Daniel Bonevac. Chapter 1 Basic Concepts of Logic

The Good, the Bad, and the Ugly

Whatever happened to cman?

MITOCW watch?v=ogo1gpxsuzu

MITOCW ocw f08-rec10_300k

TRANSCRIPT. Contact Repository Implementation Working Group Meeting Durban 14 July 2013

With the "skills gap" more eminent than ever, preparing the next generation for careers in technology is becoming

LESSON 1: Determining Your Legacy

Summary of Research about Denominational Structure in the North American Division of the Seventh-day Adventist Church

(Refer Slide Time 03:00)

The following content is provided under a Creative Commons license. Your support

Number In Scripture PDF

Commentary on Sample Test (May 2005)

ECE 5424: Introduction to Machine Learning

>> Marian Small: I was talking to a grade one teacher yesterday, and she was telling me

Light Omega Podcasts

Step 1 Pick an unwanted emotion. Step 2 Identify the thoughts behind your unwanted emotion

Church Leader Survey. Source of Data

Verification and Validation

NPTEL NPTEL ONLINE COURSES REINFORCEMENT LEARNING. UCB1 Explanation (UCB1)

THE GENESIS 1:1/JOHN 1:1 TRIANGLE. TRIPLE CIPHERS OF JOHN 1:1 (Part 1) > TRIPLE CIPHERS OF JOHN 1:1 (Part 2) By Leo Tavares

PHILOSOPHY OF RELIGION W E E K 1 1 D A Y 2 : R E L I G I O U S L A N G U A G E

AWAKENING DYNAMICS VIP CLUB SPECIAL EVENT. Emotional Eating Brent Phillips. Effortless Clearing Audio

! a c b. ! 100 a c b

Worksheet Exercise 1.1. Logic Questions

SUMMARY COMPARISON of 6 th grade Math texts approved for 2007 local Texas adoption

The Transmission of God s Word: Gender and Bible Choice

Computing Machinery and Intelligence. The Imitation Game. Criticisms of the Game. The Imitation Game. Machines Concerned in the Game

Outline. Uninformed Search. Problem-solving by searching. Requirements for searching. Problem-solving by searching Uninformed search techniques

The Book of the Covenant vs. The Book of the Law?

Diocese of Orlando. Guidelines for the Use of Video Projection in Liturgical Celebrations. Introduction

CS 2104 Intro Problem Solving in Computer Science Test 1 READ THIS NOW!

Content Area Variations of Academic Language

WHO'S IN CHARGE? HE'S NOT THE BOSS OF ME. Reply. Dear Professor Theophilus:

AGENT CAUSATION AND RESPONSIBILITY: A REPLY TO FLINT

Christ-Centered Preaching: Preparation and Delivery of Sermons Lesson 6a, page 1

The following content is provided under a Creative Commons license. Your support

HAPPINESS QUICK NOTES Healing the Culture Visit the Principles and Choices Facebook page. 1

The Personal, Professional and Spiritual Success Mastery Program Created by

Heaven Declares: Prophetic Decrees To Start Your Day PDF

Excel Lesson 3 page 1 April 15

Virtual Logic Number and Imagination

Number, Part I of II

Math Matters: Why Do I Need To Know This? 1 Logic Understanding the English language

Learning The Secret of Contented Living Philippians 4:10-14 (NKJV)

Balancing Authority Ace Limit (BAAL) Proof-of-Concept BAAL Field Trial

IMPLEMENTING GOD S WORD... YEAR FIVE FALL QUARTER CHRISTIAN APOLOGETICS 1 SUNDAY SCHOOL CURRICULUM FOR HIGH SCHOOL YOUTH SSY05F

The Fixed Hebrew Calendar

Christ's Atoning Death and Eternal Redemption Hebrews 9:11-14

Torah Code Cluster Probabilities

Transcription ICANN London IDN Variants Saturday 21 June 2014

A Dialog with Our Father - Version 1

COMPARATIVE RELIGION

The Bible Study For Beginners Series: Learn The Bible In The Least Amount Of Time: The Bible, Bible Study, Christian, Catholic, Holy Bible, Book 4

Houghton Mifflin MATHEMATICS

MIRACLES, BUTTERFLIES, & RESURRECTION

The Kohlberg Dilemmas By Lawrence Kohlberg From Moral Judgment Interviews 1958

UNITED STATES DISTRICT COURT

IN THE MATTER OF THE SHOOTING OF A MALE BY A MEMBER OF THE RCMP NEAR THE CITY OF KELOWNA, BRITISH COLUMBIA ON AUGUST 3, 2017

Follow Will of the People. Your leftist h. b. ave often d1sgusted b h

WEEK #7: Chapter 5 HOW IT WORKS (Step 4)

*THE AMAZING GRACE OF GOD Psalm 86:15, Isaiah 30:18

Preface. Current Implied Meanings

Admissions Policy

An Alternative to Risk Management for Information and Software Security Transcript

Chapter 5: Freedom and Determinism

Report Generation WorkFlow. Production for Individual Instructors. BLUE Course Evaluation System. Hossein Hakimzadeh 6/1/2016

Nothing Just Happens Fall Series: Expecting An Encounter Installment Four Exodus 2:1-10, {Moses guided by currents into the purposes of God}

INTERMEDIATE LOGIC Glossary of key terms

Minimal and Maximal Models in Reinforcement Learning

When we talk about things that are generally or always true, we can use: If/When/Unless plus a present form PLUS present simple or imperative

Shahriar Shahriari William Polk Russell Professor of Mathematics. Pomona College Convocation 2010 August 31, 2010

Making Sense Out of Life's Puzzle

MITOCW watch?v=6pxncdxixne

Grade 6 Math Connects Suggested Course Outline for Schooling at Home

To make it in life you've got to learn to deal with people. One of the secrets of success is learning how to deal with people who disappoint you.

Artificial Intelligence Prof. Deepak Khemani Department of Computer Science and Engineering Indian Institute of Technology, Madras

Sermon - Eye-Opening Prayer Sunday January 11, 2015

Matthew. Chapter 18. Blue Letter Bible

HARRY TRIGUBOFF. HOWARD: Why did your family choose to come to Australia? I know you were living in China but why did you

6.00 Introduction to Computer Science and Programming, Fall 2008

A CONSEQUENTIALIST RESPONSE TO THE DEMANDINGNESS OBJECTION Nicholas R. Baker, Lee University THE DEMANDS OF ACT CONSEQUENTIALISM

September 27, 2009 Your Final Breath Hebrews 9:27-28

Transcription:

Part 6 Intel x86 Jump Instructions Control Logic Fly over code Operations: Program Flow Control Operations: Program Flow Control Unlike high-level languages, processors don't have fancy expressions or blocks Programs are controlled by jumping over blocks of code based on status flags The processor moves the program counter (where your program is running in memory) to a new address and execution continues 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 3 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 4 Types of Jumps: Unconditional Instruction: Jump Unconditional jumps simple transfers the running program to a new address Basically, it just "gotos" to a new line These are used extensively to recreate the blocks we use in 3GLs (like Java) JMP address Usually a label an constant that holds an address 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 5 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 6 1

Infinite Loop Infinite Loop.data message:.ascii "I'm getting dizzy!\n\0".text.global _start _start: mov $message, %rcx Loop: call PrintCString jmp Loop _start: mov $message, %rcx Loop: call PrintCString jmp Loop 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 7 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 8 Instruction: Compare Behind the scenes Performs a comparison operation between two arguments The result of the comparison is used for conditional jumps Necessary to construct all conditional statements if, while, The first argument is subtracted from the second The result of this computation is used to determine how the operands compare This subtraction result is discarded 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 9 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 10 But why subtract? Instruction: Compare Why subtract the operands? The result can tell you which is larger For example: A and B are both positive A B positive number A was larger A B negative number B was larger A B zero both numbers are equal Immediate, Register, Memory CMP arg-1, arg-2 Register, Memory 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 11 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 12 2

Flags Flags A flag is a Boolean value that indicates the result of an action These are set by various actions such as calculations, comparisons, etc Flags are typically stored as individual bits in the Status Register You can't change the register directly, but numerous instructions use it for control and logic 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 13 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 14 Zero Flag (ZF) Sign Flag (SF) True if the last computation resulted in zero (all bits are 0) For compare, the zero flag indicates the two operands are equal Used by quite a few conditional jump statements True of the most significant bit of the result is 1 This would indicate a negative 2's complement number Meaningless if the operands are interpreted as unsigned 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 15 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 16 Carry Flag (CF) Overflow Flag (OF) True if a 1 is "borrowed" when subtraction is performed or a 1 is "carried" from addition For unsigned numbers, it indicates: exceeded the size of the register on addition or an underflow (too small value) on subtraction Also known as "signed carry flag" True if the sign bit changed when it shouldn't For example: (negative positive number) should be negative a positive result will set the flag For signed numbers, it indicates: exceeded the size of the register on addition or an underflow (too small value) on subtraction 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 17 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 18 3

x86 Flags Used by Compare -68 vs. 30 (if interpreted as signed) 188 vs. 30 (if interpreted as unsigned) Name Description When True CF 0 0 CF Carry Flag If an extra bit was "carried" or "borrowed" during math. ZF Zero Flag All the bits in the result are zero. OF 0 1 0 1 1 1 1 0 0 0 0 0 1 1 1 1 0 SF Sign Flag If the most significant bit is 1. OF Overflow Flag If the sign-bit changed when it shouldn t have. SF 1 1 0 0 1 1 1 1 0 ZF 0 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 19 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 20 Conditional Jumps Conditional Jumps Conditional jumps (aka branching) will only jump if a certain condition is met What happens processor jumps if and only if a specific status flag is set otherwise, it simply continues with the next instruction x86 contains a large number of conditional jump statements Each takes advantage of status flags (such as the ones set with compare) x86 assembly has several names for the same instruction which adds readability 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 21 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 22 Jump on Equality Conditional Jump Example Jump Description When True JE Equal ZF = 1 JNE Not equal ZF = 0 _start: cmp $13, %rax je Equal... rax = 13? Equal:... 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 23 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 24 4

Signed Jump Instructions Unsigned Jumps Jump Description When True JG Jump Greater than SF = OF, ZF = 0 JGE Jump Greater than or Equal SF = OF JL Jump Less than SF OF, ZF = 0 JLE Jump Less than or Equal SF OF Jump Description When True JA Jump Above CF = 0, ZF = 0 JAE Jump Above or Equal CF = 0 JB Jump Below CF = 1, ZF = 0 JBE Jump Below or Equal CF = 1 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 25 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 26 Conditional Jump Example _start: mov $42, %rax cmp $13, %rax jge Bigger... Bigger: add $5, %rax rax >= 13? (yes, its backwards!) If Statements on the x86 How to we conditionally execute code? 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 27 If Statements in assembly If Statements in assembly High-level programming language have easy to use If- Statements However, processors handle all branching logic using jumps You basically jump over true and else blocks Converting from an If Statement to assembly is easy Let's look at If Statements the block only executes if the expression is true so, if the expression is false your program will skip over the block this is a jump 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 29 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 30 5

If Statement jumps over code Converting an If Statement rax = 18; if (rax >= 21) //true part rbx = 12; False Compare the two values If the result is false then jump over the true block you will need label to jump to To jump on false, reverse your logic a < b not (a >= b) a >= b not (a < b) 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 31 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 32 Please Note Converting an If Statement Following examples use very generic label names In your program, each label you create must be unique So, please don't think that each label (as it is typed) is "the" label you need to use if (rax >= 21) //true block //end Greater-Than or Equal So, jump on Less-Than 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 33 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 34 Jump over true part Jump over true part jl End Branch when false. JL (Jump Less Than) is the opposite of JGE jl End Jumps over true part 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 35 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 36 6

Else Clause Else Clause The Else Clause is a tad more complex You need to have a true block and a false block Like before you must jump over instructions just remember: the program will continue with the next instruction unless you jump! if (rax >= 21) //true block else //false block //end 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 37 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 38 Jump over true part Jump over true part jl Else Jump to false block jl Else Else: #false block False block flows down to End Else: #false block If we run the true block, we have to jump over the false block 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 39 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 40 Alternative Approach If Statement No Else In the examples before, I put the False Block first and used inverted logic for the jump You can construct If Statements without inverting the conditional jump, but the format is layout is different jge Then Then: Jumps to true block 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 41 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 42 7

If Statement No Else If Statement with Else jge Then Then: Jump to end if false (it didn't jump with JGE) jge Then #false block Then: Notice that this is identical to the last slide the false block is just empty 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 43 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 44 While Statement While Loops Doing the same thing again and again and again Processors do not have While Statements just like If Statements Looping is performed much like an implementing an If Statement A While Statement is, in fact, the same thing as an If Statement 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 46 If Statement vs. While Statement Converting a While Statement If Statement Uses a conditional expression Executes a block of statements Executes only once While Statement Uses a conditional expression Executes a block of statements Executes multiple times To create a While Statement start with an If Statement and add an unconditional jump at the end of the block that jumps to the beginning You will "branch out" of an infinite loop Structurally, this is almost identical to what you did before However, you do need another label :( 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 47 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 48 8

Converting an While Statement Converting an While Statement while (rax < 21) //true block //end Less-Than. So, jump on Greater-Than or Equal jge End Branch when false. JL (Jump Less Than) is the opposite of >= 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 49 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 50 Converting an While Statement Converting an While Statement jge End jge End Escape infinite loop Loop after block executes 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 51 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 52 Alternative Approach Alternative Approach Before, we created an If Statement by inverting the branch logic (jump on false) You can, alternatively, also implement a While Statement without inverting the logic Either approach is valid use what you think is best while (rax < 21) //true block //end 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 53 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 54 9

Alternative Approach Alternative Approach jl Do Do: Jumps to Do Block jl Do Do: bge was false, jump out of the loop 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 55 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 56 Alternative Approach jl Do Do: Repeat the loop 10/29/2018 Sacramento State - Cook - CSc 35 - Fall 2018 57 10