assembly language for intel-based computers chapter 9: strings and arrays kip r. irvine

39
Assembly Language for Intel- Assembly Language for Intel- Based Computers Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Post on 20-Dec-2015

246 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Assembly Language for Intel-Based Assembly Language for Intel-Based ComputersComputers

Chapter 9: Strings and Arrays

Kip R. Irvine

Page 2: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 2Web site Examples

Chapter OverviewChapter Overview

• String Primitive Instructions• Selected String Procedures• Two-Dimensional Arrays• Searching and Sorting Integer Arrays

Page 3: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 3Web site Examples

String Primitive InstructionsString Primitive Instructions

• MOVSB, MOVSW, and MOVSD• CMPSB, CMPSW, and CMPSD• SCASB, SCASW, and SCASD• STOSB, STOSW, and STOSD• LODSB, LODSW, and LODSD

Page 4: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 4Web site Examples

MOVSB, MOVSW, and MOVSDMOVSB, MOVSW, and MOVSD (1 of 2) (1 of 2)

• The MOVSB, MOVSW, and MOVSD instructions copy data from the memory location pointed to by ESI to the memory location pointed to by EDI.

.datasource DD 0FFFFFFFFhtarget DD ?.codeMov ax,@dataMov ds,axMov es,ax

mov si,OFFSET sourcemov di,OFFSET targetmovsd

Page 5: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 5Web site Examples

MOVSB, MOVSW, and MOVSDMOVSB, MOVSW, and MOVSD (2 of 2) (2 of 2)

• ESI and EDI are automatically incremented or decremented:• MOVSB increments/decrements by 1

• MOVSW increments/decrements by 2

• MOVSD increments/decrements by 4

Page 6: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 6Web site Examples

Direction FlagDirection Flag

• The Direction flag controls the incrementing or decrementing of ESI and EDI.• DF = clear (0): increment ESI and EDI

• DF = set (1): decrement ESI and EDI

The Direction flag can be explicitly changed using the CLD and STD instructions:

CLD ; clear Direction flag

STD ; set Direction flag

Page 7: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 7Web site Examples

Using a Repeat PrefixUsing a Repeat Prefix

• REP (a repeat prefix) can be inserted just before MOVSB, MOVSW, or MOVSD.

• ECX controls the number of repetitions• Example: Copy 20 doublewords from source to target

.datasource DD 20 DUP(?)target DD 20 DUP(?).codecld ; direction = forwardmov ecx, 20 ; set REP countermov esi,OFFSET sourcemov edi,OFFSET targetrep movsd

Page 8: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 8Web site Examples

Your turn . . .Your turn . . .

• Use MOVSD to delete the first element of the following doubleword array. All subsequent array values must be moved one position forward toward the beginning of the array:

array DWORD 1,1,2,3,4,5,6,7,8,9,10

.dataarray DWORD 1,1,2,3,4,5,6,7,8,9,10.codecldmov ecx,10mov esi,OFFSET array+4mov edi,OFFSET arrayrep movsd

Page 9: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 9Web site Examples

CMPSB, CMPSW, and CMPSDCMPSB, CMPSW, and CMPSD

• The CMPSB, CMPSW, and CMPSD instructions each compare a memory operand pointed to by ESI to a memory operand pointed to by EDI.• CMPSB compares bytes

• CMPSW compares words

• CMPSD compares doublewords

• Repeat prefix often used• REPE (REPZ)

• REPNE (REPNZ)

Page 10: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 10Web site Examples

Comparing a Pair of DoublewordsComparing a Pair of Doublewords

.datasource DD 1234htarget DD 5678h

.codemov esi,OFFSET sourcemov edi,OFFSET targetcmpsd ; compare doublewordsja L1 ; jump if source > targetjmp L2 ; jump if source <= target

If source > target, the code jumps to label L1; otherwise, it jumps to label L2

Page 11: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 11Web site Examples

Comparing ArraysComparing Arrays

.datasource DD COUNT DUP(?)target DD COUNT DUP(?).codemov ecx,COUNT ; repetition countmov esi,OFFSET sourcemov edi,OFFSET targetcld ; direction = forwardrepe cmpsd ; repeat while equal

Use a REPE (repeat while equal) prefix to compare corresponding elements of two arrays.

Page 12: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 12Web site Examples

Example: Comparing Two StringsExample: Comparing Two Strings (1 of 3) (1 of 3)

.datasource db "MARTIN "dest db "MARTINEZ"

This program compares two strings (source and destination). It indicats whether the value of the source string is less than the destination string.

Page 13: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 13Web site Examples

Example: Comparing Two StringsExample: Comparing Two Strings (2 of 3) (2 of 3)

.codemain PROC

cld ; direction = forwardmov esi,OFFSET sourcemov edi,OFFSET destmov ecx,8repe cmpsbjb source_smallerjmp done ; "source is not smaller"

