Chapter six of V4: The String Instructions

Slides:



Advertisements
Similar presentations
Princess Sumaya Univ. Computer Engineering Dept. Chapter 9:
Advertisements

INSTRUCTION SET ARCHITECTURES
There are two types of addressing schemes:
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Computer Organization And Assembly Language
Gursharan Singh Tatla 21-Nov-20101www.eazynotes.com.
8086 : INSTRUCTION SET By, Pramod Sunagar Assistant Professor
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Introduction to Computer Engineering by Richard E. Haskell Register Indirect Addressing Module M18.2 Section 12.3.
8-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Assembly Language for Intel-Based Computers
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
Topic – string – Ch. 11 [Marut] Ch. 4 [Brey] String Data Transfer Instructions – The Direction Flag – LODS Instructions – STOS Instructions – MOVS Instructions.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
3.7 String Instructions Specifying the Operands’ Size and Address and the String Direction STRING = a data collection in memory. String ELEMENTS can be:
11.1/36 Repeat: From Bits and Pieces Till Strings.
Dr. José M. Reyes Álamo 1.  Review: ◦ Statement Labels ◦ Unconditional Jumps ◦ Conditional Jumps.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Today's topics Multi-dimensional arrays Multi-dimensional arrays String processing String processing Macros Macros.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Strings, Procedures and Macros
ICS312 Lecture13 String Instructions.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Addressing Modes of 8086 Processor Ammar Anwar Khan Electrical Engineer King Saud University Riyadh Saudi Arabia.
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.
String Instructions String instructions were designed to operate on large data structures. The SI and DI registers are used as pointers to the data structures.
Chapter 10 Instruction Sets: Characteristics and Functions Felipe Navarro Luis Gomez Collin Brown.
String Processing Chapter 10 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
1 The Stack and Procedures Chapter 5. 2 A Process in Virtual Memory  This is how a process is placed into its virtual addressable space  The code is.
String Instructions String instructions were designed to operate on large data structures. The SI and DI registers are used as pointers to the data structures.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Assembly 09. Outline Strings in x86 esi, edi, ecx, eax stosb, stosw, stosd cld, std rep loop 1.
Arrays. Outline 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Assembly Language Data Movement Instructions. MOV Instruction Move source operand to destination mov destination, source The source and destination are.
Khaled A. Al-Utaibi  Introduction  The MOV Instruction  The LEA Instruction  The Stack Instructions  The String Data Transfer.
Lecture 6 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
Chapter 8 String Operations. 8.1 Using String Instructions.
Computer Architecture. Instruction Set “The collection of different instructions that the processor can execute it”. Usually represented by assembly codes,
Stack Operations Dr. Hadi AL Saadi.
Chapter Nov-2010
Assembly Lab 3.
Assembly 07 String Processing.
BYTE AND STRING MANIPULATON
Chapter 9.
Aaron Miller David Cohen Spring 2011
Chapter 4 Data Movement Instructions
A Closer Look at Instruction Set Architectures
EE3541 Introduction to Microprocessors
INSTRUCTION SET.
Assembly Lang. – Intel 8086 Addressing modes – 1
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
Chapter 4 Data Movement Instructions
Morgan Kaufmann Publishers Computer Organization and Assembly Language
32-bit instruction mode(80386-Pentium 4 only)
Symbolic Instruction and Addressing
Practical Session 4.
Symbolic Instruction and Addressing
Assembly Language for Intel-Based Computers, 5th Edition
T opic: S TRING I NSTRUCTION P RESENTED B Y: N OOR FATIMA M AHA AKRAM ASIF.
X86 Assembly Review.
Chapter 5 Arithmetic and Logic Instructions
Data Movement Instructions
Chapter 6 –Symbolic Instruction and Addressing
CNET 315 Microprocessor & Assembly Language
CSC 497/583 Advanced Topics in Computer Security
Computer Organization and Assembly Language
Computer Architecture and System Programming Laboratory
Presentation transcript:

Chapter six of V4: The String Instructions YAN Hongfei Yhf@net.cs.pku.edu.cn Network Group Spring 2003 In this chapter we took a quick look at the 80x86's string instructions. We studied their implementation and saw how to use them. These instructions are quite useful for synthesizing character set functions (see the source code for the HLA Standard Library string module for examples). We also saw how to use these instructions for non-character string purpose such as moving large blocks of memory (i.e., assigning one array to another) and comparing large integer values.

Chapter Overview A string is a collection of objects stored in contiguous memory locations. Strings are usually arrays of bytes, words, or (on 80386 and later processors) double words. The 80x86 CPUs can process three types of strings: byte strings , word strings, and double word strings. They can move strings, compare strings, search for a specific value within a string, initialize a string to a fixed value, and do other primitive operations on strings.

The 80x86 String Instructions five different string instructions: MOVSx, CMPSx, SCASx, LODSx, and STOSx. (x= B, W, or D for byte, word, or double word, respectively)

How the String Instructions Operate operate on blocks (contiguous linear arrays) of memory. often require three operands, a destination block address, a source block address, and (optionally) an element count. The operands for the string instructions include the ESI (source index) register, the EDI (destination index) register, the ECX (count) register, the AL/AX/EAX register, and the direction flag in the FLAGS register. 1)MOVS copies a string from the source address specified by ESI to the destination address specified by EDI, of length ECX. 2)Likewise, the CMPS instruction compares the string pointed at by ESI, of length ECX, to the string pointed at by EDI. 3)SCAS instruction compares the value in the accumulator (AL, AX, or EAX) to values in memory.

