Assembly 06. Outline cmp (review) Jump commands test mnemonic bt mnemonic Addressing 1.

Slides:



Advertisements
Similar presentations
Jump Condition.
Advertisements

Departemen Ilmu Komputer FMIPA IPB Conditional Jump Instruction A conditional jump instruction transfer control to a destination address when a.
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
NEG Instruction Change operand content into two’s complement (negative value) and stored back into its operand mov bl, b neg bl; bl = mov.
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
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.
Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1.
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.
Flow Control Instructions
Conditional Processing If … then … else While … do; Repeat … until.
Quiz #2 Topics Character codes Intel IA-32 architecture Mostly MASM
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Introduction to Assembly Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#5) By Dr. Syed Noman.
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)
Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice.
Decision Structures – Code Generation Lecture 24 Mon, Apr 18, 2005.
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.
Assembly 03. Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 1.
Assembly 05. Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 1.
Overview of Assembly Language Chapter 4 S. Dandamudi.
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.
Review of Assembly language. Recalling main concepts.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Conditional Loop Instructions, Conditional Structures
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
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.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Assembly 09. Outline Strings in x86 esi, edi, ecx, eax stosb, stosw, stosd cld, std rep loop 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.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Selection and Iteration Chapter 8 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
K.K. Leung Fall 2008Introductory Pentium Programming1 Pentium Architecture: Introductory Programming Kin K. Leung
CS-401 Computer Architecture & Assembly Language Programming
Practical Session 2 Computer Architecture and Assembly Language.
Computer Architecture and Assembly Language
CSC 221 Computer Organization and Assembly Language
Introduction to assembly programmıng language
Computer Architecture and Assembly Language
Homework Reading Labs PAL, pp
Practical Session 2.
Instruksi Set Prosesor 8088
EE3541 Introduction to Microprocessors
Computer Architecture
Assembly IA-32.
More on logical instruction and
Assembly Language Programming Part 2
Processor Processor characterized by register set (state variables)
Computer Organization and Assembly Language
(Array and Addressing Modes)
Shift & Rotate Instructions)
Homework Reading Machine Projects Labs PAL, pp
(Array and Addressing Modes)
Shift & Rotate Instructions)
Flow Control Instructions
EECE.3170 Microprocessor Systems Design I
Assembly Language for Intel 8086 Jump Condition
Carnegie Mellon Ithaca College
Chapter 8: Instruction Set 8086 CPU Architecture
Computer Architecture and Assembly Language
(Array and Addressing Modes)
Presentation transcript:

Assembly 06

Outline cmp (review) Jump commands test mnemonic bt mnemonic Addressing 1

cmp (Review) cmp command compares two operands cmp, cmp works by subtracting right from left and temporarily storing the result (hidden from you) result = left – right 2

cmp (Review) cmp, result = left – right if left == right ---> result = 0 if left > right ---> result > 0 if left result < 0 3 keep this in mind…

Jump Commands Many jump commands jmp, je, ja, jb, jnae, … Conditional jumps examine flag(s) to decide what to do Placed immediately after cmp instruction If jump condition not met, execution “falls through” to next instruction jmp mnemonic always jumps to label e.g., jmp DONE;will jump to DONE label 4

jump Commands 5 condition operato r unsigne d values jumps when signed values jumps when equal==jeZF==1jeZF==1 not equal!=jneZF==0jneZF==0 greater than>ja CF==0 and ZF==0 jg ZF==0 or SF==OF less than<jbCF==1jlSF != OF greater than equal to >=jaeCF==0jgeSF==OF less than equal to<=jbe CF==1 or ZF==1 jle ZF==1 or SF != OF ZF: zero flag SF: sign flag CF: carry flag OF: overflow flag

jump Commands 6 condition operato r unsigne d values jumps when signed values jumps when equal==jeZF==1jeZF==1 not equal!=jneZF==0jneZF==0 greater than>ja CF==0 and ZF==0 jg ZF==0 or SF==OF less than<jbCF==1jlSF != OF greater than equal to >=jaeCF==0jgeSF==OF less than equal to<=jbe CF==1 or ZF==1 jle ZF==1 or SF != OF ZF: zero flag SF: sign flag CF: carry flag OF: overflow flag