source_smaller:mov edx, -1 ; "source is smaller"

done:mov edx, 1

main ENDPEND main

Page 14: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 14Web site Examples

Example: Comparing Two StringsExample: Comparing Two Strings (3 of 3) (3 of 3)

• The following diagram shows the final values of ESI and EDI after comparing the strings:

M

M

A R T I N

A R T I N E Z

M

M

A R T I N

A R T I N E Z

AfterBefore

Source:

Dest:

ESI

EDI

ESI

EDI

Before After

Page 15: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 15Web site Examples

SCASB, SCASW, and SCASDSCASB, SCASW, and SCASD

• The SCASB, SCASW, and SCASD instructions compare a value in AL/AX/EAX to a byte, word, or doubleword, respectively, addressed by EDI.

• Useful types of searches:

• Search for a specific element in a long string or array.

• Search for the first element that does not match a given value.

Page 16: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 16Web site Examples

SCASB ExampleSCASB Example

.dataalpha BYTE "ABCDEFGH".codemov edi,OFFSET alphamov al,'F' ; search for 'F'mov ecx,8cldrepne scasb ; repeat while not equaljnz quitdec edi ; EDI points to 'F'

Search for the letter 'F' in a string named alpha:

What is the purpose of the JNZ instruction?

Page 17: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 17Web site Examples

STOSB, STOSW, and STOSDSTOSB, STOSW, and STOSD

• The STOSB, STOSW, and STOSD instructions store the contents of AL/AX/EAX, respectively, in memory at the offset pointed to by EDI.

• Example: fill an array with 0FFh

.dataCount = 100string1 BYTE Count DUP(?).codemov al,0FFh ; value to be storedmov edi,OFFSET string1 ; ES:DI points to targetmov ecx,Count ; character countcld ; direction = forwardrep stosb ; fill with contents of AL

Page 18: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 18Web site Examples

LODSB, LODSW, and LODSDLODSB, LODSW, and LODSD

• LODSB, LODSW, and LODSD load a byte or word from memory at ESI into AL/AX/EAX, respectively.

Mov al, [esi]

inc esi

Page 19: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 19Web site Examples

Array Multiplication ExampleArray Multiplication Example

.dataarray DD 1,2,3,4,5,6,7,8,9,10multiplier DD 10.code

cld ; direction = upmov esi,OFFSET array ; source indexmov edi,esi ; destination indexmov ecx,10 ; loop counter

L1: lodsd ; copy [ESI] into EAXmul multiplier ; multiply by a valuestosd ; store EAX at [EDI]loop L1

Multiply each element of a doubleword array by a constant value.

Page 20: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 20Web site Examples

Your turn . . .Your turn . . .

• Write a program that converts each binary-coded decimal byte belonging to an array into an ASCII decimal byte and copies it to a new array.

mov esi,OFFSET arraymov edi,OFFSET destmov ecx, 9cld

L1: lodsb ; load into ALor al,30h ; convert to ASCIIstosb ; store into memoryloop L1

.dataarray db 1,2,3,4,5,6,7,8,9dest db 9 DUP(?)

Page 21: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 21Web site Examples

What's NextWhat's Next

• String Primitive Instructions• Selected String Procedures• Two-Dimensional Arrays• Searching and Sorting Integer Arrays

Page 22: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 22Web site Examples

Selected String ProceduresSelected String Procedures

• Str_compare Procedure• Str_length Procedure• Str_copy Procedure• Str_trim Procedure• Str_ucase Procedure

Page 23: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 23Web site Examples

Str_length ProcedureStr_length Procedure• Calculates the length of a null-terminated string and

returns the length in the EAX register.

Str_length PROC ; input: di offset of the string; output: axpush dimov ax,0 ; character count

L1:cmp byte ptr [di],0 ; end of string?je L2 ; yes: quitinc di ; no: point to nextinc ax ; add 1 to countjmp L1

L2: pop diret

Str_length ENDP

Page 24: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 24Web site Examples

Str_trim ProcedureStr_trim Procedure

• The Str_trim procedure removes all occurrences of a selected trailing character from a null-terminated string.

.datamyString BYTE "Hello###",0.code

Mov bl,’#’Mov di,offset myString Call Str_trim

Example:

Page 25: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 25Web site Examples

Str_trim ProcedureStr_trim Procedure

