Assembly 07 String Processing.

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.
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
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
Microprocessor Programming II
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:
COSC 456 Lesson 8 Cool Codes ADD AL,SIAL AL + SI ADD AL,[SI]AL AL + [SI] INC BXBX BX + 1 INC [BX]Ambiguity error INC BYTE PTR [BX][BX] [BX] + 1 INC WORD.
11.1/36 Repeat: From Bits and Pieces Till Strings.
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.
Strings, Procedures and Macros
ICS312 Lecture13 String Instructions.
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.
String Processing Chapter 10 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string.
4. Kernel and VGA ENGI 3655 Lab Sessions. Richard Khoury2 Textbook Readings  None.
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 6 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
Chapter 8 String Operations. 8.1 Using String Instructions.
Format of Assembly language
Chapter Nov-2010
Data Transfers, Addressing, and Arithmetic
Assembly Language for x86 Processors 6th Edition
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.
INSTRUCTION SET.
اصول اساسی برنامه نویسی به زبان اسمبلی
Defining Types of data expression Dn [name] expression Dn [name]
Computer Organization and Assembly Language
Controlling Program Flow
Chapter 4 Data Movement Instructions
Lecture 1 Instruction set of 8086 Лектор: Люличева И.А. 1.
Computer Organization and Assembly Languages Yung-Yu Chuang 2005/12/01
32-bit instruction mode(80386-Pentium 4 only)
Symbolic Instruction and Addressing
(Array and Addressing Modes)
Symbolic Instruction and Addressing
(Array and Addressing Modes)
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.
CS-401 Computer Architecture & Assembly Language Programming
X86 Assembly Review.
Chapter 5 Arithmetic and Logic Instructions
Data Movement Instructions
UNIT-II Assembly Language Programs Involving Logical
Chapter 6 –Symbolic Instruction and Addressing
Chapter 5: Arithmetic and Logic Instructions
CNET 315 Microprocessor & Assembly Language
Computer Architecture and Assembly Language
UNIT-II ADDRESSING MODES & Instruction set
(Array and Addressing Modes)
Presentation transcript:

Assembly 07 String Processing

Definition Defining strings Using sting length Zero Ending Explicitly string DB ’Error message’ str_len DW 13 Relative str_len DW $ - string Zero Ending string1 DB ’This is OK’,0 string2 DB ’Price = $9.99’,0

String instruction String instruction may require a source operand, a destination operand, or both. String instructions use ESI and EDI registers to point to the source and destination operands. The source operand is assumed to be at ESI and the destination operand at EDI in memory. For 16-bit segments SI and DI are used The registers/indices are update automatically as the instruction is performed

Rep instruction Rep instruction unconditional repeat rep REPeat conditional repeat repe REPeat while Equal repz REPeat while Zero repne REPeat while Not Equal repnz REPeat while Not Zero This instruction causes the instruction to repeat according to the value in ECX/CX while (ECX = 0) execute the string instruction; ECX := ECX–1; end while

Direction Flag The direction of string operations depends on the value of the direction flag. Direction flag (DF) is clear (DF = 0) String operations proceed in the forward direction, from head to tail of a string. Direction flag (DF) is set (DF = 1) String processing is done in the opposite direction, from head to tail of a string. DF instruction std set direction flag (DF = 1) cld clear direction flag (DF = 0)

String instructions String move Formats: movs dest_string,source_string movsb movsw Movsd The suffix b, w, or d is used to indicate byte, word, or doubleword operands. The source string value is pointed to by ESI and the destination string location is indicated by EDI in memory. After copying, the ESI and EDI registers are updated according to the value of the direction flag.

Code 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

Load String Load a String (lods) This instruction copies the value from the source string (pointed to by ESI) in memory to AL (for byte operands—lodsb),AX (forword operands—lodsw), or EAX (for doubleword operands—lodsd). lodsb— load a byte string AL := (ESI) ; copy a byte if (DF = 0) ; forward direction then ESI := ESI+1 else ; backward direction ESI := ESI−1 end if

Store a String (stos) stosb— store a byte string This instruction performs the complementary operation. It copies the value in AL (for stosb), AX (for stosw), or EAX (for stosd) to the destination string (pointed to by ES:EDI) in memory. 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

Initializing an array Initializing array 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,array mov AX,-1 cld ; forward direction rep stosw

String Compare Instruction The cmps instruction can be used to compare two strings. Like the cmp instruction, cmps performs ESI − EDI cmpsb— compare two byte strings Compare the two bytes at ESI and EDI and set flags if (DF = 0) then ; forward direction ESI := ESI+1 EDI := EDI+1 else ; backward direction ESI := ESI−1 EDI := EDI−1 end if Flags affected: As per cmp instruction

Using cmps We can use conditional jumps like ja, jg, jc, etc. to test the relationship of the two values. As usual, the ESI and EDI registers are updated according to the value of the direction flag and the operand size. The cmps instruction is typically used with the repe/repz or repne/repnz prefix. .DATA string1 db ’abcdfghi’,0 strLen EQU $ - string1 string2 db ’abcdefgh’,0 .CODE

Using cmps .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 leaves ESI pointing to g in string1 and EDI to f in string2. Therefore, adding dec ESI dec EDI leaves ESI and EDI pointing to the last character that differs. Then we can use, ja str1Above

Scanning a String The scas (scanning a string) instruction is useful in searching for a particular value or character in a string. The value should be in AL (for scasb), AX (for scasw), or EAX (for scasd), and ES:EDI should point to the string to be searched. scasb— scan a byte string Compare AL to the byte at ES:EDI and set flags if (DF = 0) then; forward direction EDI := EDI+1 else ; backward direction EDI := EDI−1 end if Flags affected: As per cmp instruction

Using scas .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 This program leaves EDI pointing to e in string1. The following example can be used to skip the initial blanks.

Example: Skip initial blanks .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