The REP/REPE/REPZ and REPNZ/REPNE Prefixes When specifying the repeat prefix before a string instruction, the string instruction repeats ECX times. rep.movsx(); repe.cmpsx(); repe.scasx(); Without the repeat prefix, the instruction operates only on a single byte, word, or double word. (x= B, W, or D for byte, word, or double word, respectively)

The Direction Flag the direction flag in the flags register controls how the CPU processes strings. If the direction flag is clear, the CPU increments ESI and EDI after operating upon each string element. If the direction flag is set, then the 80x86 decrements ESI and EDI after processing each string element. Note: The first is always to insert the CLD or STD instructions immediately before executing a sequence of one or more string instructions. The other alternative is to save and restore the direction flag using the PUSHFD and POPFD instructions.

The MOVS Instruction fetches the byte/word/dword at address ESI, stores it at address EDI and then increments or decrements the ESI and EDI registers by one/two/four. CharArray1: byte[ 384 ]; CharArray2: byte[ 384 ]; . . . cld(); lea( esi, CharArray1 ); lea( edi, CharArray2 ); mov( 384, ecx ); rep.movsb(); WordArray1: word[ 384 ]; WordArray2: word[ 384 ]; . . . cld(); lea( esi, WordArray1 ); lea( edi, WordArray2 ); mov( 384, ecx ); rep.movsw(); Note, the ECX register contains the element count, not the byte count. When using the MOVSW instruction, the CPU moves the number of words specified in the ECX register.

lea( esi, CharArray1[383] ); lea( edi, CharArray2[383] ); Set the direction flag before executing a MOVSB/MOVSW/MOVSD instruction CharArray1: byte[ 384 ]; CharArray2: byte[ 384 ]; . . . std(); lea( esi, CharArray1[383] ); lea( edi, CharArray2[383] ); mov( 384, ecx ); rep.movsb();

Copying Data Between Two Overlapping Arrays 1/2 CharArray1: byte; CharArray2: byte[ 384 ]; . . . cld(); lea( esi, CharArray1 ); lea( edi, CharArray2 ); mov( 384, ecx ); rep.movsb();

Copying Data Between Two Overlapping Arrays 2/2 Setting the direction flag when the source string begins at a lower address than the destination string. Clearing the direction flag when the source string begins at a higher address than the destination string.

Ex1: efficiently copy 4099 bytes lea( esi, Source ); lea( edi, Destination ); mov( 1024, ecx ); // Copy 1024 dwords = 4096 bytes rep.movsd(); movsw(); // Copy bytes 4097 and 4098. movsb(); // Copy the last byte.

Ex2: efficiently copy ? bytes lea( esi, Source ); lea( edi, Dest ); mov( Length, ecx ); shr( 2, ecx ); if( @nz ) then rep.movsd(); endif; and( %11, ecx ); // Compute (Length mod 4). if( @nz ) then // Only execute MOVSB if #bytes/4 <> 0. rep.movsb();

ECX contains the length of the two strings The CMPS Instruction The CMPS instruction compares two strings. The CPU compares the string referenced by EDI to the string pointed at by ESI. ECX contains the length of the two strings mov( AdrsStr1, esi ); mov( AdrsStr2, edi ); mov( LengthSrc, ecx ); if( ecx > LengthDest ) then // Put the length of the shorter string in ECX. mov( LengthDest, ecx ); endif; repe.cmpsb(); if( @z ) then // If equal to the length of the shorter string, cmp lengths. cmp( ecx, LengthDest );

Use the CMPS instruction to compare multi-word integer values //to compare two 32-byte (256-bit) integer values, use the following code on the 80x86 std(); lea( esi, SourceInteger[28] ); lea( edi, DestInteger[28] ); mov( 8, ecx ); rep.cmpsd(); keep in mind with using the CMPS instruction - the value in the ECX register determines the number of elements to process, not the number of bytes.

The SCAS Instruction The source operand is the value in the accumulator. The SCAS instruction compares the value in the accumulator against the value pointed at by EDI and then increments (or decrements) EDI by one, two, or four. The CPU sets the flags according to the result of the comparison.

The SCAS instructions forms: scasx() repe.scasx() repne.scasx() The SCAS instruction REPE searches through the string for the first element which does not match the value in the accumulator. The SCAS instruction with REPNE scans through the string while the accumulator is not equal to the string operand.

The STOS Instruction The STOS instruction stores the value in the accumulator at the location specified by EDI. After storing the value, the CPU increments or decrements EDI depending upon the state of the direction flag. its primary use is to initialize arrays and strings to a constant value. stosb(); stosw(); stosd(); rep.stosb(); rep.stosw(); rep.stosd();

STOS example //Ex: if you have a 256-byte array you want to clear out with zeros, use the following code cld(); lea( edi, DestArray ); mov( 64, ecx ); // 64 double words = 256 bytes. xor( eax, eax ); // Zero out EAX. rep.stosd();

The LODS Instruction copies the byte, word, or double word pointed at by ESI into the AL, AX, or EAX register, after which it increments or decrements the ESI register by one, two, or four. Repeating this instruction via the repeat prefix would serve no purpose whatsoever lodsb(); lodsw(); lodsd(); rep.lodsb(); rep.lodsw(); rep.lodsd();

Building Complex String Functions from LODS and STOS //converts all the upper case characters in a string to lower case. mov( StringAddress, esi ); // Load string address into ESI. mov( esi, edi ); // Also point EDI here. mov( (type str.strrec [esi].length, ecx ); repeat lodsb(); // Get the next character in the string. if( al in 'A'..'Z' ) then or( $20, al ); // Convert upper case character to lower case. endif; stosb(); // Store converted character back into string. dec( ecx ); until( ecx == 0 );

Thank you for your attention!