• Str_trim checks a number of possible cases (shown here with # as the trailing character):• The string is empty.

• The string contains other characters followed by one or more trailing characters, as in "Hello##".

• The string contains only one character, the trailing character, as in "#"

• The string contains no trailing character, as in "Hello" or "H".

• The string contains one or more trailing characters followed by one or more nontrailing characters, as in "#H" or "###Hello".

Page 26: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 26Web site Examples

Str_trim Source CodeStr_trim Source Code

Str_trim PROC call Str_length ; returns length in EAX

cmp ax,0 ; zero-length string?je L2 ; yes: exitmov cx, ax ; no: counter = string lengthdec axadd di, ax ; EDI points to last charmov al,bl ; char to trimstd ; direction = reverserepe scasb ; skip past trim characterjne L1 ; removed first character?dec di ; adjust EDI: ZF=1 && ECX=0

L1: mov BYTE PTR [di+2],0 ; insert null byteL2: retStr_trim ENDP

Page 27: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 27Web site Examples

Str_ucase ProcedureStr_ucase Procedure

• The Str_ucase procedure converts a string to all uppercase characters. It returns no value.

.datamyString BYTE "Hello",0.code

mov si, offset myStringcall Str_ucase

Example:

Page 28: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 28Web site Examples

Str_ucase Source CodeStr_ucase Source Code

Str_ucase PROC

L1: mov al,[si] ; get charcmp al,0 ; end of string?je L3 ; yes: quitcmp al,'a' ; below "a"?jb L2cmp al,'z' ; above "z"?ja L2and BYTE PTR [si],11011111b ; convert the char

L2: inc si ; next charjmp L1

L3: retStr_ucase ENDP

Page 29: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 29Web site Examples

What's NextWhat's Next

• String Primitive Instructions• Selected String Procedures• Two-Dimensional Arrays• Searching and Sorting Integer Arrays

Page 30: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 30Web site Examples

Two-Dimensional ArraysTwo-Dimensional Arrays

• Base-Index Operands• Base-Index Displacement

Page 31: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 31Web site Examples

Base-Index OperandBase-Index Operand

• A base-index operand adds the values of two registers (called base and index), producing an effective address.

table BYTE 10h, 20h, 30h, 40h, 50h BYTE 60h, 70h, 80h, 90h, 0A0h BYTE 0B0h, 0C0h, 0D0h, 0E0h, 0F0hNumCols = 5

Page 32: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 32Web site Examples

Base-Index OperandBase-Index Operand

tableB BYTE 10h, 20h, 30h, 40h, 50h BYTE 60h, 70h, 80h, 90h, 0A0h BYTE 0B0h, 0C0h, 0D0h, 0E0h, 0F0hNumCols = 5

Page 33: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 33Web site Examples

Base-Index-Displacement OperandBase-Index-Displacement Operand

• A base-index-displacement operand adds base and index registers to a constant, producing an effective address.

• Common formats:

[ base + index + displacement ]

displacement [ base + index ]

Page 34: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 34Web site Examples

Two-Dimensional Table ExampleTwo-Dimensional Table Example

Imagine a table with three rows and five columns. The data can be arranged in any format on the page:

table BYTE 10h, 20h, 30h, 40h, 50h BYTE 60h, 70h, 80h, 90h, 0A0h BYTE 0B0h, 0C0h, 0D0h, 0E0h, 0F0hNumCols = 5

table BYTE 10h,20h,30h,40h,50h,60h,70h, 80h,90h,0A0h, 0B0h,0C0h,0D0h, 0E0h,0F0hNumCols = 5

Alternative format:

Page 35: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 35Web site Examples

Two-Dimensional Table ExampleTwo-Dimensional Table Example

The following code loads the table element stored in row 1, column 2:

RowNumber = 1ColumnNumber = 2

mov ebx,NumCols * RowNumbermov esi,ColumnNumbermov al,table[ebx + esi]

Page 36: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 36Web site Examples

What's NextWhat's Next

• String Primitive Instructions• Selected String Procedures• Two-Dimensional Arrays• Sorting Integer Arrays

Page 37: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 37Web site Examples

Bubble SortBubble Sort

Each pair of adjacent values is compared, and exchanged if the values are not ordered correctly:

Page 38: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 38Web site Examples

Bubble Sort PseudocodeBubble Sort Pseudocode

N = array size, cx1 = outer loop counter, cx2 = inner loop counter:

cx1 = N - 1while( cx1 > 0 ){ esi = address(array) cx2 = cx1 while( cx2 > 0 ) { if( array[esi] < array[esi+4] ) exchange( array[esi], array[esi+4] ) add esi,4 dec cx2 } dec cx1}

Page 39: Assembly Language for Intel-Based Computers Chapter 9: Strings and Arrays Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 39Web site Examples

Bubble Sort ImplementationBubble Sort ImplementationBubbleSort PROC

; input: SI offset array; ecx countdec ecx ; decrement count by 1mov edi,esi

L1: push ecx ; save outer loop countmov esi,edi ; point to first value

L2: mov eax,[esi] ; get array valuecmp [esi+4],eax ; compare a pair of valuesjge L3 ; if [esi] <= [edi], skipxchg eax,[esi+4] ; else exchange the pairmov [esi],eax

L3: add esi,4 ; move both pointers forwardloop L2 ; inner looppop ecx ; retrieve outer loop countloop L1 ; else repeat outer loop

L4: retBubbleSort ENDP