jump Commands je -> “jump if equal” jumps when ZF==1 cmp, result = left – right if left == right, result = 0 ZF set! 7

jump Commands jne -> “jump if not equal” jumps when ZF==0 cmp, result = left – right if left != right, result != 0 ZF clear! 8

jump Commands Encourage you to think about other conditions What happens to flags during cmp instruction? How does that relate to the logical operation? cmp, result = left - right 9

jump Commands Conditional jumps have synonyms: Greater than or equal to not less than 10

jump Commands 11 mnemoni c meaning synony m meaning jaJump if AbovejnbeJump if Not Below or Equal jaeJump if Above or EqualjnbJump if Not Below jbJump if BelowjnaeJump if Not Above or Equal jbeJump if Below or EqualjnaJump if Not Above jeJump if EqualjzJump if Zero jneJump if Not EqualjnzJump if Not Zero jgJump if GreaterjnleJump if Not Less or Equal jge Jump if Greater or Equal jnlJump if Not Less jlJump if LessjngeJump if Not Greater or Equal jleJump if Less or EqualjngJump if Not Greater

jump Commands 12 mnemoni c meaning synony m meaning jaJump if AbovejnbeJump if Not Below or Equal jaeJump if Above or EqualjnbJump if Not Below jbJump if BelowjnaeJump if Not Above or Equal jbeJump if Below or EqualjnaJump if Not Above

jump Commands 13 mnemoni c meaning synony m meaning jeJump if EqualjzJump if Zero jneJump if Not EqualjnzJump if Not Zero

jump Commands 14 mnemoni c meaning synony m meaning jgJump if GreaterjnleJump if Not Less or Equal jge Jump if Greater or Equal jnlJump if Not Less jlJump if LessjngeJump if Not Greater or Equal jleJump if Less or EqualjngJump if Not Greater

jump Commands msg: db 10; in.data len: equ $-msg; in.data ; system call to print msg to stdout (not shown) ; for each character in msg (loop code not shown) cmp byte [msg + eax], compare byte to jne _skip; if byte != jump to _skip mov byte [msg + eax], ‘l’; if byte == replace with ‘l’ ; _skip label to continue loop.. (not shown) ; system call to print msg to stdout (not shown) ; system call to exit gracefully (not shown) 15

jump Commands UNIX>./example Hello World!! UNIX> 16

test Mnemonic Bit testing common in assembly test ==> cmp instruction for bits test performs logical AND operation between operands test does NOT modify destination operand test, 17

test Mnemonic test only good at finding one 1, not 0s if test “passes”, ZF=0 if test “fails”, ZF=1 18

test Mnemonic mov al, b; mov bl, b; test al, bl; 19 al bl ZF

test Mnemonic mov al, b; mov bl, b; test al, bl; al bl ZF

test Mnemonic mov al, b; mov bl, b; test al, bl; al bl ZF

test Mnemonic mov al, b; mov bl, b; test al, bl; al bl ZF

test Mnemonic mov al, b; mov bl, al; test al, bl; 23 al bl ZF

test Mnemonic mov al, b; mov bl, al; test al, bl; al bl ZF

test Mnemonic mov al, b; mov bl, al; test al, bl; al bl ZF

test Mnemonic mov al, b; mov bl, al; test al, bl; al bl ZF

bt Mnemonic bt -> bit test Used to examine a single bit Can test for 1 OR 0 Modifies CF depending on bit bt, 27

bt Mnemonic bt, bit # specifies the actual “bit number” LSB is 0 th bit if bit at bit # is 1, CF = 1 if bit at bit # is 0, CF = 0 Use jnc (jump if not carry) after bt 28

bt Mnemonic mov al, b; bt eax, 0; check 0 th bit bt eax, 4; check 4 th bit 29

