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

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

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

MITOCW ocw f99-lec18_300k

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

UCB CS61C : Machine Structures

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

Number, Part I of II

MITOCW MITRES18_006F10_26_0703_300k-mp4

Math 10 Lesson 1 4 Answers

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

The Good, the Bad, and the Ugly

MITOCW ocw f99-lec19_300k

Introduction to Statistical Hypothesis Testing Prof. Arun K Tangirala Department of Chemical Engineering Indian Institute of Technology, Madras

CONDITIONAL SENTENCES CONDITIONAL SENTENCES

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

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

Digital Logic Lecture 5 Boolean Algebra and Logic Gates Part I

(Refer Slide Time 03:00)

The Transmission of God s Word: Gender and Bible Choice

Lesson 09 Notes. Machine Learning. Intro

The Fixed Hebrew Calendar

Commentary on Sample Test (May 2005)

Symbolic Logic Prof. Chhanda Chakraborti Department of Humanities and Social Sciences Indian Institute of Technology, Kharagpur

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

Worksheet Exercise 1.1. Logic Questions

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

Whatever happened to cman?

1)Asher: create a handout for the week summing up LOGIC

Cash Register Exercise

Light Omega Podcasts

Programming Language Research

Grade 6 Math Connects Suggested Course Outline for Schooling at Home

The Cosmological Argument

Deduction by Daniel Bonevac. Chapter 1 Basic Concepts of Logic

Logic & Proofs. Chapter 3 Content. Sentential Logic Semantics. Contents: Studying this chapter will enable you to:

Virtual Logic Number and Imagination

Your Higher Self is your Soul Self. It is the ancient, infinitely wise part of you. What Is Your Higher Self?

Verification and Validation

ORDINAL GENESIS 1:1/JOHN 1:1 TRIANGLE (Part 1)

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

Chapter 3: Basic Propositional Logic. Based on Harry Gensler s book For CS2209A/B By Dr. Charles Ling;

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

SEVENTH GRADE RELIGION

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

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

OPENRULES. Tutorial. Determine Patient Therapy. Decision Model. Open Source Business Decision Management System. Release 6.0

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

Houghton Mifflin MATHEMATICS

THE GOD/MAN TRIANGLE OF JESUS CHRIST. THE IMAGE OF GOD (Part 1) > THE IMAGE OF GOD (Part 2) By Leo Tavares

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

September 17-18, Joshua and Jericho. Joshua 5-6, Isaiah 40:28. God wants our obedience.

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

LESSON 1: Determining Your Legacy

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

The Circle Maker Praying Circles Around Your Biggest Dreams and Greatest Fears. By: Mark Batterson

HOW TO WRITE AN NDES POLICY MODULE

The Inscrutability of Reference and the Scrutability of Truth

CSSS/SOC/STAT 321 Case-Based Statistics I. Introduction to Probability

ORDINAL GENESIS 1:1/JOHN 1:1 TRIANGLE (Part 2) By Leo Tavares

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

III Knowledge is true belief based on argument. Plato, Theaetetus, 201 c-d Is Justified True Belief Knowledge? Edmund Gettier

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

Artificial Intelligence: Valid Arguments and Proof Systems. Prof. Deepak Khemani. Department of Computer Science and Engineering

The Personal, Professional and Spiritual Success Mastery Program Created by

Information Booklet for Donors

Module 5. Knowledge Representation and Logic (Propositional Logic) Version 2 CSE IIT, Kharagpur

Chapter 6, Tutorial 1 Predicate Logic Introduction

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

Summary of Registration Changes

What would count as Ibn Sīnā (11th century Persia) having first order logic?

A romp through the foothills of logic Session 3

! a c b. ! 100 a c b

Module 02 Lecture - 10 Inferential Statistics Single Sample Tests

Fr. Copleston vs. Bertrand Russell: The Famous 1948 BBC Radio Debate on the Existence of God

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

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

Discover God's Calling On Your Life

ABB STOTZ-KONTAKT GmbH ABB i-bus KNX DGN/S DALI Gateway for emergency lighting

Chapter 5: Freedom and Determinism

Part II: How to Evaluate Deductive Arguments

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

SQL: A Language for Database Applications

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

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

What is an Argument? Validity vs. Soundess of Arguments

Love Builds Up. 1 Corinthians 8:1-13 July 13,

Truth and Modality - can they be reconciled?

Grade 7 Math Connects Suggested Course Outline for Schooling at Home 132 lessons

MITOCW watch?v=ogo1gpxsuzu

Date of last example: Never Today/yesterday Last week Last month Last year Before the last year

October 24, 2010 You Might Be A Methodist Matthew 25: You Might Be A Methodist Rev. Michael Love October 24, 2010 Text: Matthew 25:31-40

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

MITOCW ocw f08-rec10_300k

Wholehearted Coaching: Week Three Self-Love & Worthiness

Tips for Using Logos Bible Software Version 3

en.mp3 [audio.icann.org] Adobe Connect recording:

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

Necessity in mathematics

FUNDAMENTAL PRINCIPLES OF THE METAPHYSIC OF MORALS. by Immanuel Kant

Illustrating Deduction. A Didactic Sequence for Secondary School

Transcription:

Part 5 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/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 3 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 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/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 5 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 6 1

Infinite Loop Infinite Loop.data message:.ascii "I'm getting dizzy!\n\0".text.global _start _start: mov $message, %rax Loop: call PrintCString jmp Loop _start: mov $message, %rax Loop: call PrintCString jmp Loop 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 7 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 8 Types of Jumps: Conditional Instruction: Compare 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 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, 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 9 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 10 Instruction: Compare Instruction: Compare Behind the scenes first argument is subtracted from the second both values are interpreted as signed integers and both are sign-extended to the same size subtraction result is discarded 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 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 11 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 12 2

