CS2422 Assembly Language & System Programming October 17, 2006.

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.
Assembly Programming Notes for Practical 3 Munaf Sheikh
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
Assembly Language :CSC 225 (Lec#4: Flag Register and Conditional Statements) By Dr. Syed Noman.
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
Conditional Processing
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
CS2422 Assembly Language and System Programming Conditional Processing Department of Computer Science National Tsing Hua University.
Flow Control Instructions
Conditional Processing If … then … else While … do; Repeat … until.
CS2422 Assembly Language & System Programming October 19, 2006.
Quiz #2 Topics Character codes Intel IA-32 architecture Mostly MASM
Conditional Processing Computer Organization & Assembly Language Programming Dr Adnan Gutub aagutub ‘at’ uqu.edu.sa [Adapted from slides of Dr. Kip Irvine:
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
Comparing and Branching ifs and loops part A. JMP instruction Consider the forever loop: for ( ; ; ) { … } How can we accomplish this in Assembler?
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.
Wednesday Feb 1 Project #1 Due Sunday, February 3 Quiz #2 Wednesday, February 6, in class Programming Project #2 is posted Due Sunday, February 10.
Dr. José M. Reyes Álamo 1.  Review: ◦ of Comparisons ◦ of Set on Condition  Statement Labels  Unconditional Jumps  Conditional Jumps.
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)
Logic Conditional Processing. Status flags - review The Zero flag is set when the result of an operation equals zero. The Carry flag is set when an instruction.
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.
Comparison Instructions Test instruction –Performs an implied AND operation between each of the bits in 2 operands. Neither operand is modified. (Flags.
CSC 221 Computer Organization and Assembly Language Lecture 20: Conditional and Block Structures.
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.
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 Week 10: Conditional Processing Slides modified by Dr. Osama Younes.
K.K. Leung Fall 2008Introductory Pentium Programming1 Pentium Architecture: Introductory Programming Kin K. Leung
CS2422 Assembly Language and System Programming 0 Week 13 & 14 Codes in Assembly Language.
CS-401 Computer Architecture & Assembly Language Programming
1 Chapter 6 Conditional Processing Assembly Language for Intel-Based Computers, 3rd edition Kip R. Irvine.
Assembly Language for x86 Processors 6th Edition
CSC 221 Computer Organization and Assembly Language
Assembly Language for Intel-Based Computers, 5th Edition
Chapter 6: Conditional Processing
Homework Reading Labs PAL, pp
CSC 221 Computer Organization and Assembly Language
Assembly Language for x86 Processors 7th Edition
Assembly Language for Intel-Based Computers, 4th Edition
EE3541 Introduction to Microprocessors
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Computer Architecture
INSTRUCTION SET.
More on logical instruction and
Assembly Language Programming Part 2
Microprocessor and Assembly Language
Computer Organization and Assembly Language
Flags Register & Jump Instruction
فصل پنجم انشعاب و حلقه.
Program Logic and Control
Program Logic and Control
Homework Reading Machine Projects Labs PAL, pp
Flow Control Instructions
EECE.3170 Microprocessor Systems Design I
Computer Organization and Assembly Language
Assembly Language for Intel 8086 Jump Condition
Chapter 7 –Program Logic and Control
Carnegie Mellon Ithaca College
Chapter 7 –Program Logic and Control
Computer Architecture and Assembly Language
Presentation transcript:

CS2422 Assembly Language & System Programming October 17, 2006

Today’s Topics Conditional Processing –If…then…else –While…do; Repeat…until

Study Guide Suggested Order: –Section 6.1: Introduction –Section : Conditional Jumps –Section 6.5: Conditional Structures –Section 6.2: Boolean Operations (Optional): Sections 6.3.5, 6.4, 6.6, 6.7

CMP and Jcond Instruction The IF statement in C and PASCAL is converted into CMP and Jcond instructions in x86 Assembly: CMP X, op1 JNG EndIf EndIf: If (X > op1) Then End If

CMP Instruction (1 of 3) Compares the destination operand to the source operand –Nondestructive subtraction of source from destination (destination operand is not changed) Syntax: CMP destination, source Example: destination == source mov al,5 cmp al,5; Zero flag set

CMP Instruction (2 of 3) Example: destination > source mov al,6 cmp al,5; ZF = 0, CF = 0 (both the Zero and Carry flags are clear) The comparisons shown so far were unsigned.

CMP Instruction (3 of 3) Example: destination > source mov al,5 cmp al,-2 ; Sign flag = Overflow flag The comparisons shown here are performed with signed integers. Example: destination < source mov al,-1 cmp al,5 ; Sign flag != Overflow flag

J cond Instruction A conditional jump instruction branches to a label when specific register or flag conditions are met Examples: –JB, JC jump to a label if the Carry flag is set –JE, JZ jump to a label if the Zero flag is set –JS jumps to a label if the Sign flag is set –JNE, JNZ jump to a label if the Zero flag is clear –JECXZ jumps to a label if ECX equals 0

Jumps Based on Specific Flags

Jumps Based on Equality

Jumps Based on Unsigned Comparisons

Jumps Based on Signed Comparisons

More Frequently Used Jcond Instructions JE (Equal) JNE (Not Equal) JG or JGE (Greater Than or Equal) JL or JLE (Less Than or Equal) Note: JG=JNLE, JGE=JNL, …etc.

Simple IF If (op1=op2) then end if Two different approaches: CMP op1, op2 JE True JMP EndIf True: EndIf CMP op1, op2 JNE False False:

IF … AND … CMP X, op1 JNG EndIf CMP Y, op2 JNLE EndIf CMP … … EndIf: If (X > op1)and (Y <=op2)and … Then End If

IF … OR … CMP X, op1 JG True CMP Y, op2 JLE True CMP … … JMP EndIf True: EndIf: If (X > op1) or (Y <=op2) or … Then End If

Exercise Can you rewrite the last slide and remove the “JMP EndIf”? How about adding the “else” part?

A Strategy for Compound Conditions Figure out the “longest path” of the program. Then find the “short cuts” (or early jumps).

WHILE While: CMP op1, op2 JNL EndDo JMP While EndDo: DO WHILE(op1<op2) END DO

REPEAT UNTIL repeat: CMP X, op1 JE EndIf CMP Y, op2 JNG repeat EndIf: REPEAT UNTIL(X = op1) or (Y > op2)

More Exercises Write the code for the CASE statement. AND conditions in REPEAT-UNTIL? repeat: CMP ? ? EndIf: REPEAT UNTIL (X=op1) and (Y>op2)

Flags and Jcond How do Jcond instructions decide which way to go? They check the flags! Examples: –JE/JNE checks Zero flag. –JG/JL checks Sign flag. CMP instruction sets the flags.

Boolean and Comparison Instructions AND Instruction OR Instruction XOR Instruction NOT Instruction TEST Instruction CMP Instruction

AND Instruction Performs a Boolean AND operation between each pair of matching bits in two operands Syntax: AND destination, source (same operand types as MOV) AND

OR Instruction Performs a Boolean OR operation between each pair of matching bits in two operands Syntax: OR destination, source OR

XOR Instruction Performs a Boolean exclusive-OR operation between each pair of matching bits in two operands Syntax: XOR destination, source XOR XOR is a useful way to toggle (invert) the bits in an operand.

NOT Instruction Performs a Boolean NOT operation on a single destination operand Syntax: NOT destination NOT

Application – An Example mov al,'a‘ ; AL = b and al, b ; AL = b Task: Convert the character in AL to upper case. Solution: Use the AND instruction to clear bit 5.

TEST Instruction Performs a nondestructive AND operation between each pair of matching bits in two operands No operands are modified, but the Zero flag is affected. Example: jump to a label if either bit 0 or bit 1 in AL is set. test al, b jnz ValueFound