;------------------------------------------------------------------------------ ; This program demonstrates using string instructions in 32 bit x86 Assembly. ; The procedures to get and put both characters and strings are found in the ; "included" file "strio32.inc. ; ; Written by Joe Toone 10/20/2003 11:41:50 AM ;------------------------------------------------------------------------------ TITLE strinc32.asm CR equ 0dh ;ASCII 13 for carriage return LF equ 0ah ;ASCII 10 for line feed MAXSTR equ 50h ;Maximum string length of 80 chars STD_OUTPUT equ -11d ;Function number for monitor output STD_INPUT equ -10d ;Function number for keyboard input .586 ;Allow for Pentium instrucitons .MODEL FLAT ;Memory model is FLAT (4GB) INCLUDE strio32.inc ;Character and String I/O included .STACK 1024h ;One kilobyte of hex bytes for stack ;------------------------------------------------------------------------------ ;NOTE: All strings in this sample will be NULL (0) terminated. ;------------------------------------------------------------------------------ .DATA Prompt BYTE CR,LF,'Enter a string of less than 80 characters --> ',0 Str1Contains BYTE CR,LF,'Contents of String1 --> ',0 Str2Contains BYTE CR,LF,'Contents of String2 --> ',0 Str3Contains BYTE CR,LF,'Contents of String3 --> ',0 Compare1 BYTE CR,LF,LF,'Compare String1 to String2',0 Compare2 BYTE CR,LF,'Compare String1 to String3',0 Equal BYTE ' --> EQUAL.',CR,LF,0 NotEqual BYTE ' --> NOT EQUAL.',0 NewLine BYTE CR, LF, 0 String1 BYTE MAXSTR DUP(0) ;80 bytes of storage String2 BYTE MAXSTR DUP(0) ;80 bytes of storage String3 BYTE MAXSTR DUP(0) ;80 bytes of storage ;-------- Main Body ----------------------------------------------------------- ; Given :Nothing ; Process :Main Body - contains inline code and procedure calls ; Return :Nothing ;------------------------------------------------------------------------------ .CODE _main: lea esi,Prompt ;Set esi (source index) to Prompt message call PrintString ;Call the routine to print a string lea esi,String1 ;Set esi (source index) to String1 call GetString ;Call the routine to get a string lea esi,NewLine ;Pointer to NewLine String call PrintString ;Execute a carriage return call CopyStr ;Make copies of the string call SearchSwap ;Search for and swap case for 'a' call ShowAll ;Display the contents of all strings call CmpStr ;Compare the three strings INVOKE ExitProcess, 0 ; exit with return code 0 ;-------- Procedure to Copy Strings ------------------------------------------- ; Given :Nothing ; Process :Copy the contents of String 1 into String2 and String3 ; Return :Nothing ;------------------------------------------------------------------------------ CopyStr PROC NEAR32 pushad ;Save the registers lea esi,String1 ;Set esi to source string address lea edi,String2 ;Set edi to destination string address mov ecx,MAXSTR ;80 bytes to be moved cld ;Set copy direction to FORWARD rep movsb ;Copy string1 to string2 lea esi,String1 ;Set esi to source string address lea edi,String3 ;Set edi to destination string address mov ecx,MAXSTR ;80 bytes to be moved rep movsb ;Copy string1 to string3 popad ;Restore the registers ret ;Return to caller CopyStr ENDP ;-------- Procedure to Search Strings ----------------------------------------- ; Given :Nothing ; Process :Search for and Convert lower case a's to UPPER case ; Return :Nothing ;------------------------------------------------------------------------------ SearchSwap PROC NEAR32 pushad ;Save the registers lea edi,String2 ;Set edi to start of string to search cld ;Set search direction to FORWARD mov ecx,MAXSTR ;Maximum of 80 chars to search NextCk: mov al,'a' ;Place letter to find in al repnz scasb ;Scan the byte jnz SwapDone ;All bytes have been checked dec edi ;Point to byte just scaned sub al,20h ;Convert from lower to upper case stosb ;Write converted char back to string inc ecx ;Add 1 removed by STOSB back to ecx loop NextCk ;Go check the next char SwapDone: ;All a's have been converted popad ;Restore the registers ret ;Return to caller SearchSwap ENDP ;-------- Procedure to Display Strings ---------------------------------------- ; Given :Nothing ; Process :Display the contents of all three strings ; Return :Nothing ;------------------------------------------------------------------------------ ShowAll PROC NEAR32 pushad ;Save the registers lea esi,Str1Contains ;Set esi to Str1Contains message call PrintString ;Print the string lea esi,String1 ;Set esi to String1 call PrintString ;Print the string lea esi,Str2Contains ;Set esi to Str2Contains message call PrintString ;Print the string lea esi,String2 ;Set esi to String2 call PrintString ;Print the string lea esi,Str3Contains ;Set esi to Str3Contains message call PrintString ;Print the string lea esi,String3 ;Set esi to String3 call PrintString ;Print the string popad ;Restore the registers ret ;Return to caller ShowAll ENDP ;-------- Procedure to Compare Strings ---------------------------------------- ; Given :Nothing ; Process :Compare String1 to String 2 & String1 to String 3 for equality ; Return :Nothing ;------------------------------------------------------------------------------ CmpStr PROC NEAR32 pushad ;Save the registers lea esi,Compare1 ;Set esi for first compare message call PrintString ;Print the message lea esi,String1 ;Set esi to address of String1 lea edi,String2 ;Set edi to address of String2 call Compare ;Call routine to compare strings lea esi,Compare2 ;Set esi for second compare message call PrintString ;Print the message lea esi,String1 ;Set esi to address of String1 lea edi,String3 ;Set edi to address of String2 call Compare ;Call routine to compare strings popad ;Restore the registers ret ;Return to caller CmpStr ENDP ;-------- Procedure Compare --------------------------------------------------- ; Given :The address of one string in esi and another string in edi ; Process :Compare the strings pointed to by esi and edi for equality, then ; print a message stating whether or not they are equal ; Return :Nothing ;------------------------------------------------------------------------------ Compare PROC NEAR32 pushad ;Save the registers cld ;Set compare direction to forward mov ecx,MAXSTR ;Compare the 80 bytes repz cmpsb ;Compare the two strings jnz Different ;Jump to not equal message lea esi,Equal ;Address of equal message jmp Result ;Go print the Result Different:lea esi,NotEqual ;Address of strings not equal message Result: call PrintString ;Go print the Message popad ;Restore the registers ret ;Return to caller Compare ENDP PUBLIC _main END ;----------------- Sample Run ------------------------------------------------- Enter a string of less than 80 characters --> apples and oranges Contents of String1 --> apples and oranges Contents of String2 --> Apples And orAnges Contents of String3 --> apples and oranges Compare String1 to String2 --> NOT EQUAL. Compare String1 to String3 --> EQUAL. Enter a string of less than 80 characters --> peaches and pears Contents of String1 --> peaches and pears Contents of String2 --> peAches And peArs Contents of String3 --> peaches and pears Compare String1 to String2 --> NOT EQUAL. Compare String1 to String3 --> EQUAL.