String Instructions - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

String Instructions

Description:

Store String. STOSB, STOSW. Copy AL or AX into an array of ... stosb ;store at next location. loop lp. 9/5/09. Dr. Tim Margush - Assembly Language Programming ... – PowerPoint PPT presentation

Number of Views:572
Avg rating:3.0/5.0
Slides: 18
Provided by: timma87
Category:

less

Transcript and Presenter's Notes

Title: String Instructions


1
String Instructions
  • Assembly Language Programming
  • University of Akron
  • Dr. Tim Margush

2
String?
  • An array of bytes or words located in memory
  • Supported String Operations
  • Copy
  • Search
  • Store
  • Compare

3
String Instruction Basics
  • Source DSSI, Destination ESDI
  • You must ensure DS and ES are correct
  • You must ensure SI and DI are offsets into DS and
    ES respectively
  • Direction Flag (0 Up, 1 Down)
  • CLD - Increment addresses (left to right)
  • STD - Decrement addresses (right to left)

4
Moving (Copying)
  • MOVSB, MOVSW
  • Memory to memory copy of a byte or word
  • Each execution of this instruction causes
  • Copy byte/word at DSSI to ESDI
  • Inc/Dec SI and DI by 1 or 2
  • If CX contains a repetition factor
  • REP MOVSB or REP MOVSW will automatically execute
    the move CX times, and CX becomes 0

5
Example Copy a String
  • Copy array a to b, assume ESDS, and 10 bytes
    are to be copied
  • mov cx, 10 10 bytes to copy
  • mov di, offset b destination
  • mov si, offset a source cld left to right
  • rep movsb

6
Example Memory Shift
  • shift bytes of a 3 bytes to right
  • mov cx, 7 bytes to copy
  • mov di, offset a9 dest
  • mov si, offset a9-3 source
  • std copy from right to left
  • rep movsb

SI
DI
a
7
Example Replication
  • pattern db "!_at_" duplicate
  • db (100-4) dup (?) space
  • mov cx,100-4 96 bytes to copy mov si, offset
    pattern
  • mov di, offset pattern4
  • cld destructive overlap
  • rep movsb

DI
SI
!
_at_


a
8
Store String
  • STOSB, STOSW
  • Copy AL or AX into an array of bytes or words
  • destination ESDI
  • Each repetition Increments or Decrements DI
  • depends on DF
  • Commonly used with REP prefix and number of
    repetitions in CX
  • The Word version byte reverses AX as usual

9
Example Initilializing Storage
  • arr dw 200 dup (?) empty words
  • to be initialized to A050A050...
  • mov ax,50A0h
  • mov di,offset arr
  • mov cx,200 array size
  • cld
  • stosw

DI
50
A0
AX
A0
50
A0
50
arr
10
Load String
  • LODSB, LODSW
  • Byte or word at DSSI is copied into AL or AX
  • SI is increased or decreased by 1 or 2
  • This is commonly paired with STOSx in a loop to
    process each component of an array
  • There is no reason to use REP with this
    instruction

11
Example Process Array
  • array b toUpper(array a)
  • mov di, offset b dest
  • mov si, offset a source
  • mov cx,30 array size cld left to right
    processing
  • lp
  • lodsb get next byte
  • and al,0DFh to upper case
  • stosb store at next location
  • loop lp

12
Scan String
  • SCASB, SCASW
  • Compares AL or AX with ESDI and auto increments
    or decrements DI
  • This instruction sets the flags register
  • Flags set according to result of compare
  • Used in a loop, or with conditional REPs
  • REPZ, REPE, REPNZ, REPNE

13
Conditional Repeats for SCASx and CMPSx
  • while (CX ! 0 )
  • do string primitive
  • --CX
  • if (REPNE and ZF 1) exit loop
  • if (REPE and ZF 0) exit loop
  • The test for CX is at the top of the loop
  • Test of zero flag is at the end of the loop.
  • Only CMPS and SCAS instructions can affect the ZF
    value

14
Example String Search
  • arr db 'abcdefghijklmnopqrstuvwxyz'
  • mov di, offset arr
  • mov cx,26 26 bytes
  • cld left to right processing
  • mov al,target ch to find
  • repne scasb search for match
  • make at most cx comparisons
  • jne nomatch ZF never set
  • match occurred at ESdi-1
  • di is incremented even if match

15
Compare String
  • CMPSB, CMPSW
  • Compares DSSI to ESDI, setting flags and
    auto-increments/decrements SI and DI
  • Used to compare arrays of words or arrays of
    bytes
  • Typically used with conditional REP instruction

16
Example String Compare
  • mov si, offset str1
  • mov di, offset str2
  • cld left to right processing
  • mov cx, 12 shorter string
  • repe cmpsb cmp til ltgt or cx0
  • jl str1smaller
  • jg str2smaller
  • the strings are equal - so far
  • if sizes different, shorter string is less

17
Homework
  • Pg 225
  • 1-4
Write a Comment
User Comments (0)
About PowerShow.com