Instruction: Compare Flags Immediate, Register, Memory CMP arg-1, arg-2 Register, Memory A flag is a Boolean value that indicates the result of an action These are set by various actions such as calculations, comparisons, etc 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 13 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 14 Flags Zero Flag (ZF) 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 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 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 15 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 16 Sign Flag (SF) Carry Flag (CF) 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 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 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 17 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 18 3

Overflow Flag (OF) x86 Flags Used by Compare 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 Name Description When True CF Carry Flag If an extra bit was "carried" or "borrowed" during math. ZF Zero Flag All the bits in the result are zero. SF Sign Flag If the most significant bit is 1. OF Overflow Flag If the sign-bit changed when it shouldn t have. 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 19 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 20-68 vs. 30 (if interpreted as signed) 188 vs. 30 (if interpreted as unsigned) Jump Instructions CF 0 0 OF 0 1 0 1 1 1 1 0 0 0 0 0 1 1 1 1 0 1 0 0 1 1 1 1 0 ZF 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 SF 1 0 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 21 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 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/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 23 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 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/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 25 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 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/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 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/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 29 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 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/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 31 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 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/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 33 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 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/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 35 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 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/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 37 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 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/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 39 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 40 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/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 41 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 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/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 43 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 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/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 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/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 47 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 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 While: jge End jmp While Branch when false. JL (Jump Less Than) is the opposite of >= 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 49 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 50 Converting an While Statement Converting an While Statement While: jge End While: jge End Escape infinite loop jmp While Loop after block executes jmp While 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 51 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 52 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/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 53 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 54 9

While: jl Do Do: jmp While Jumps to Do Block While: jl Do Do: jmp While bge was false, jump out of the loop 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 55 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 56 While: jl Do Do: jmp While Repeat the loop Do Loops Test Last While Loops 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 57 Do Loops Converting Do Loops Programming languages also support test-last loop statements Many programming languages use the keyword "repeat" or "do" Easier than While Statements do //true block while (rax < 10); //end We jump UP when TRUE 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 59 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 60 10

Converting Do Loops Do: jl Do Positive logic You can also implement Do Loops using negative logic But it requires a few an extra label and jump statement 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 61 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 62 Do: Do: jge End jmp Do Negative logic jge End jmp Do Infinite loop 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 63 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 64 Switch Statements on the x86 Switch Statements on the x86 Reason for the C, Java, and C# design You might have noticed the strange behavior of Switch statements in C, Java, and C# Java and C# inherited their behavior from C 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 66 11

Switch Statements on the x86 Switch Statement C, in turn, was designed for embedded systems Language creates very efficient assembly code The Switch Statement converts easily to efficient code It is very efficient because it is restricted to integer constants once a case is matched, no others are checked they can fall through to match multiple values So, how? start of the statement sets up just 1 register compared to each "case" constant jumps to a label created for each 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 67 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 68 Switch Statement Syntax C/Java Code switch (integer) case value : Statements Statements integer expression You can have as many of these as needed Executed if nothing matched switch (Party) case 1: Democrat(); case 2: Republican(); ThirdParty(); 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 69 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 70 Assembly Code Assembly Code mov Party, %rax cmp $1, %rax je case_1 cmp $2, %rax je case_2 jmp default case_1: call Democrat case_2: call Republican call ThirdParty mov Party, %rax cmp $1, %rax je case_1 cmp $2, %rax je case_2 jmp default case_1: call Democrat case_2: call Republican call ThirdParty Jump header 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 71 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 72 12

Assembly Code: Jump Header Assembly Code mov Party, %rax cmp $1, %rax case 1: je case_1 cmp $2, %rax je case_2 jmp default case 2: mov Party, %rax cmp $1, %rax je case_1 cmp $2, %rax je case_2 jmp default case_1: call Democrat case_2: call Republican call ThirdParty Case Body 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 73 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 74 Assembly Code: The Case Body Fall-Through Labels case_1: call Democrat case_2: call Republican call ThirdParty Each "falls through". They are just labels! 1 Democrat Republican Third Party 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 75 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 76 Break Statement Java Code Even in the last example, we still fallthrough to the default The "Break" Statement is used exit a case Semantics simply jumps to a label after the last case so, break converts directly to a single jump switch (Party) case 1: Democrat(); break; case 2: Republican(); break; ThirdParty(); Let's jump to the end 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 77 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 78 13

Assembly Code: The Cases When Fallthrough Works case_1: call Democrat case_2: call Republican call ThirdParty Break jumps to the end The fallthrough behavior of C was designed for a reason It makes it easy to combine "cases" make a Switch Statement match multiple values and keeps the same efficient assembly code 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 79 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 80 Java Code: Primes from 1 to 10 Primes: Jump Header switch (number) case 2: case 3: case 5: case 7: result = True; break; result = False; Match Multiple mov Number, %rax cmp $2, %rax je case_2 cmp $3, %rax je case_3 cmp $5, %rax je case_5 cmp $7, %rax je case_7 jmp default These are our primes 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 81 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 82 Assembly Code: The Cases case_2: case_3: case_7: case_9: mov $1, Result mov $0, Result All these labels will be at the same address. You, of course, would write prettier code. 10/5/2017 Sacramento State - Cook - CSc 35 - Fall 2017 83 14