bt Mnemonic mov al, b bt eax, 0 bt eax, 4 30 al CF

bt Mnemonic mov al, b bt eax, 0 bt eax, al CF

bt Mnemonic mov al, b bt eax, 0 bt eax, al CF

bt Mnemonic mov al, b bt eax, 0 bt eax, al CF

Addressing Book claims that: “memory addressing is the key skill in assembly language programming” Book goes into way too much detail about addressing Displacement, index, offset, base, scale, … (yada-yada-yada) Focus on what you need to know How to access different data types in arrays 34

Addressing Data access for byte arrays: array: db 8,6,7,5,3,0,9;in.data mov al, byte [array + 3]; in.text ; access byte at array[3] 35 array’s address index into array

Addressing Data access for byte arrays: array: db 8,6,7,5,3,0,9;in.data mov ebx, 3; mov al, byte [array + ebx]; in.text ; access byte at array[3] 36 array’s address index can be 32-bit register

Addressing Data access for byte arrays: array: db 8,6,7,5,3,0,9;in.data mov al, byte [array + 3]; in.text ; access byte at array[3] 37 index is in terms of number of bytes

Addressing [array + 3] = data at address (array + 3 bytes) array: index index is in terms of number of bytes

Addressing What happens when the array is another data type? E.g., 16-bit word or 32-bit dword? How do you access a data element? array of 16-bit items array of 32-bit items index

Addressing array: dw 0x0123, 0xABCD; in.data mov ax, [array]; in.text mov bx, [array + 1] mov cx, [array + 2] 40

Addressing array: dw 0x0123, 0xABCD; mov ax, [array] mov bx, [array + 1] mov cx, [array + 2] index array ax bx cx

Addressing array: dw 0x0123, 0xABCD; mov ax, [array] mov bx, [array + 1] mov cx, [array + 2] index array 0x0123 ax bx cx 01CDAB x86 is little endian

Addressing array: dw 0x0123, 0xABCD; mov ax, [array] mov bx, [array + 1] mov cx, [array + 2] index array 0x0123 ax 0xCD01 bx cx 01CDAB x86 is little endian

Addressing array: dw 0x0123, 0xABCD; mov ax, [array] mov bx, [array + 1] mov cx, [array + 2] index array 0x0123 ax 0xCD01 bx 0xABCD cx 01CDAB x86 is little endian

Addressing array: dw 0x0123, 0xABCD; mov ax, [array] mov bx, [array + 1] mov cx, [array + 2] index array 0x0123 ax 0xCD01 bx 0xABCD cx 01CDAB What happened?? Shouldn’t array[1] = 0xABCD?

Addressing In assembly, the array’s index is based on number of bytes Previous example, array contains 16-bit (2-byte) data items Hence why [array + 2] produced the “second” 16-bit data item mov cx, [array + 2] 46 0xABCD cx 23 array 01CDAB

Addressing An array’s index is based on number of bytes Also explains outcome: mov bx, [array + 1] 47 0xCD01 bx 23 array 01CDAB

Addressing So, how do we address this? (pun intended) We scale the index: For byte arrays, multiply index by 1 (implicit) For word arrays, multiply index by 2 For dword arrays, multiply index by 4 mov cx, [array + 1*2] == mov cx, [array + 2] 48

Addressing array: dd 32768, 42991, 23486, 77877;in.data mov eax, [array + ???];in.text ; want to access data item ; at index 3. How?? 49

Addressing array: dd 32768, 42991, 23486, 77877;in.data mov eax, [array + 3*4];in.text ; want to access data item ; at index 3. How?? 50

Addressing It’s convenient to declare scale as constant in.data array: dd 32768, 42991, 23486, scale: equ 4; 4 bytes for dwords. mov eax, [array + 3*scale]; array[3] 51

Translational Tables and xlat Mnemonic Encourage you to read pages Translational tables can be convenient xlat command can also be convenient Uses implicit operands… 52