1 / 12

Chapter 9

Chapter 9. Question 1. Copy 20 doublewords from source to target. .data source DWORD 20 DUP(?) target DWORD 20 DUP(?) .code. Question 1. Copy 20 doublewords from source to target. .data source DWORD 20 DUP(?) target DWORD 20 DUP(?) .code cld ; direction = forward

paloma
Download Presentation

Chapter 9

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. Chapter 9

  2. Question 1 Copy 20 doublewords from source to target. .data source DWORD 20 DUP(?) target DWORD 20 DUP(?) .code

  3. Question 1 Copy 20 doublewords from source to target. .data source DWORD 20 DUP(?) target DWORD 20 DUP(?) .code cld ; direction = forward movecx,LENGTHOF source ; set REP counter movesi,OFFSET source movedi,OFFSET target rep movsd

  4. Direction 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 ESI High memory address (STD) Low memory address (CLD) …… …… …… …… EDI Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

  5. Question 2 Comparing a Pair of Doublewords If source > target, the code jumps to label L1; otherwise, it jumps to label L2 .data source DWORD 1234h target DWORD 5678h .code movesi,OFFSET source movedi,OFFSET target ; missing code Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

  6. Question 2 Comparing a Pair of Doublewords If source > target, the code jumps to label L1; otherwise, it jumps to label L2 .data source DWORD 1234h target DWORD 5678h .code movesi,OFFSET source movedi,OFFSET target cmpsd ; compare doublewords ja L1 ; jump if source > target jmp L2 ; jump if source <= target Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

  7. CMPSB, 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) Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

  8. Question 3 Comparing Arrays Use a REPE (repeat while equal) prefix to compare corresponding elements of two arrays. .data source DWORD COUNT DUP(?) target DWORD COUNT DUP(?) .code movecx,COUNT ; repetition count movesi,OFFSET source movedi,OFFSET target cld ; direction = forward repecmpsd ; repeat while equal Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

  9. 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 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

  10. Exercise Write a procedure str_cmp to perform comparison for two null-terminated strings. Return value in EAX: -1: if str1 < str2 0 : if str1 == str2 1: if str1 > str2 (1) Define a prototype of str_cmp. (2) Write the procedure content.

  11. Exercise Write a procedure to perform comparison for two null-terminated strings. Return value in EAX: -1: if str1 < str2 0 : if str1 == str2 1: if str1 > str2 str1 str1 str1 str2 str2 str2

  12. Exercise Return value in EAX: -1: if str1 < str2; 0 : if str1 == str2; 1: if str1 > str2. This version is wrong. Why? str_cmp PROC C, str1: PTR BYTE, str2: PTR BYTE movesi, str1 movedi, str2 movbl, [esi] mov dl, [edi] Cmpbl, dl jae L0 moveax, -1 jump exit0 ret str_cmp L0: jne L1 moveax, 0 jmp exit0 L1: moveax, 1 exit0: ret

More Related