1 / 60

4345 Assembly Language

4345 Assembly Language. Strings and Arrays. Dr. Esam Al_Qaralleh CE Department Princess Sumaya University for Technology. String Instructions. String is a collection of bytes, words, or long-words that can be up to 64KB in length.

cheung
Download Presentation

4345 Assembly Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 4345 Assembly Language Strings and Arrays Dr. Esam Al_Qaralleh CE Department Princess Sumaya University for Technology

  2. String Instructions • String is a collection of bytes, words, or long-words that can be up to 64KB in length • String instructions can have at most two operands. One is referred to as source string and the other one is called destination string • Source string must locate in Data Segment and SI register points to the current element of the source string • Destination string must locate in Extra Segment and DI register points to the current element of the destination string DS : SI ES : DI S S 0510:0000 53 02A8:2000 53 0510:0001 48 H 02A8:2001 48 H 0510:0002 4F 02A8:2002 4F O O 50 50 0510:0003 02A8:2003 P P P P 50 50 0510:0004 02A8:2004 E I 0510:0005 45 02A8:2005 49 52 R 4E N 0510:0006 02A8:2006 Source String Destination String

  3. String Instructions • Five string instructions LODS LOaD String source STOS STOre String destination MOVS MOVe String source & destination CMPS CoMPare String source & destination SCAS SCAn String destination • Specifying operands • 32-bit segments: DS:ESI = source operand ES:EDI = destination operand • 16-bit segments: DS:SI = source operand ES:DI = destination operand

  4. String Instructions (cont’d) • Each string instruction • Can operate on 8-, 16-, or 32-bit operands • Updates index register(s) automatically • Byte operands: increment/decrement by 1 • Word operands: increment/decrement by 2 • Doubleword operands: increment/decrement by 4 • Direction flag • DF = 0: Forward direction (increments index registers) • DF = 1: Backward direction (decrements index registers) • Two instructions to manipulate DF std set direction flag (DF = 1) cld clear direction flag (DF = 0)

  5. Direction Flag • Direction Flag (DF) is used to control the way SI and DI are adjusted during the execution of a string instruction • DF=0, SI and DI will auto-increment during the execution; otherwise, SI and DI auto-decrement • Instruction to set DF: STD; Instruction to clear DF: CLD • Example: CLD MOV CX, 5 REP MOVSB DS : SI S 0510:0000 53 SI CX=5 0510:0001 48 H SI CX=4 0510:0002 4F O SI CX=3 50 0510:0003 P SI CX=2 At the beginning of execution, DS=0510H and SI=0000H P 50 SI CX=1 0510:0004 E 0510:0005 45 SI CX=0 52 R 0510:0006 Source String

  6. Repetition Prefixes • String instructions can be repeated by using a repetition prefix • Two types • Unconditional repetition rep REPeat • Conditional repetition repe/repz REPeat while Equal REPeat while Zero repne/repnz REPeat while Not Equal REPeat while Not Zero

  7. Repetition Prefixes (cont’d) rep while (CX  0) execute the string instruction CX := CX-1 end while • CX register is first checked • If zero, string instruction is not executed at all • More like the JCXZ instruction

  8. Repeat Prefix Instructions • For Example: MOV CX, 5 REP MOVSB By the above two instructions, the microprocessor will execute MOVSB 5 times. • Execution flow of REP MOVSB:: While (CX!=0) { CX = CX –1; MOVSB; } Check_CX: If CX!=0 Then CX = CX –1; MOVSB; goto Check_CX; end if OR

  9. Repetition Prefixes (cont’d) repe/repz while (CX  0) execute the string instruction CX := CX-1 if (ZF = 0) then exit loop end if end while • Useful with cmps and scas string instructions

  10. Repetition Prefixes (cont’d) repne/repnz while (CX  0) execute the string instruction CX := CX-1 if (ZF = 1) then exit loop end if end while

  11. String Move Instructions • Three basic instructions • movs, lods, and stos Move a string (movs) • Format movs dest_string,source_string movsb ; operands are bytes movsw ; operands are words movsd ; operands are doublewords • First form is not used frequently • Source and destination pointed by DS:(E)SI and ES:(E)DI, respectively

  12. String Move Instructions (cont’d) movsb --- move a byte string ES:DI := (DS:SI) ; copy a byte if (DF=0) ; forward direction then SI := SI+1 DI := DI+1 else ; backward direction SI := SI-1 DI := DI-1 end if Flags affected: none

  13. String Instructions • MOVSB (MOVSW) Example DS : SI ES : DI S 0510:0000 53 0300:0100 MOV AX, 0510H MOV DS, AX MOV SI, 0 MOV AX, 0300H MOV ES, AX MOV DI, 100H CLD MOV CX, 5 REP MOVSB 0510:0001 48 H 0510:0002 4F O 50 0510:0003 P P 50 0510:0004 E 0510:0005 45 52 R 0510:0006 Source String Destination String

  14. Your turn . . . • Use MOVSD to delete the first element of the following double-word 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 .data array DWORD 1,1,2,3,4,5,6,7,8,9,10 .code cld mov cx,(LENGTHOF array) - 1 mov si,OFFSET array+4 mov di,OFFSET array rep movsd

  15. String Move Instructions (cont’d) Example .DATA string1 DB 'The original string',0 strLen EQU $ - string1 string2 DB 80 DUP (?) .CODE .STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov CX,strLen ; strLen includes NULL mov SI,OFFSET string1 mov DI,OFFSET string2 cld ; forward direction rep movsb

  16. String Move Instructions (cont’d) Load a String (LODS) • Copies the value from the source string at DS:(E)SI to • AL (lodsb) • AX (lodsw) • EAX (lodsd) • Repetition prefix does not make sense • It leaves only the last value in AL, AX, or EAX register

  17. String Move Instructions (cont’d) lodsb --- load a byte string AL := (DS:SI) ; copy a byte if (DF=0) ; forward direction then SI := SI+1 else ; backward direction SI := SI-1 end if Flags affected: none

  18. String Move Instructions (cont’d) Store a String (STOS) • Performs the complementary operation • Copies the value in • AL (lodsb) • AX (lodsw) • EAX (lodsd) to the destination string at ES:(E)DI • Repetition prefix can be used to initialize a block of memory

  19. String Move Instructions (cont’d) stosb --- store a byte string ES:DI := AL ; copy a byte if (DF=0) ; forward direction then DI := DI+1 else ; backward direction DI := DI-1 end if Flags affected: none

  20. String Move Instructions (cont’d) Example: Initializes array1 with -1 .DATA array1 DW 100 DUP (?) .CODE .STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov CX,100 mov DI,OFFSET array1 mov AX,-1 cld ; forward direction rep stosw

  21. String Move Instructions (cont’d) • In general, repeat prefixes are not useful with lods and stos • Used in a loop to do conversions while copying mov CX,strLen mov SI,OFFSET string1 mov DI,OFFSET string2 cld ; forward direction loop1: lodsb or AL,20H stosb loop loop1 done:

  22. String Compare Instruction cmpsb --- compare two byte strings Compare two bytes at DS:SI and ES:DI and set flags if (DF=0) ; forward direction then SI := SI+1 DI := DI+1 else ; backward direction SI := SI-1 DI := DI-1 end if Flags affected: As per cmp instruction (DS:SI)-(ES:DI)

  23. String Instructions • CMPSB (CMPSW) Example Assume: ES = 02A8H DI = 2000H DS = 0510H SI = 0000H DS : SI ES : DI S 0510:0000 53 S 53 02A8:2000 0510:0001 48 H 48 H 02A8:2001 0510:0002 4F 4F O O 02A8:2002 CLD MOV CX, 9 REPZ CMPSB 50 50 0510:0003 P P 02A8:2003 P 50 P 50 0510:0004 02A8:2004 E 0510:0005 45 I 49 02A8:2005 52 R 0510:0006 4E N 02A8:2006 What’s the values of CX after The execution? Source String Destination String

  24. Source is smaller Screen output: Example: Comparing Two Strings (1 of 3) This program compares two strings (source and destination). It displays a message indicating whether the lexical value of the source string is less than the destination string. .data source BYTE "MARTIN " dest BYTE "MARTINEZ" str1 BYTE "Source is smaller",0dh,0ah,0 str2 BYTE "Source is not smaller",0dh,0ah,0

  25. Example: Comparing Two Strings (2 of 3) • .code • main PROC • cld ; direction = forward • mov si,OFFSET source • mov di,OFFSET dest • mov cx,LENGTHOF source • repe cmpsb • jb source_smaller • mov dx,OFFSET str2 ; "source is not smaller" • jmp done • source_smaller: • mov dx,OFFSET str1 ; "source is smaller" • done: • call WriteString • exit • main ENDP • END main

  26. Example: Comparing Two Strings (3 of 3) • The following diagram shows the final values of SI and DI after comparing the strings:

  27. Your turn . . . • Modify the String Comparison program from the previous two slides. Prompt the user for both the source and destination strings. • Sample output: Input first string: ABCDEFG Input second string: ABCDDG The first string is not smaller.

  28. String Compare Instruction (cont’d) .DATA string1 DB 'abcdfghi',0 strLen EQU $ - string1 string2 DB 'abcdefgh',0 .CODE .STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov CX,strLen mov SI,OFFSET string1 mov DI,OFFSET string2 cld ; forward direction repe cmpsb dec SI dec DI ;leaves SI & DI pointing to the last character that differs

  29. String Compare Instruction (cont’d) .DATA string1 DB 'abcdfghi',0 strLen EQU $ - string1 - 1 string2 DB 'abcdefgh',0 .CODE .STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov CX,strLen mov SI,OFFSET string1 + strLen - 1 mov DI,OFFSET string2 + strLen - 1 std ; backward direction repne cmpsb inc SI ;Leaves SI & DI pointing to the first character that matches inc DI ; in the backward direction

  30. String Scan Instruction scasb --- Scan a byte string Compare AL to the byte at ES:DI and set flags if (DF=0) ; forward direction then DI := DI+1 else ; backward direction DI := DI-1 end if Flags affected: As per cmp instruction (DS:SI)-(ES:DI) • scasw uses AX and scasd uses EAX registers instead of AL

  31. SCASB Example Search for the letter 'F' in a string named alpha: .data alpha BYTE "ABCDEFGH",0 .code mov di,OFFSET alpha mov al,'F' ; search for 'F' mov cx,LENGTHOF alpha cld repne scasb ; repeat while not equal jnz quit dec di ; DI points to 'F' What is the purpose of the JNZ instruction?

  32. String Scan Instruction (cont’d) .DATA string1 DB 'abcdefgh',0 strLen EQU $ - string1 .CODE .STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov CX,strLen mov DI,OFFSET string1 mov AL,'e' ; character to be searched cld ; forward direction repne scasb dec DI ; leaves DI pointing to e in string1 Example 1

  33. String Scan Instruction (cont’d) .DATA string1 DB ' abc',0 strLen EQU $ - string1 .CODE .STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov CX,strLen mov DI,OFFSET string1 mov AL,' ' ; character to be searched cld ; forward direction repe scasb dec DI ; leaves DI pointing to the first non-blank character a Example 2

  34. Arrays

  35. Arrays One-Dimensional Arrays • Array declaration in HLL (such as C) int test_marks[10]; specifies a lot of information about the array: • Name of the array (test_marks) • Number of elements (10) • Element size (2 bytes) • Interpretation of each element (int i.e., signed integer) • Index range (0 to 9 in C) • You get very little help in assembly language!

  36. Arrays (cont’d) • In assembly language, declaration such as test_marks DW 10 DUP (?) only assigns name and allocates storage space. • You, as the assembly language programmer, have to “properly” access the array elements by taking element size and the range of subscripts. • Accessing an array element requires its displacement or offset relative to the start of the array in bytes

  37. To compute displacement, we need to know how the array is laid out Simple for 1-D arrays Assuming C style subscripts displacement = subscript * element size in bytes Arrays (cont’d)

  38. Multidimensional Arrays • We focus on two-dimensional arrays • Our discussion can be generalized to higher dimensions • A 53 array can be declared in C as int class_marks[5][3]; • Two dimensional arrays can be stored in one of two ways: • Row-major order • Array is stored row by row • Most HLL including C and Pascal use this method • Column-major order • Array is stored column by column • FORTRAN uses this method

  39. Multidimensional Arrays (cont’d)

  40. Multidimensional Arrays (cont’d) • Why do we need to know the underlying storage representation? • In a HLL, we really don’t need to know • In assembly language, we need this information as we have to calculate displacement of element to be accessed • In assembly language, class_marks DW 5*3 DUP (?) allocates 30 bytes of storage • There is no support for using row and column subscripts • Need to translate these subscripts into a displacement value

  41. Multidimensional Arrays (cont’d) • Assuming C language subscript convention, we can express displacement of an element in a 2-D array at row i and column j as displacement = (i * COLUMNS + j) * ELEMENT_SIZE where COLUMNS = number of columns in the array ELEMENT_SIZE = element size in bytes Example: Displacement of class_marks[3,1] element is (3*3 + 1) * 2 = 20

  42. Examples

  43. Reverse an array • Reverse and array of N elements, BX has the number of elements N, SI points to the array MOV DI, SI ; DI = SI DEC BX ; BX = N – 1 ADD DI, BX ;DI Points to the last element INC BX ; BX = N SHR BX, 1 ;BX = N/2 LOOP: MOV AX, [SI] XCHG AX, [DI] MOV [SI], AX INC SI DEC DI DEC BX JNZ LOOP

  44. TWO DIMENTIONAL ARRAY EXAMPLE • Suppose A is a 5x7 word array: 1) clear row 3. 2) clear column 4 1) the starting address of the row 3 is A+[(3-1) x 7 ] x 2 = A+28 MOV BX, 28 ; BX indexes row 3 XOR SI, SI ; SI will index the columns MOV CX, 7 ; number of elements CLEAR: MOV A[BX][SI], 0 ADD SI, 2 DEC CX JNZ CLEAR

  45. TWO DIMENTIONAL ARRAY EXAMPLE • Suppose A is a 5x7 word array: 1) clear row 3. 2) clear column 4 2) the starting address of column 4 is A+[(4-1)] x 2 = A+6 XOR BX, BX ; BX indexes rows MOV SI, 6 ; SI will index column 4 MOV CX, 5 ; number of elements CLEAR: MOV A[BX][SI], 0 ADD BX, 14 ;go to next row (7 elements * 2bytes) DEC CX JNZ CLEAR

  46. String Copy Example • Copy string1 into string2 • String1 DB ‘HELLO’ • String2 DB 5DUP(0) • LEA SI, STRING1 • LEA DI, STRING2 • MOV CX, 5 • CLD • REP MOVSB

  47. String Copy Example • Copy string1 into string2 in reverse order String1 DB ‘HELLO’ String2 DB 5DUP(0) LEA SI, STRING1+4 LEA DI, STRING2 CLD MOV CX, 5 MOVE: MOVSB ADD DI,2 DEC CX JNZ MOVE

  48. Alternative format: table BYTE 10h,20h,30h,40h,50h,60h,70h, 80h,90h,0A0h, 0B0h,0C0h,0D0h, 0E0h,0F0h NumCols = 5 Two-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, 0F0h NumCols = 5

  49. Two-Dimensional Table Example The following code loads the table element stored in row 1, column 2: RowNumber = 1 ColumnNumber = 2 mov bx,NumCols * RowNumber mov si,ColumnNumber mov al,table[bx + si]

  50. Searching and Sorting Integer Arrays • Bubble Sort • A simple sorting algorithm that works well for small arrays • Binary Search • A simple searching algorithm that works well for large arrays of values that have been placed in either ascending or descending order

More Related