1 Homework Reading –PAL, pp 127-152 Machine Projects –MP2 due at start of Class 12 Labs –Continue labs with your assigned section.

Slides:



Advertisements
Similar presentations
Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Advertisements

INSTRUCTION SET ARCHITECTURES
Deeper Assembly: Addressing, Conditions, Branching, and Loops
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.
TK 2633 Microprocessor & Interfacing Lecture 3: Introduction to 8085 Assembly Language Programming (2) 1 Prepared By: Associate Prof. Dr Masri Ayob.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Assembly Language for Intel-Based Computers
Flow Control Instructions
Assembly Language for Intel-Based Computers Chapter 2: IA-32 Processor Architecture Kip Irvine.
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
1 Homework Reading –Professional Assembly Language, pp 17-32, Continue work on mp1 –Questions? Lab with your assigned section this week.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 1 Flow Control.
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.
Chapter 4 - Implementing Standard Program Structures in 8086 Assembly Language from Microprocessors and Interfacing by Douglas Hall.
Dr. José M. Reyes Álamo 1.  Review: ◦ Statement Labels ◦ Unconditional Jumps ◦ Conditional Jumps.
CE302 MICROPROCESSORS Levent EREN Izmir University of Economics.
Lecture 4 ( Assembly Language).
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.
Arithmetic Flags and Instructions
(Flow Control Instructions)
1 ICS 51 Introductory Computer Organization Fall 2009.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
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.
Computer Systems – Machine & Assembly code. Objectives Machine Code Assembly Language Op-code Operand Instruction Set.
3.4 Addressing modes Specify the operand to be used. To generate an address, a segment register is used also. Immediate addressing: the operand is a number.
תרגול 5 תכנות באסמבלי, המשך
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.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Jumps, Loops and Branching. Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Machine-Level Programming 2 Control Flow
Data Transfers, Addressing, and Arithmetic
Homework Reading Labs PAL, pp
3.Instruction Set of 8085 Consists of 74 operation codes, e.g. MOV
Lecture Set 5 The 8051 Instruction Set.
Aaron Miller David Cohen Spring 2011
Microprocessor and Assembly Language
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Computer Architecture
Assembly IA-32.
Assembly Language Lab (4).
More on logical instruction and
Assembly Language Programming Part 2
Homework Reading Continue work on mp1
Computer Architecture adapted by Jason Fritts then by David Ferry
Flags Register & Jump Instruction
Assembly Language: IA-32 Instructions
Machine-Level Programming 2 Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Representation of Programs III
Machine-Level Programming 2 Control Flow
ECE 3430 – Intro to Microcomputer Systems
Homework Reading Machine Projects Labs PAL, pp
Flow Control Instructions
CNET 315 Microprocessor & Assembly Language
EECE.3170 Microprocessor Systems Design I
Carnegie Mellon Ithaca College
CS201- Lecture 8 IA32 Flow Control
Chapter 8: Instruction Set 8086 CPU Architecture
Credits and Disclaimers
Computer Architecture and Assembly Language
Ch. 5 – Intel 8086 – study details from Yu & Marut
Presentation transcript:

1 Homework Reading –PAL, pp Machine Projects –MP2 due at start of Class 12 Labs –Continue labs with your assigned section

2 Jumps and Flow of Control In assembly language, there are NO “if-else”, “for”, “do”, or “do … while” statements as in C Must use some combination of conditional and unconditional “jump” instructions for if-else branching or looping Jump instruction is similar to a C “go to” Jump instruction is similar to “call” instruction, but it doesn’t push a return address via %esp

3 Jumps and Flow of Control When the processor is fetching and executing instructions, it follows this cycle: –Fetches an instruction from memory using %eip –Executes the instruction –Increments %eip to point to the next instruction When a jump is executed, the last step of the fetch and execute cycle may be the loading of a different value into the %eip instead of the address for the next instruction in sequence

4 Jumps and Flow of Control Because there are no “structured programming” statements in assembly language, it may not be possible to use pseudo-code for the design The design technique that best supports logic for an assembly language program is the flowchart The flow chart has circles that represent labels and arrows that represent go-to’s

5 Jumps and Flow of Control If-Else if (test) statement1; else statement2; test One or more flags are set or reset statement1 True False Conditional Jump statement2 Unconditional Jump

6 Jumps and Flow of Control If-else in assembly code: cmpl$0, %eax# test value of eax for zero jnz else...# statement1 jmpend# and jump over statement2 else:# just a label …# statement2 end: …# next instruction after if-else

7 Jumps and Flow of Control Iterative Loop while (test) { body; } test One or more flags are set or reset body Unconditional Jump True False Conditional Jump

8 Jumps and Flow of Control While loop in assembly code: movl$3, %eax# loop three times while:# note – just a label cmpl$0, %eax# test value of eax for zero jzend# exit if counted down to zero …# body of loop here subl$1, %eax# decrement eax jmpwhile# loop end: …# next instruction after loop

9 Unconditional Jumps “Unconditional jump” always loads %eip with a new value: –Hard coded address jmp 0x10ec# hard coded address... –Label for address jmplabel# address of a label... label:

10 An Infinite Loop The following is an infinite loop based on a single unconditional jump instruction: movl $0, %eax movl $2, %ecx xyz: addl %ecx, %eax jmp xyz

11 Conditional Jumps “Conditional jump” may or may not load %eip with a new value When your code performs instructions, specific flags in %eflag may get set (=1) or reset (=0) Depends on the definition of the instruction: addb %bl, %al # affects zero and carry flags %al %bl %al Carry Flag 0 Zero Flag

