Professional Documents
Culture Documents
Strings and Arrays
Strings and Arrays
May 2008
ICS212
11
outline
May 2008
ICS212
22
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
May 2008
ICS212
44
String instructions
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
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
*size may be B for byte, W for word or D for double word May 2008 ICS212 77
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
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
Address sequence
Low to high High to low
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
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
SCAS:
compares the value in the accumulator (AL, AX or EAX) to the memory element whose offset is given by EDI.
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
loads the accumulator (AL, AX or EAX) from the memory element whose offset is given by by ESI.
.data vectorA DWORD 20 DUP (?) vectorB DWORD 20 DUP (?) .code ?
May 2008
ICS212
1515
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
May 2008
ICS212
1818
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
How defined in assembly (and allocated in memory) Row major Column major
May 2008
ICS212
2020
E.g. semantic
[EBX + EDI]
The effective address (offset) is obtained by adding the contents of the two registers
May 2008
ICS212
2121
syntax
[basereg + indexreg + displ]
semantic
The 2008 May effective address (offset) is obtained by adding the contents of the ICS212 2222 two registers and the displ value
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