Chapter 8 String Operations. 8.1 Using String Instructions.

Slides:



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

C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Gursharan Singh Tatla 21-Nov-20101www.eazynotes.com.
8086 : INSTRUCTION SET By, Pramod Sunagar Assistant Professor
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 Chapter 5: Procedures Kip R. Irvine.
TK 2633 Microprocessor & Interfacing Lecture 3: Introduction to 8085 Assembly Language Programming (2) 1 Prepared By: Associate Prof. Dr Masri Ayob.
Accessing parameters from the stack and calling functions.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
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
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 5 Arithmetic and Logic Instructions.
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 4 Data Movement Instructions by.
8051 ASSEMBLY LANGUAGE PROGRAMMING
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
CDP ECE Spring 2000 ECE 291 Spring 2000 Lecture 7: More on Addressing Modes, Structures, and Stack Constantine D. Polychronopoulos Professor, ECE.
3.7 String Instructions Specifying the Operands’ Size and Address and the String Direction STRING = a data collection in memory. String ELEMENTS can be:
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
11.1/36 Repeat: From Bits and Pieces Till Strings.
CSC 221 Computer Organization and Assembly Language
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.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Today's topics Multi-dimensional arrays Multi-dimensional arrays String processing String processing Macros Macros.
String-Introduction String is a series of bytes or a series of words in sequential memory locations. Index registers - SI (Data segment) - DI (Extra segment)
Strings, Procedures and Macros
ICS312 Lecture13 String Instructions.
26-Nov-15 (1) CSC Computer Organization Lecture 6: Pentium IA-32.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
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.
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.
String Processing Chapter 10 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Assembly 03. Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 1.
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.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
8086/8088 Instruction Set, Machine Codes and Addressing Modes.
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 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.
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.
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.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Instruction sets : Addressing modes and Formats
Chapter Nov-2010
Assembly Lab 3.
Data Transfers, Addressing, and Arithmetic
Assembly 07 String Processing.
Assembly Language Programming of 8085
Microprocessor T. Y. B. Sc..
BYTE AND STRING MANIPULATON
Today we are going to discuss about,
Chapter 9.
Chapter six of V4: The String Instructions
Chapter 4 Data Movement Instructions
EE3541 Introduction to Microprocessors
INSTRUCTION SET.
Introduction to Assembly Language
Chapter 4 Data Movement Instructions
Practical Session 4.
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
CNET 315 Microprocessor & Assembly Language
Computer Architecture and System Programming Laboratory
Presentation transcript:

Chapter 8 String Operations

8.1 Using String Instructions

String in the 80x86 Environment Contiguous collection of bytes, words, doublewords or quadwords in memory Commonly defined in a program’s data segment using such directives as response BYTE 20 DUP (?) label1 BYTE 'The results are ', 0 arrayD DWORD 60 DUP (0)

String Instructions movs (move string) –Copy a string from one location to another cmps (compare string) –Compare the contents of two strings scas (scan string) –Search a string for one particular value stos (store string) –Store a new value in some string position lods (load string) –Copies a value out of some string position

String Instruction Operation Each instruction applies to a source string, a destination string, or both The elements (bytes, words, doublewords or quadwords) are processed one at a time Register indirect addressing is used to locate the individual string elements –ESI (RSI in 64-bit mode) used for source string elements –EDI (RDI) for destination string elements

Mnemonic Variants Since ESI and EDI are automatically used, operands are unnecessary Assembler can’t tell size of string element without further information – suffix on mnemonic can be used –Example, movsb to move string of bytes

More on String Instruction Operation String instruction operates on only one string element at a time, but gets ready to operate on the next element –Changes the source index register ESI and/or the destination index register EDI to contain the address of the next element of the string(s) Can move forward or backward depending on direction flag DF –cld instruction sets forward direction –std instruction sets backward direction

Design to Copy Null-Terminated String while next source byte is not null loop copy source byte to destination; increment source index; increment destination index; end while; put null byte at end of destination string;

