Quiz #2 Topics Character codes Intel IA-32 architecture Mostly MASM

Slides:



Advertisements
Similar presentations
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
Advertisements

Deeper Assembly: Addressing, Conditions, Branching, and Loops
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Conditional Loop Instructions LOOPZ and LOOPE LOOPNZ.
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Assembly Language for Intel-Based Computers
CS2422 Assembly Language & System Programming October 17, 2006.
CS2422 Assembly Language and System Programming Conditional Processing Department of Computer Science National Tsing Hua University.
Flow Control Instructions
CS2422 Assembly Language & System Programming September 28, 2006.
1 Homework Reading –PAL, pp Machine Projects –MP2 due at start of Class 12 Labs –Continue labs with your assigned section.
Conditional Processing If … then … else While … do; Repeat … until.
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.
IA32 (Pentium) Processor Architecture. Processor modes: 1.Protected (mode we will study) – 32-bit mode – 32-bit (4GB) address space 2.Virtual 8086 modes.
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.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
1 ICS 51 Introductory Computer Organization Fall 2009.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
26-Nov-15 (1) CSC Computer Organization Lecture 6: Pentium IA-32.
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.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 06: Control Structures Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
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.
Conditional Loop Instructions, Conditional Structures
Assembly 06. Outline cmp (review) Jump commands test mnemonic bt mnemonic Addressing 1.
Fall 2012 Chapter 4: Data Transfers, Addressing, and Arithmetic.
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.
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 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
CS2422 Assembly Language and System Programming 0 Week 9 Data Transfers, Addressing, and Arithmetic.
CSC 221 Computer Organization and Assembly Language
Assembly Language for Intel-Based Computers, 5th Edition
Deeper Assembly: Addressing, Conditions, Branching, and Loops
Data Transfers, Addressing, and Arithmetic
Homework Reading Labs PAL, pp
Microprocessor and Assembly Language
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Assembly IA-32.
Assembly Language Lab (4).
INSTRUCTION SET.
More on logical instruction and
Assembly Language Programming Part 2
Microprocessor and Assembly Language
Processor Processor characterized by register set (state variables)
Assembly Language for Intel-Based Computers, 4th Edition
Introduction to Assembly Language
Computer Organization and Assembly Language
Data Transfers, Addressing, and Arithmetic
Program Logic and Control
Program Logic and Control
Homework Reading Machine Projects Labs PAL, pp
Flow Control Instructions
EECE.3170 Microprocessor Systems Design I
Chapter 7 –Program Logic and Control
CSC 497/583 Advanced Topics in Computer Security
Carnegie Mellon Ithaca College
Chapter 8: Instruction Set 8086 CPU Architecture
Chapter 7 –Program Logic and Control
Presentation transcript:

Quiz #2 Topics Character codes Intel IA-32 architecture Mostly MASM Registers, memory addressing Bytes, words, etc. Little-endian representation Floating-point unit Mostly MASM General form of a MASM program Directives (TITLE, INCLUDE, etc.) Segments (.code, .data, etc.) Declare variables, constants Comments Instruction format Instructions (mov, add, call, etc.) Trace MASM code Convert simple statements to MASM Assembling, linking, loading, etc.

Today’s topics More MASM programming Addressing modes Conditional control structures Decision Repetition

IA-32 Operand Modes Immediate constant or literal, OFFSET (memory address) Examples: PI equ 3.14159 size DWORD 10 myName BYTE ”Barney” mov eax, 10 mov edx, OFFSET myName Register register contents Examples: mov eax, 10 add eax, ebx mov size, eax Direct memory contents Examples: mov eax, size Others (later) Register indirect, Indexed, Base-indexed, Stack

Instruction Operand Notation

mov ecx,256 mov edx,OFFSET string Syntax Examples MOV mem,accum mov total,eax MOV accum,mem mov al,string Notes: accum means “eax or some valid part of eax” imm means “a literal or constant” Syntax Examples MOV mem,imm mov color,7 Syntax Examples MOV reg,imm mov ecx,256 mov edx,OFFSET string

mov dh,bh mov edx,ecx mov ebp,esp Syntax Examples MOV reg,reg mov dh,bh mov edx,ecx mov ebp,esp MOV mem,reg mov count,ecx MOV reg,mem mov ebx,pointer Notes: mem8 means “BYTE” mem16 means “WORD” mem32 means “DWORD” sreg means CS, DS, ES, FS, GS, or SS Syntax Examples MOV sreg,reg16 mov ds, ax MOV sreg,mem16 mov es,pos1 MOV reg16,sreg mov ax,ds MOV mem16,sreg mov stack_save,ss

Invalid MOV statements .data bVal BYTE 100 bVal2 BYTE ? wVal WORD 2 dVal DWORD 5 .code mov ds,45 mov esi,wVal mov eip,dVal mov 25,bVal mov bVal2,bVal immediate move to DS not permitted size mismatch EIP cannot be the destination immediate value cannot be destination memory-to-memory move not permitted

Branching execution Sometimes it is necessary to interrupt sequential instruction execution EIP is changed Examples: Skip ahead (e.g., skip the else block) Jump backwards (e.g., repeat a section of code) Call a procedure Conditional / Unconditional branching Label required

MASM Labels Same rules as other identifiers May not be any previously defined identifier Label definition ends with : Don’t use : when referencing the label Specifies the memory address of the associated instruction … just like a variable name Good practice to put label: on a separate line.

Unconditional branching Instruction format is jmp label label: should be inside the same procedure MASM allows jumps to labels in other procedures, but execution will certainly get lost in space.

