String Processing Chapter 10 S. Dandamudi. 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,

Slides:



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

Judul Mata Kuliah Judul Pokok Bahasan 1/total Data Movement Instructions.
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
CS2422 Assembly Language & System Programming October 3, 2006.
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
CS2422 Assembly Language & System Programming October 31, 2006.
Topic – string – Ch. 11 [Marut] Ch. 4 [Brey] String Data Transfer Instructions – The Direction Flag – LODS Instructions – STOS Instructions – MOVS Instructions.
Addressing Modes Chapter 11 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer,  S.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Microprocessor Programming II
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
CDP ECE Spring 2000 ECE 291 Spring 2000 Lecture 7: More on Addressing Modes, Structures, and Stack Constantine D. Polychronopoulos Professor, ECE.
The Pentium Processor Chapter 3 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
The Pentium Processor Chapter 3 S. Dandamudi.
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 Four-- The 80x86 Instruction Set Principles of Microcomputers 2015年10月6日 2015年10月6日 2015年10月6日 2015年10月6日 2015年10月6日 2015年10月6日 1 Chapter Four.
11.1/36 Repeat: From Bits and Pieces Till Strings.
Faculty of Engineering, Electrical Department,
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)
Selected Pentium Instructions Chapter 12 S. Dandamudi.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Strings, Procedures and Macros
ICS312 Lecture13 String Instructions.
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Processing String Data and Binary Data (continue)
Click to add Title Comunicación y Gerencia Click To add Subtitle Click to add Text Fundamentals of Assembly Language.
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.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string.
Overview of Assembly Language Chapter 4 S. Dandamudi.
Logical and Bit Operations Chapter 9 S. Dandamudi.
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.
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.
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 11 Text mode video
Lecture 6 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
Assembly Language Wei Gao. Assembler language Instructions.
Selection and Iteration Chapter 8 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Chapter 8 String Operations. 8.1 Using String Instructions.
Chapter Nov-2010
Data Transfers, Addressing, and Arithmetic
Assembly Language for x86 Processors 6th Edition
Assembly 07 String Processing.
BYTE AND STRING MANIPULATON
Chapter 9.
Chapter six of V4: The String Instructions
Chapter 4 Data Movement Instructions
EE3541 Introduction to Microprocessors
INSTRUCTION SET.
Assembly IA-32.
INSTRUCTION SET.
Computer Organization and Assembly Language
Chapter 4 Data Movement Instructions
Lecture 1 Instruction set of 8086 Лектор: Люличева И.А. 1.
32-bit instruction mode(80386-Pentium 4 only)
Symbolic Instruction and Addressing
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.
Computer Architecture CST 250
X86 Assembly Review.
Data Movement Instructions
Chapter 6 –Symbolic Instruction and Addressing
Presentation transcript:

String Processing Chapter 10 S. Dandamudi

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 2 Outline String representation  Using string length  Using a sentinel character String instructions  Repetition prefixes  Direction flag  String move instructions  String compare instructions  String scan instructions Illustrative examples  LDS and LES instructions Examples  str_len  str-cpy  str_cat  str_cmp  str_chr  str_cnv Indirect procedure call Performance: Advantage of string instructions

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 3 String Representation Two types  Fixed-length  Variable-length Fixed length strings  Each string uses the same length »Shorter strings are padded (e.g. by blank characters) »Longer strings are truncated  Selection of string length is critical »Too large ==> inefficient »Too small ==> truncation of larger strings

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 4 String Representation (cont’d) Variable-length strings  Avoids the pitfalls associated with fixed-length strings Two ways of representation  Explicitly storing string length (used in PASCAL) string DB ‘Error message’ str_len DW $-string –$ represents the current value of the location counter  $ points to the byte after the last character of string  Using a sentinel character (used in C) »Uses NULL character – Such NULL-terminated strings are called ASCIIZ strings

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 5 String Instructions Five string instructions LODSLOaD Stringsource STOSSTOre Stringdestination MOVSMOVe Stringsource & destination CMPSCoMPare Stringsource & destination SCASSCAn Stringdestination Specifying operands  32-bit segments: DS:ESI = source operand ES:EDI = destination operand  16-bit segments: DS:SI = source operand ES:DI = destination operand

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 6 String Instructions (cont’d) Each string instruction  Can operate on 8-, 16-, or 32-bit operands  Updates index register(s) automatically »Byte operands: increment/decrement by 1 »Word operands: increment/decrement by 2 »Doubleword operands: increment/decrement by 4 Direction flag  DF = 0: Forward direction (increments index registers)  DF = 1: Backward direction (decrements index registers) Two instructions to manipulate DF std set direction flag (DF = 1) cld clear direction flag (DF = 0)

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 7 Repetition Prefixes String instructions can be repeated by using a repetition prefix Two types  Unconditional repetition rep REPeat  Conditional repetition repe/repz REPeat while Equal REPeat while Zero repne/repnz REPeat while Not Equal REPeat while Not Zero

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 8 Repetition Prefixes (cont’d) rep while (ECX  0) execute the string instruction ECX := ECX  1 end while ECX register is first checked  If zero, string instruction is not executed at all  More like the JECXZ instruction

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 9 Repetition Prefixes (cont’d) repe/repz while (ECX  0) execute the string instruction ECX := ECX  1 if (ZF = 0) then exit loop end if end while Useful with cmps and scas string instructions

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 10 Repetition Prefixes (cont’d) repne/repnz while (ECX  0) execute the string instruction ECX := ECX  1 if (ZF = 1) then exit loop end if end while

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 11 String Move Instructions Three basic instructions movs, lods, and stos Move a string (movs) Format movs dest_string,source_string movsb ; operands are bytes movsw ; operands are words movsd ; operands are doublewords First form is not used frequently  Source and destination are assumed to be pointed by DS:ESI and ES:EDI, respectively

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 12 String Move Instructions (cont’d) movsb --- move a byte string ES:EDI:= (DS:ESI); copy a byte if (DF=0); forward direction then ESI := ESI+1 EDI := EDI+1 else ; backward direction ESI := ESI  1 EDI := EDI  1 end if Flags affected: none

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 13 String Move Instructions (cont’d) Example.DATA string1 db 'The original string',0 strLen EQU $ - string1.UDATA string2 resb 80.CODE.STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov ECX,strLen ; strLen includes NULL mov ESI,string1 mov EDI,string2 cld ; forward direction rep movsb

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 14 String Move Instructions (cont’d) Load a String (LODS) Copies the value from the source string at DS:ESI to  AL ( lodsb )  AX ( lodsw )  EAX ( lodsd ) Repetition prefix does not make sense  It leaves only the last value in AL, AX, or EAX register

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 15 String Move Instructions (cont’d) lodsb --- load a byte string AL := (DS:ESI); copy a byte if (DF=0); forward direction then ESI := ESI+1 else ; backward direction ESI := ESI  1 end if Flags affected: none

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 16 String Move Instructions (cont’d) Store a String (STOS) Performs the complementary operation Copies the value in »AL ( lodsb ) »AX ( lodsw ) »EAX ( lodsd ) to the destination string at ES:EDI Repetition prefix can be used if you want to initialize a block of memory

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 17 String Move Instructions (cont’d) stosb --- store a byte string ES:EDI := AL; copy a byte if (DF=0); forward direction then EDI := EDI+1 else ; backward direction EDI := EDI  1 end if Flags affected: none

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 18 String Move Instructions (cont’d) Example: Initializes array1 with -1.UDATA array1 resw 100.CODE.STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov ECX,100 mov EDI,array1 mov AX,-1 cld ; forward direction rep stosw

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 19 String Move Instructions (cont’d) In general, repeat prefixes are not useful with lods and stos Used in a loop to do conversions while copying mov ECX,strLen mov ESI,string1 mov EDI,string2 cld ; forward direction loop1: lodsb or AL,20H stosb loop loop1 done:

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 20 String Compare Instruction cmpsb --- compare two byte strings Compare two bytes at DS:ESI and ES:EDI and set flags if (DF=0); forward direction then ESI := ESI+1 EDI := EDI+1 else ; backward direction ESI := ESI  1 EDI := EDI  1 end if Flags affected: As per cmp instruction (DS:ESI)  (ES:EDI)

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 21 String Compare Instruction (cont’d).DATA string1 db 'abcdfghi',0 strLen EQU $ - string1 string2 db 'abcdefgh',0.CODE.STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov ECX,strLen mov ESI,string1 mov EDI,string2 cld ; forward direction repe cmpsb dec ESI dec EDI ; ESI & EDI pointing to the last character that differs

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 22 String Compare Instruction (cont’d).DATA string1 db 'abcdfghi',0 strLen EQU $ - string1 - 1 string2 db 'abcdefgh',0.CODE.STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov EECX,strLen mov ESI,string1 + strLen - 1 mov EDI,string2 + strLen - 1 std ; backward direction repne cmpsb inc ESI ; ESI & EDI pointing to the first character that matches inc EDI ; in the backward direction

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 23 String Scan Instruction scasb --- Scan a byte string Compare AL to the byte at ES:EDI & set flags if (DF=0); forward direction then EDI := EDI+1 else ; backward direction EDI := EDI  1 end if Flags affected: As per cmp instruction (DS:ESI)  (ES:EDI) scasw uses AX and scasd uses EAX instead of AL

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 24 String Scan Instruction (cont’d) Example 1.DATA string1 db 'abcdefgh',0 strLen EQU $ - string1.CODE.STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov ECX,strLen mov EDI,string1 mov AL,'e' ; character to be searched cld ; forward direction repne scasb dec EDI ; leaves EDI pointing to e in string1

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 25 String Scan Instruction (cont’d) Example 2.DATA string1 db ' abc',0 strLen EQU $ - string1.CODE.STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov ECX,strLen mov EDI,string1 mov AL,' ' ; character to be searched cld ; forward direction repe scasb dec EDI ; EDI pointing to the first non-blank character a

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 26 Illustrative Examples LDS and LES instructions String pointer can be loaded into DS/SI or ES/DI register pair by using lds or les instructions Syntax lds register,source les register,source  register should be a 32-bit register  source is a pointer to a 48-bit memory operand »register is typically ESI in lds and EDI in les

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 27 Illustrative Examples (cont’d) Actions of lds and les lds register := (source) DS := (source+4) les register := (source) ES := (source+4) Pentium also supports lfs, lgs, and lss to load the other segment registers

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 28 Illustrative Examples (cont’d) Seven popular string processing routines are given as examples  str_len  str-cpy  str_cat  str_cmp  str_chr  str_cnv

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 29 Indirect Procedure Call Direct procedure calls specify the offset of the first instruction of the called procedure In indirect procedure call, the offset is specified through memory or a register  If BX contains pointer to the procedure, we can use call EBX  If the word in memory at target_proc_ptr contains the offset of the called procedure, we can use call [target_proc_ptr] These are similar to direct and indirect jumps

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 30 Performance: Advantage of String Instructions Two chief advantages of string instructions  Index registers are automatically updated  Can operate two operands in memory Example: Copy data from array1 to array2 cld rep movsd  Assumes: DS:ESI points to array1 ES:EDI points to array2 ECX contains the array size

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 10: Page 31 Performance: Advantage of String Instructions (cont’d) No string instructions With string instructions 50,000-element array-to-array copy Last slide