ICS312 Lecture13 String Instructions.

Slides:



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

For the example slides do not click the mouse to see the full animation Microprocessor 1 Dr.Raed Al-qadi 2009 for the example slides do not click the mouse.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
ICS312 Set 6 Operands. Basic Operand Types (1) Register Operands. An operand that refers to a register. MOV AX, BX ; moves contents of register BX to.
Gursharan Singh Tatla 21-Nov-20101www.eazynotes.com.
8086 : INSTRUCTION SET By, Pramod Sunagar Assistant Professor
Direct video practice and Keyboard Operations
Target Processor Directives , When using.386, the program can only run on 386 and above processors.
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
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.
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.
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
Processing String Data and Binary Data (continue)
Addressing Modes of 8086 Processor Ammar Anwar Khan Electrical Engineer King Saud University Riyadh Saudi Arabia.
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.
Introduction to Assembly Language Programming 1. Overview of Assembly Language  Advantages:  Disadvantages: Faster as compared to programs written using.
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.
Khaled A. Al-Utaibi  I/O Ports  I/O Space VS Memory Space  80x86 I/O Instructions − Direct I/O Instructions − Indirect I/O Instructions.
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.
Multi-module programming. Requirements of an assembly language module when it is linked with another module PUBLIC directive - it exports to other modules.
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.
File Operations. FILE PROCESSING For the purposes of the following discussion, reading means copying all or part of an existing file into memory Writing.
Chapter 8 String Operations. 8.1 Using String Instructions.
Chapter Nov-2010
Assembly Language for x86 Processors 6th Edition
Assembly 07 String Processing.
BYTE AND STRING MANIPULATON
8086 MICROPROCESSOR.
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]
Symbolic Instruction and Addressing
Chapter 4 Data Movement Instructions
Lecture 1 Instruction set of 8086 Лектор: Люличева И.А. 1.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
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.
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
CNET 315 Microprocessor & Assembly Language
Presentation transcript:

ICS312 Lecture13 String Instructions

What is String String - a sequence of characters Note. The only string instructions you will be expected to know are: movsb cmpsb stosb The other string instructions described are just for background reading.

Operations that can be performed with string instructions Copy a string into another string Store characters in a string Compare strings of characters alphanumerically Search a string for a particular byte or word (not covered in our course)

Direction Flag one of Pentium's processor control flags. controls the direction of string operations: DF = 0 => forward (left to right) processing DF = 1 => backward (right to left) processing CLD - clears the DF; sets DF = 0 STD - sets DF = 1

Moving (Copying) Strings Instructions:     MOVSB - copies contents of BYTE given by DS:SI into ES:DI     MOVSW - copies contents of WORD given by DS:SI into ES:DI     MOVSD - copies contenst of DOUBLE WORD given by DS:SI into ES:DI

Moving (Copying) Strings (Cont.1) Notes: The string instructions use DS:SI as the Source string and ES:DI as the destination string. (For the string instructions only, DI references an offset in the ES by default).

Moving (Copying) Strings (Cont.2) Example: .DATA STRING1 DB 'HELLO' STRING2 DB 5 DUP (0) .CODE         MOV AX, @DATA         MOV DS, AX            ; init DS         MOV ES, AX            ; init ES         LEA SI, STRING1       ; source         LEA DI, STRING2        ; destination         CLD                   ; DF = 0         MOVSB                 ; mov 1st byte         MOVSB                 ; mov 2nd byte Note: MOVSB moves only 1 byte at a time. Instead … Set CX = count and use the REP prefix to move a specified number of bytes.

Moving (Copying) Strings (Cont.3) Example:         CLD                   ; forward direction         PUSH DS               ; set ES = DS         POP ES                         LEA SI, STRING1       ; set SI to source         LEA DI, STRING2       ; set DI to destination         MOV CX, 5         REP MOVSB             ; copies 5 chars Note: both SI and DI are incremented for each byte that is copied from the offset given in SI to the offset given in DI (in the forward direction)

Moving (Copying) Strings (Cont.4) Reverse:         STD                        ; reverse direction         PUSH DS         POP ES                     ; ES = DS         LEA SI, STRING1+4         ; end of string         LEA DI, STRING2+4         ; end of string         MOV CX, 5         REP MOVSB                  ; copy 5 chars Note: both SI and DI are decremented by one for each byte that is copied from the offset given in SI to the offset given in (in the reverse direction).

Moving (Copying) Strings (Cont.5) MOVSW works the same way as MOVSB except that it moves one word (2 bytes) at a time. Consequently, SI and DI will be incremented or decremented by 2 bytes for each word copied. MOVSD moves one double word (4 bytes) at a time.   Consequently, SI and DI will be incremented or decremented by 4 for each word copied.