Conditional branching Used for: if structures (decisions, alternation) loop structures (repetition, iteration) In general, MASM requires you to build your own control structures Note: in the following discussion, status bits (flags) are Set (means status bit is set to 1) Cleared (means status bit is set to 0)

loop instruction Instruction format is loop label label: should be inside the same procedure, before the loop instruction Used for counted loops. Implements a “for” loop. Conditional branch Decrements ecx, if ecx is not zero, branch to label Problem if ecx is changed inside the loop body Problem if ecx starts at 0, or ecx becomes negative Exercise great care when constructing nested “loop” loops

loop Example Find sum of integers from 1 to 10 ; initialize accumulator, first number, ; and loop control mov eax, 0 mov ebx, 1 mov ecx, 10 sumLoop: ; add numbers from 1 to 10 add eax, ebx inc ebx loop sumLoop ; Print result call WriteDec ; ...

Conditional branching We need a way to control branching by checking some other types of conditions Examples: Some repetitive tasks can not be counted in advance IF-THEN-ELSE structures MASM provides a way to compare two operands

CMP Instruction Compares the destination operand to the source operand Non-destructive subtraction of source from destination (destination operand is not changed) Syntax: CMP destination, source

CMP Instruction (unsigned) Example: destination is equal to source mov al,5 cmp al,5 ; Zero flag set

CMP Instruction (unsigned) Example: destination < source mov al,4 cmp al,5 ; Carry flag set

CMP Instruction (unsigned) Example: destination > source mov al,6 cmp al,5 ; ZF = 0, CF = 0 (both the Zero and Carry flags are cleared)

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

Conditional Jumps Jumps Based On . . . Specific flags Equality Unsigned comparisons Signed Comparisons

Jcond Instruction A conditional jump instruction branches to a label when specific register or flag conditions are met Usually the next instruction after cmp 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 cleared 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

Compare and Jump Task: Jump to a label if unsigned EAX is greater than EBX Solution: Use CMP, followed by JA cmp eax,ebx ja Larger Task: Jump to a label if signed EAX is greater than EBX Solution: Use CMP, followed by JG cmp eax,ebx jg Greater

Compare and Jump Jump to label L1 if unsigned EAX is less than or equal to Val1 cmp eax,Val1 jbe L1 ; below or equal Jump to label L1 if signed EAX is less than or equal to Val1 cmp eax,Val1 jle L1

Conditional Loop Instructions (Use after CMP) LOOPZ and LOOPE Syntax: LOOPE destination LOOPZ destination Logic: ECX  ECX – 1 if ECX > 0 and ZF=1, jump to destination

Conditional Loop Instructions (Use after CMP) LOOPNZ and LOOPNE Syntax: LOOPNZ destination LOOPNE destination Logic: ECX  ECX – 1; if ECX > 0 and ZF=0, jump to destination

Conditional Directives .IF, .ELSE, .ELSEIF, and .ENDIF .WHILE, .ENDW .REPEAT, .UNTIL Not required for this course It’s OK to use these in programming assignments, but you must know the “hard way” for exams and quizzes

Block-Structured IF Statements You can create assembly language control structures that are equivalent to statements written in C++/Java/etc.. Example: mov eax,op1 cmp eax,op2 jne L1 mov X,1 jmp L2 L1: mov X,2 L2: if( op1 == op2 ) X = 1; else X = 2;

Assembly Language Control Structures Extend the idea to create your own if-then if-then-else if-then-elseif-else compound conditions while loop do-while loop for loop nested structures, switch structures, etc.

if-then check condition using CMP if condition is false, jump to endThen (Note: test for complement of condition) code for TRUE block endThen:

if-then-else (Method 1) check condition using CMP if condition is false, jump to falseBlock (Note: test for complement of condition) code for TRUE block jump to endFalse falseBlock: code for FALSE block endFalse:

if-then-else (Method 2) check condition using CMP if condition is true, jump to trueBlock code for FALSE block jump to endTrue trueBlock: code for TRUE block endTrue:

if-then-elseif-else check condition1 using CMP if condition1 is true, jump to trueBlock1 check condition2 using CMP if condition2 is true, jump to trueBlock2 code for FALSE block jump to endBlock trueBlock1: code for TRUE block1 trueBlock2: code for TRUE block2 endBlock:

Compound conditions (AND) check condition1 using CMP if condition1 is false, jump to falseBlock check condition2 using CMP if condition2 is false, jump to falseBlock code for TRUE block jump to endBlock falseBlock code for FALSE block endBlock:

Compound conditions (OR) check condition1 using CMP if condition1 is true, jump to trueBlock check condition2 using CMP if condition2 is true, jump to trueBlock code for FALSE block jump to endBlock trueBlock code for TRUE block endBlock:

Pretest loop (while) initialize loop control variable(s) top: check condition using CMP if condition is false, jump to endWhile code for LOOP BODY (including loop control update) jump to top endWhile:

Example pre-test loop Double x until x>1000 ; initialize accumulator mov eax, x dblLoop: ; Double x while x <= 1000 cmp eax, 1000 ja endLoop add eax, eax jmp dblLoop endLoop: mov x, eax ; ...

Posttest loop (do-while) top: code for LOOP BODY (including loop control update) check condition using CMP if condition is true, jump to top

Example post-test loop Double x until x>1000 ; initialize accumulator mov eax, x dblLoop: ; Double x while x <= 1000 add eax, eax cmp eax, 1000 jbe dblLoop mov x, eax ; ... Note: may want initial test for x>1000

Various solutions Any control structure may be implemented in a variety of ways. Experiment!

Questions? Quiz #2 Thursday Program #2 is assigned Learn the MASM instructions! Experiment! Experiment!! Experiment!!!