Implementation of String Copy mov edi,[ebp+8] ;destination mov esi,[ebp+12] ;initial source address cld ;clear direction flag whileNoNull: cmp BYTE PTR [esi],0 ;null source byte? je endWhileNoNull ;stop copying if null movsb ;copy one byte jmp whileNoNull ;go check next byte endWhileNoNull: mov BYTE PTR [edi],0 ;terminate dest string

8.2 Repeat Prefixes and More String Instructions

Repeat Prefixes Change the string instructions into versions which repeat automatically either for a fixed number of iterations or until some condition is satisfied The three repeat prefixes actually correspond to two different single-byte codes –Not themselves instructions, but supplement machine codes for the primitive string instructions, making new instructions

rep prefix Normally used with movs and with stos Causes this design to be executed: while count in ECX > 0 loop perform primitive instruction; decrement ECX by 1; end while;

Additional Repeat Prefixes repe (equivalent mnemonic repz ) –“repeat while equal” (“repeat while zero”) repne (same as repnz ) –“repeat while not equal” (“repeat while not zero”) Each appropriate for use with cmps and scas which affect the zero flag ZF

repe and repne Operation Each works the same as rep, iterating a primitive instruction while ECX is not zero Each also examines ZF after the string instruction is executed –repe and repz continue iterating while ZF=1, as it would be following a comparison where two operands were equal –repne and repnz continue iterating while ZF=0

cmps Subtracts two string elements and sets flags based on the difference If used in a loop, it is appropriate to follow cmps by a conditional jump instruction repe and repne prefixes often used with cmps instructions

scas Used to scan a string for the presence or absence of a particular string element –String which is examined is a destination string – the address of the element being examined is in the destination index register EDI –Accumulator contains the element being scanned for

stos Copies a byte, a word, a doubleword or a quadword from the accumulator to an element of a destination string Affects no flag, so only the rep prefix is appropriate for use with it –When repeated, it copies the same value into consecutive positions of a string

lods Copies a source string element to the accumulator No repeat prefix is useful with lods lods and stos are often used together in a loop –lods at the beginning of a loop to fetch an element –stos at the end after the element is manipulated

8.3 Character Translation

xlat “translate” Uses a lookup table to modify the byte in AL The table is at the address in EBX The original value in AL is used as an index into the table The byte at that index is stored as the new value in AL

Translation Code Example mov ecx, strLength ; string length lea ebx, table ; addr of translation table lea esi, string ; address of string lea edi, string ; destination also string forIndex: lodsb ; copy next character to AL xlat ; translate character stosb ; copy character back into string loop forIndex ; repeat for all characters

Building a Translation Table Normally 256 bytes long –One entry for each possible byte value

Example Table for ASCII Codes Leaves lower case letters and digits unchanged Translates upper case letters to lower case Translates all other characters to spaces table BYTE 48 DUP (' '), " ", 7 DUP (' ') BYTE "abcdefghijklmnopqrstuvwxyz", 6 DUP (' ') BYTE "abcdefghijklmnopqrstuvwxyz", 133 DUP (' ') 0 at position 48 (30 16 ) so 0 translated to 0 a at position 65 (41 16 ) so A translated to a

8.4 Converting a 2’s Complement Integer to an ASCII String

dtoa macro expansion push ebx ; save EBX lea ebx, dest ; destination address push ebx ; destination parameter mov ebx, [esp+4] ; in case source was EBX mov ebx, source ; source value push ebx ; source parameter call dtoaproc ; dtoaproc(source,dest) add esp, 8 ; remove parameters pop ebx ; restore EBX The real work is done by dtoaproc

dtoaproc algorithm determine whether source positive or negative; put 10 spaces in destination area; make EDI point at 11 th byte; repeat digit := source mod 10; convert digit to ASCII and store at [EDI]; decrement EDI; divide source by 10; until source = 0; if original source negative, append leading minus sign;

dtoaproc special case Most negative numbers are handled by processing the corresponding positive number, “remembering” the minus sign has no corresponding positive number, so the characters are stored in the destination area one at a time