12 Flags Flags are set by arithmetic or logical instructions: –Carry Flag – Set by a carry out / borrow in at MSB –Zero Flag – Set if entire byte, word, or long == 0 –Sign Flag – Set if sign bit == 1 –Parity Flag – Set if 8 LSB’s contain an even number of 1’s –Overflow Flag – Set by a carry into sign bit w/o a carry out –Auxiliary Carry Flag – Set by a carry / borrow in 4 LSBs These flags are individual bits in the %eflag register Specific flag settings control the behavior of specific conditional jump instructions

13 Conditional Jumps Operation of conditional jump: If (state of specific flags) Load a new value based on operand into %eip Else Let the %eip be incremented to next sequential instruction Examples: jzlabel # if zero flag is set jslabel # if sign flag is set jnzlabel # if zero flag not set jnslabel # if sign flag not set... label:

14 Conditional Jumps Be careful about the meaning of flag bits! C code: if (al < bl) eax = 1; else eax = 0; /* compute boolean value */ Gas code (buggy): # assume values already in %al and %bl subb %bl, %al # set/reset sign flag js sib # jump if sign flag set movl$0, %eax # %al is bigger or = jmpend # don’t fall through sib: movl$1, %eax # %bl is bigger end: ret # return value 0 or 1 Bug is ignoring overflow flag!

15 Signed Comparisons Is it true?: A < B if and only if A – B is negative Not with fixed register sizes that can overflow! Example test in signed character (1 byte) arithmetic: Is 100 < -50? No, but (-50) = -106 (Due to overflow!) (Add two’s compliment of -50) (Sets sign flag and sets overflow flag) Note: Carry into sign bit without a carry out  Set overflow flag!

16 Signed Comparisons If overflow occurs, the sign flag value will be the opposite of what it should be! So we need our jump condition to be: –If overflow flag == 0, jump if sign flag == 1 –If overflow flag == 1, jump if sign flag == 0 Same as: –Jump if (sign flag XOR overflow flag) == 1 –Hence, useful Intel instruction “jump less than”: jl label# jump if (SF xor OV) is set

17 Signed Comparisons Proper interpretation of flag bits! C code: if (al < bl) eax = 1; else eax = 0; /* compute boolean value */ Gas code (bug fixed for SIGNED data): # assume values already in %al and %bl subb %bl, %al # set/reset sign flag jl sib # jump less than movl$0, %eax # %al is bigger or = jmpend # don’t fall through sib: movl$1, %eax # %bl is bigger end: ret # return value 0 or 1

18 Signed Comparisons Compare Command –Sets the flags according to a subtraction –Does not save the result of the subtraction –Does not overwrite values in the registers being compared (just sets the flag bits)

19 Signed Comparisons Proper interpretation of flag bits! C code: if (al < bl) eax = 1; else eax = 0; /* compute boolean value */ Gas code (using cmpb instead of subb): # assume values already in %al and %bl cmpb %bl, %al # set/reset flags jl sib # jump less than movl$0, %eax # %al is bigger or = jmpend # don’t fall through sib: movl$1, %eax # %bl is bigger end: ret # return value 0 or 1

20 Conditional Jumps (Signed) JumpCondition –jlless than –jleless than or equal –jggreater than –jgegreater than or equal –jeequal –jnccNOT of each of the above conditions

21 Unsigned Comparisons Is it true?: A < B if and only if A – B is “negative” Carry Flag will indicate underflow –Example test in unsigned character arithmetic: –Is 100 < 206? (206 = same bits as -50 was before) –Yes (because now the “sign bit” is 2 7 ) (Add two’s compliment of 206) (Underflows = goes below zero) Note: Underflow is a “Carry Error”  Set Carry flag!

22 Unsigned Comparisons Meaning of the carry flag is reversed A carry means a correct positive result after an unsigned subtraction, so carry flag = 0 If underflow occurs, the carry flag = 1 will be indicator of an unsigned “negative” result! So we need our jump condition to be: –If carry == 1, jump –If carry == 0, don’t jump Hence, useful Intel instruction “jump below”: jb label# jump if CF is set

23 Unsigned Comparisons Proper interpretation of flag bits! C code: if (al < bl) eax = 1; else eax = 0; /* compute boolean value */ Gas code (bug fixed for UNSIGNED data): # assume values already in %al and %bl cmpb %bl, %al # set/reset carry flag jb sib # jump below movl$0, %eax # %al is bigger or = jmpend # don’t fall through sib: movl$1, %eax # %bl is bigger end: ret # return value 0 or 1

24 Conditional Jumps (Unsigned) JumpCondition –jbbelow –jbebelow or equal –jaabove –jaeabove or equal –je *equal * –jncc NOT of each of the above conditions –* Note: Same instruction as signed jump

25 loop Instruction Loop instruction = Decrement, Test, and Jump Instruction explanation: Decrement %ecx If %ecx != 0 Jump to label (Back to beginning of loop) Else Continue in sequence (Ends the loop) Example: movl $0x0a, %ecx # loop 10 times label: (instructions in loop) loop label (next instruction after loop)

26 Scanning Pointer Problem Want to sum up the elements in an array of N elements.data iarray:.long1, 4, 9, 16 # n = 4 in example The code might look like this: _sumarray:xorl%eax, %eax# initial sum = 0 movl$4, %ecx# initial loop count movl$iarray,%edx# initial pointer value add1:addl(%edx), %eax# add in next element addl$4,%edx# bump pointer loopadd1# test and loop ret

27 inc and dec Instructions Incrementing and decrementing by one Useful inside loops C code: i++; or i--; /* i is a variable in memory! */ Gas incrementing and decrementing registers incl %eaxdecl %eax Gas incrementing and decrementing memory incl indexdecl index