Download Arrays construction on mips and more Lecture notes Assembly Language Programming in PDF only on Docsity!
Assembly Language for Intel-Assembly Language for Intel
-BasedBased
Computers, 4Computers, 4
thth^ EditionEdition
Chapter 9: Strings and Arrays
Kip R. Irvine
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Chapter Overview^ Chapter Overview• String Primitive Instructions• Selected String Procedures• Two-Dimensional Arrays• Searching and Sorting Integer Arrays
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
MOVSB, MOVSW, and MOVSD^ MOVSB, MOVSW, and MOVSD
(1 of 2)(1 of 2)
•^ The MOVSB, MOVSW, and MOVSD instructionscopy data from the memory location pointed to byESI to the memory location pointed to by EDI.^ .datasource DWORD 0FFFFFFFFhtarget DWORD ?.codemov esi,OFFSET sourcemov edi,OFFSET targetmovsd
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
MOVSB, MOVSW, and MOVSD^ MOVSB, MOVSW, and MOVSD
(2 of 2)(2 of 2)
•^ ESI and EDI are automatically incremented ordecremented:•^ MOVSB increments/decrements by 1•^ MOVSW increments/decrements by 2•^ MOVSD increments/decrements by 4
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Using a Repeat Prefix^ Using a Repeat Prefix
•^ REP (a repeat prefix) can be inserted just beforeMOVSB, MOVSW, or MOVSD.•^ ECX controls the number of repetitions•^ Example: Copy 20 doublewords from source to target^ .datasource DWORD 20 DUP(?)target DWORD 20 DUP(?).codecld^
; direction = forward mov ecx,LENGTHOF source
; set REP counter mov esi,OFFSET sourcemov edi,OFFSET targetrep movsd
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Your turn...^ Your turn...
•^ Use MOVSD to delete the first element of the followingdoubleword array. All subsequent array values must bemoved one position forward toward the beginning of thearray:^ 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,(LENGTHOF array) - 1mov esi,OFFSET array+4mov edi,OFFSET arrayrep movsd
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Comparing a Pair of Doublewords^ Comparing a Pair of
Doublewords
.datasource DWORD 1234htarget DWORD 5678h.codemov esi,OFFSET sourcemov edi,OFFSET targetcmpsd^
; compare doublewords ja L^
; jump if source > target jmp L^
; jump if source <= target
If source > target, the code jumps to label L1; otherwise,it jumps to label L
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Comparing Arrays^ Comparing Arrays
.datasource DWORD COUNT DUP(?)target DWORD COUNT DUP(?).codemov ecx,COUNT^
; repetition count mov esi,OFFSET sourcemov edi,OFFSET targetcld^
; direction = forward repe cmpsd^
; repeat while equal
Use a REPE (repeat while equal) prefix to comparecorresponding elements of two arrays.
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Example: Comparing Two Strings^ Example: Comparing Two Strings
(2 of 3)(2 of 3)
.codemain PROCcld^
; direction = forward mov esi,OFFSET sourcemov edi,OFFSET destmov cx,LENGTHOF sourcerepe cmpsbjb source_smallermov edx,OFFSET str
;^ "source is not smaller" jmp donesource_smaller:mov edx,OFFSET str
; "source is smaller" done:call WriteStringexitmain ENDPEND main
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Example: Comparing Two Strings^ Example: Comparing Two Strings
(3 of 3)(3 of 3)
•^ The following diagram shows the final values of ESIand EDI after comparing the strings:MA^ M
R^ T^ I^ N A R^ T^ I^ N^ E^ Z
MA^ R^ T^ I^ N MA^ R^ T^ I^ N After^ E^ Z Before Source:SI Dest:DI
SI DI Before^
After
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
SCASB Example^ SCASB Example
.dataalpha BYTE "ABCDEFGH",0.codemov edi,OFFSET alphamov al,'F'^
; search for 'F' mov ecx,LENGTHOF alphacldrepne scasb^
; repeat while not equal jnz quitdec edi^
; EDI points to 'F'
Search for the letter 'F' in a string named alpha:What is the purpose of the JNZ instruction?
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
STOSB, STOSW, and STOSD^ STOSB, STOSW, and STOSD• The STOSB, STOSW, and STOSD instructions storethe contents of AL/AX/EAX, respectively, in memoryat the offset pointed to by EDI.• Example: fill an array with 0FFh .dataCount = 100string1 BYTE Count DUP(?).codemov al,0FFh^
; value to be stored mov edi,OFFSET string
; ES:DI points to target mov ecx,Count^
; character count cld^
; direction = forward rep stosb^
; fill with contents of AL
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Array Multiplication Example^ Array Multiplication Example .dataarray DWORD 1,2,3,4,5,6,7,8,9,10multiplier DWORD 10.codecld^
; direction = up mov esi,OFFSET array
; source index mov edi,esi^
; destination index mov ecx,LENGTHOF array
; loop counter L1: lodsd^
; copy [ESI] into EAX mul multiplier
; multiply by a value stosd^
; store EAX at [EDI]
Multiply each element of a doubleword array by aconstant value. loop L
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Your turn...^ Your turn...
•^ Write a program that converts each unpacked binary-coded decimal byte belonging to an array into anASCII decimal byte and copies it to a new array.^ mov esi,OFFSET arraymov edi,OFFSET destmov ecx,LENGTHOF arraycldL1: lodsb^
; load into AL or al,30h^
; convert to ASCII stosb^
; store into memory .dataarray BYTE 1,2,3,4,5,6,7,8,9dest^ BYTE (LENGTHOF array) DUP(?)loop L