;***************************************************************************** ; This program is designed to show how to input a string from the keyboard and ; and print a string on the monitor. This program must be linked to the ; io32lib.asm program and kernel32.lib in order to work. ; ; To build the program, issue the following commands. ; C:\temp>ml /c /coff linkdemo.asm ; C:\temp>ml /c /coff io32lib.asm ; C:\temp>link32 /entry:main /out:linkdemo.exe linkdemo.obj io32lib.obj kernel32.lib ; ; Written by Joe Toone 10/8/2003 3:30:35 PM ;***************************************************************************** TITLE linkdemo.asm ;file name of program LF equ 0ah ;ASCII 10 - newline character CR equ 0dh ;ASCII 13 - carriage return character .586 ;Allow for Pentium instrucitons .MODEL FLAT ;Memory model is FLAT (4GB) ExitProcess PROTO NEAR32 stdcall, ;Prototype for Exit of OS dwExitCode:DWORD .STACK 4096h ;4k hex bytes for stack .DATA ;Data segment begins here Menu BYTE LF,LF,CR,'[1] Character I/O',CR,LF BYTE '[2] String I/O',CR,LF BYTE '[3] ASCII to Integer and Integer to ASCII',CR,LF BYTE '[4] Exit',CR,LF,LF BYTE 'Your Selection Please? ',0 CharInput BYTE CR,LF,'Type a character please --> ',0 StrInput BYTE CR,LF,'Type a String please --> ',0 Result BYTE LF,LF,CR,'So you decided to type ==> ',0 Error BYTE CR,LF,LF,"On the Menu, there ain't no stinkin -> ",0 InputNum BYTE CR,LF,LF,'Enter an integer (less than 4294967295) --> ',0 Result1 BYTE CR,LF,LF,'The number preceeding ',0 Result2 BYTE ' is ',0 Result3 BYTE ' and the following number is ',0 NewLine BYTE LF,LF,CR,0 UserString BYTE 256d dup(?) AsciiNum BYTE 256d dup(0) KeyPress BYTE ? CharBuffer BYTE ? SaveNumber DWORD 0h OneLess DWORD 0h OneMore DWORD 0h MAXSTR DWORD 256d ;Accept up to 256 characters PUBLIC MAXSTR EXTRN GetChar:NEAR32,PutChar:NEAR32,GetString:NEAR32,PrintString:NEAR32 EXTRN Ascii2Int:NEAR32,Int2Ascii:NEAR32 ;************************** Main Body of Program ***************************** ; Given : Nothing ; Process: Main Body - calls procedures to do the processing ; Return : Nothing ;***************************************************************************** .CODE ;executable section begins here _main: lea esi,Menu ;point to the question prompt call PrintString ;go print the string lea esi,CharBuffer ;pointer to character buffer in data call GetChar ;get the character mov al,CharBuffer ;copy character to AL mov KeyPress,al ; and save a copy in KeyPress cmp KeyPress,'1' ;was 1 typed? je DoCharIO ; yes, do the character thing cmp KeyPress,'2' ;was 2 typed? je DoStringIO ; yes, do the string thing cmp KeyPress,'3' ;was 3 typed? je DoAsciiInt ; yes, do the string thing cmp KeyPress,'4' ;was 4 typed? je ByeBye ; yes, we're outta here. lea esi,Error ;else point to the error string call PrintString ; go print the string lea esi, CharBuffer ; and what they typed call PutChar ; jmp _main ;jump back to the menu for another try DoCharIO:lea esi, CharInput ;pointer to character buffer call PrintString ;go print the string lea esi,CharBuffer ;pointer to UserString in data call GetChar ;get the character input jmp ShowResult ;go print the result DoStringIO:lea esi, StrInput ;pointer to character buffer call PrintString ;go print the string lea esi,UserString ;pointer to UserString in data call GetString ;get the string jmp ShowResult ;go print the result DoAsciiInt:lea esi, InputNum ;pointer to get number prompt call PrintString ;go print the screen lea esi,AsciiNum ;pointer to AsciiNum in data call GetString ;get the string lea esi,AsciiNum ;pointer to AsciiNum in Data lea edi,SaveNumber ;pointer to DWORD storage in data call Ascii2Int ;Convert the string to integer mov eax,SaveNumber ;Copy number to eax mov OneMore,eax ;Copy number to OneMore variable inc OneMore ; and add one to it mov OneLess,eax ;Copy number to OneLess variable dec OneLess ; and subtract one from it call IntResult ;Go display all the number stuff jmp NextMenu ;go back to the menu ShowResult:lea esi, Result ;pointer to result message call PrintString ;go print the string cmp KeyPress,'2' ;if we are doing strings, je StrOut ; then go print the UserString lea esi, CharBuffer ;else point to their character call PutChar ; and print it jmp NextMenu ; then go to the menu StrOut: lea esi, UserString ;pointer to string buffer call PrintString ;go print the string NextMenu:jmp _main ;go back to the menu ByeBye: INVOKE ExitProcess, 0 ; exit with return code 0 ;************************ Procedure Output the Result ************************* ; Given : Nothing ; Process : Display a Formatted String containing the Calculated Numbers ; Return : Nothing ;****************************************************************************** IntResult PROC NEAR32 pushad ;Save the contents of all registers pushfd ; lea esi,Result1 ;Address of Result1 string call PrintString ;Display the string lea esi,SaveNumber ;Point to original number lea edi,AsciiNum ;Point to temp location for ASCII number call Int2Ascii ;Convert the integer to ascii lea esi,AsciiNum ;Point to the temp ascii number call PrintString ; and go print it lea esi,Result2 ;Address of Result2 string call PrintString ;Display the string lea esi,OneLess ;Point to decremented number lea edi,AsciiNum ;Point to temp location for ASCII number call Int2Ascii ;Convert the integer to ascii lea esi,AsciiNum ;Point to the temp ascii number call PrintString ; and go print it lea esi,Result3 ;Address of Result3 string call PrintString ;Display the string lea esi,OneMore ;Point to incremented number lea edi,AsciiNum ;Point to temp location for ASCII number call Int2Ascii ;Convert the integer to ascii lea esi,AsciiNum ;Point to the temp ascii number call PrintString ; and go print it popfd ;Restore the registers popad ; ret ;Return to Calling procedure IntResult ENDP Public _main END ;Code segment ends