Moving (Copying) Strings (Cont.6) Example MES1 DB ‘We are ready’ LMES1 EQU $-MES1 MES2 DB 30 DUP (?) ; to move characters from MES1 to MES2 CLD LEA SI, MES1 LEA DI, MES2 PUSH DS POP ES MOV CX, LMES1 REP MOVSB

Moving (Copying) Strings (Cont.7) MES DW 10, 25, 40, 50, 60, 70, ? To insert a word containing 30 into the sequence INCORRECT CODE CLD PUSH DS POP ES LEA SI, MES+4 LEA DI, MES+6 MOV CX, 4 REP MOVSW

Moving (Copying) Strings (Cont.8) CORRECT CODE STD PUSH DS POP ES LEA SI, MES+10 LEA DI, MES+12 MOV CX, 4 REP MOVSW MOV MES+4, 30

Storing Strings Instructions:     STOSB - copies contents of AL to BYTE address given by ES:DI.  DI is incremented/decremented by 1.     STOSW - copies the contents of AX to the WORD address given by ES:DI.   DI is incremented/decremented by 2.     STOSD - copies contents of EAX to the DOUBLE WORD address given by ES:DI.  DI is incremented/decremented by 4.

Storing Strings (Cont. 1) Example:     MOV AX, @DATA     MOV ES, AX ; initialize ES     LEA DI, STRING1         ; assume BYTE string     CLD     MOV AL, 'A‘     STOSB                   ; store 1st byte of A     STOSB                   ; store 2nd byte of A ; best to put no. of bytes in CX and use REP STOSB

Storing Strings (Cont. 2) EXAMPLE Buffer DB 800 DUP (?) ; to set a buffer to nulls CLD PUSH DS POP ES LEA DI, Buffer MOV CX, 800 MOV AL, ‘ ‘ ; set AL to null REP STOSB

Compare String Instructions:     CMPSB - compares BYTE at ES:DI with BYTE at DS:SI and sets flags.     CMPSW - compares WORD at ES:DI with WORD at DS:SI and sets flags.     CMPSD - compares DOUBLE WORD at ES:DI with WORD at DS:SI and sets flags.

Compare String (Cont.1) Example: To jump to “unequal” if the strings do not match .DATA STRING1 DB 'ACD' STRING2 DB 'ABC‘ .CODE         MOV AX, @DATA         MOV DS, AX         MOV ES, AX         CLD         LEA SI, STRING1         LEA DI, STRING2         MOV CX, 3            ; string length         REPE CMPSB           ; repeat while strings match JNE unequal

Compare String (Cont.2) Note: It increments (or decrements) each string pointer and successively compares bytes until there is a mismatch between the bytes being compared, or until CX = 0. CMPSB can be used to determine whether two strings match, or whether one string is a substring of another string.

Load String (OPTIONAL) Instructions:     LODSB - moves the BYTE at address DS:SI into AL. SI is incremented/decremented by 1.     LODSW - moves the WORD at address DS: SI into AX. SI is incremented/decremented by 2.     LODSD - moves the DOUBLE WORD at address DS:SI into EAX. SI is incremented/decremented by 4.

Scan String (OPTIONAL) Instructions:    SCASB - compares BYTE at ES:DI with AL and sets flags according to result.    SCASW - compares WORD at ES:DI with AX and sets flags.    SCASD - compares DOUBLE WORD at ES:DI with EAX and sets flags.

Scan String (Cont. 1) Example: .DATA STRING1 DB 'ABC‘ .CODE         MOV AX, @DATA         MOV AX, ES ; initialize ES         CLD ; left to right         LEA DI, STRING1         MOV AL, 'B' ; target character         SCASB ; scan first byte  

Scan String (Cont. 2) Note: when the target ("B") is found, ZF (the zero flag) = 1 and DI points to the byte following the target since DI is automatically incremented by SCASB. So: Set CX = count and use:         REPNE SCASB         REPNZ SCASB to repeat the scan until the target byte is found, or until the entire string has been searched (i.e., CX = 0).

Scan String (Cont. 3) ; to count the number of ‘A’s in a string MES DB ‘ABRACADABRA’ LMES EQU $-MES Count DB ? .CODE … CLD PUSH DS POP ES MOV Count, 0 LEA DI, MES MOV CX, LMES MOV AL, ‘A’

Scan String (Cont. 4) JCXZ EXIT AGAIN REPNE SCASB JNE EXIT ADD Count, 1 JMP AGAIN EXIT

BYTE, WORD, and DOUBLE WORD form of string instructions SOURCE DEST BYTE WORD DOUBLE WORD Move String ES:DI MOVSB MOVSW MOVSD Compare String  DS:SI CMPSB CMPSW CMPSD Store string  AL/AX/EAX STOSB STOSW STOSD Load string  LODSB LODSW LODSD Scan string SCASB SCASW SCASD DS:SI The operands are IMPLICIT.

Textbook Reading (Jones): Chapter 17