Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

Strings and Arrays

May 2008

ICS212

11

outline

String instructions String procedures Two dimensional arrays

May 2008

ICS212

22

First, string processing without special string instructions

Variable-length String representation


Length, value format Null terminated format

str1 byte 255 dup(?) str2 byte 255 dup(?) str3 byte 255 dup(?) str4 byte 255 dup(?) String copy str1 to str2 str4 to str3 May 2008 ICS212 33
null null

Special String instructions

May 2008

ICS212

44

String instructions

Purpose: optimize processing of string data

1.

op

2.

op

May 2008

ICS212

55

String

primitive instructions

Five groups of primitive string instructions are provided for optimized processing arrays of bytes, words and double words

The instructions assume the use of ESI (for source operand index) and EDI (for destination operand index) and automatically increment (or decrement) them accordingly. The source is assumed to be in the segment pointed at by DS and destination is in the segment pointed at by ES.

May 2008

ICS212

66

String primitive instructions

instruction

description
Move data element: copy an element from a source string to a destination string Compare a pair of data elements (from two strings) Scan a data element in a string, comparing it to a given item store accumulator contents into an element in a string Load accumulator from an element in a string

MOVSsize CMPSsize SCASsize STOSsize LODSsize

*size may be B for byte, W for word or D for double word May 2008 ICS212 77

String primitive instructions: using the


Repeat prefix

In their basic forms, each instruction processes only a single or a single pair of elements. They are therefore often more useful when written together with a repeat prefix. With the prefix, the instruction repeats using ECX as counter that is decremented automatically.

Repeat prefix
REP REPZ, REPE

description
Repeat while ECX > 0 Repeat while ZF is set and ECX > 0

REPNZ, REPNE Repeat while ZF is clear and ECX > 0


May 2008 ICS212 88

String primitive instructions: the


direction of processing

String processing may be left to right or vice versa The direction of string processing is controlled by the DF flag in the EFLAGS register

Value of DF Effect on ESI & EDI


clear set incr decr

Address sequence
Low to high High to low

Setting and clearing the DF flag - two instructions: CLD STD

May 2008

ICS212

99

String primitive instructions: example Using MOVSB to copy 10 bytes from string1 to string2 .data string1 BYTE 20 DUP (?) string2 BYTE 20 DUP (?) .code cld mov esi, OFFSET string1 mov edi, OFFSET string2 mov ecx, 10 rep movsb
May 2008 ICS212

* Compare with using the basic MOV in explicit an iterative loop


1010

String primitive instructions: example Using MOVSW to copy contents of an array of 10 words, e.g. integer values,
to a second array.

.data intArray1 WORD 10 DUP (?) intArray2 WORD 10 DUP (?) .code ?

May 2008

ICS212

1111

String primitive instructions: example Using CMPSB to compare two strings of 20 bytes each Solution: use the repeat prefix in the form that causes an exit when the zero flag is clear .data string1 BYTE 20 DUP (?) string2 BYTE 20 DUP (?) .code ; --TO DO--

May 2008

ICS212

1212

String primitive instructions: example

SCAS:

compares the value in the accumulator (AL, AX or EAX) to the memory element whose offset is given by EDI.

Use SCASB to search for an occurrence of a given character, E, in the a


given string

.data string1 BYTE 20 DUP (?) letter BYTE E .code ?


May 2008 ICS212 1313

String primitive instructions: example


STOS:

stores the content of the accumulator (AL, AX or EAX) to the memory element whose offset is given by by EDI.

Use STOSB to initialize a string with 0s .data string1 BYTE 20 DUP (?) filler BYTE 0 .code ?
May 2008 ICS212 1414

String primitive instructions: example


LODS:

loads the accumulator (AL, AX or EAX) from the memory element whose offset is given by by ESI.

Use LODSD and STOSD to implement addition of two vectors of 32-bit


words

.data vectorA DWORD 20 DUP (?) vectorB DWORD 20 DUP (?) .code ?

May 2008

ICS212

1515

Some string procedures

May 2008

ICS212

1616

String procedures
String procedures that would be useful to implement str_len: accepts pointer to string (assumed to be terminated by NULL); returns length of string str_copy: accepts pointers to source & dest strings; copies the source to dest until a NULL terminator is encountered; returns, in EDX, the count of items copied. str_trim: accepts pointer to string (assumed to be terminated by NULL), and a trail character; removes all occurrences of the trail character at the tail of the string; return the new string length in EDX. str_cmp: accepts pointers to source & dest strings (assume a NULL terminator); return 0 in register EDX if equal and 1 otherwise.

May 2008

ICS212

1717

Two dimensional arrays

May 2008

ICS212

1818

Two dimensional array

Basic structure

columns 0 1 n-1

0 1 2 rows

m-1

How to define (and allocated in memory) How to access elements Base-index addressing modes May 2008 ICS212 1919

Two dimensional array

How defined in assembly (and allocated in memory) Row major Column major

May 2008

ICS212

2020

Accessing array elements

The base+index addressing mode


syntax [basereg + indexreg]

E.g. semantic

[EBX + EDI]

The effective address (offset) is obtained by adding the contents of the two registers

May 2008

ICS212

2121

Accessing array elements The base+index + displacement addressing mode

syntax
[basereg + indexreg + displ]

displ [basereg + indexreg ]

E.g. [EBX + EDI + 4] table[EBX + EDI]

semantic
The 2008 May effective address (offset) is obtained by adding the contents of the ICS212 2222 two registers and the displ value

Base index modes

Application to two dimensional array Base register to point at the row and Index register to point at the column

May 2008

ICS212

2323

Example 8.1
Computation of column totals of a 4 x 8 table

.data intTable ??

.code main PROC ; read data into the table INVOKE tableRead, rows, cols ; code to compute column totals

May 2008

ICS212

2424

Example 8.2

Given a 4 x 8 table with each element a structure of type: struct { char cname[20]; int_32 tempr; }

Write a program fragment that computes the mean tempr over all the 32 centers

May 2008

ICS212

2525

You might also like