Description of the 5.3.2 process
The Closefile:closefile process closes a previously opened file. The file is identified by a file handle, and the file handle is passed through the eax. If the file is closed successfully, the return value in EAX is a value other than 0.
The CLRSCR:CLRSCR procedure is used to clear the contents of the console window, which is usually used at the beginning and end of the program. If called at other times, it is best to pause the program before calling. The user can see the information already before the screen is erased.
eg
Include Irvine32.Inc ;Create a user-defined typepbyte typedef ptr BYTE pword typedef ptr WORD Pdword TYPEDEF ptr DWORD. Data arrayb BYTE 10h,20h,30h arrayw WORD1,2,3Arrayd DWORD4,5,6 ;create some pointer typesptr1 pbyte arrayBptr2 pword arrayWptr3 pdword arrayd.code main1 PROCmoveax0 movEbx0 movEcx0 movEsi0 movESI,PTR1movAl,[esi]movESI,PTR2movBx,[esi]movESI,PTR3movEcx,[esi]PagerDumpregsPagerWaitmsg; PagerCLRSCR exit main1 ENDP END main1
CRLF: The procedure positions the cursor at the start of the next line in the console window, which is achieved by writing a string containing 0dh,0ah two characters through the standard output.
The Creatteoutput:createoutputfile process creates a disk file and opens in output mode. The offset address of the file name to be created is passed through edx. When the procedure returns, EAX contains a valid file handle if the file was successfully created. If creation fails, the value in EAX is invalid_handle_value a predefined constant
Include Irvine32. Inc . Data " Newfile.txt ",0. Data? Handle DWORD?. Code main PROC mov edx, OFFSET filename call Createoutputfile Call closefile exit main ENDP END main
This program will create a corresponding TXT file in your project name after successful operation.
Delay: Pauses the program's specified number of milliseconds before calling the procedure to initialize the EAX to the expected pause, measured in milliseconds
Dumpmen: Displays the contents of a piece of memory in the console window in hexadecimal number format. Before calling, the ESI needs to be set to the start address of the memory, ECX set to the number of elements ebx set to the element size.
eg
include Irvine32. inc .data array WORD 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ,0ah,0bh.code main PROC mov Esi,offset array mov Ecx,lengthof array mov Ebx,type array call Dumpmem exit main ENDP end main
Dump of Offset 00a74000
-------------------------------
0001 0002 0003 0004 0005 0006 0007 0008 0009 000A 000B
Dumpregs: Displays the contents of the EAX,EBX,ECX,EDX,ESI,EDI,EBP,ESP,EIP,EFL register in hexadecimal number format and displays values for carry, sign bit, 0, and overflow bit flags at the same time.
Getcommandtail: Copies the program's commands to a string that ends in a null character, sets the carry flag if the command behaves empty, or clears the carry flag, the process
WriteString: Displays a string on the console
Include Irvine32. Inc . Data " Welcome to masm",0. Code main PROC mov edx,offset FileName call writestring call Crlf exit Main ENDP END Main
Operation Result:
Welcome to MASM
Please press any key to continue ...
And so on. Use of functions
5.3.3 Library Test Program
Eg.1
Change the color of the output text to blue yellow, then display the contents of an array in hexadecimal numbers, and finally prompt the user to enter a signed integer, and then repeat the integer in decimal, hexadecimal, and binary number formats, respectively.
Include Irvine32.Inc. Data Arrayd DWORD 1000h,2000h,3000h,4000h prompt1 BYTE"Enter a 32Bit signed integer:",0dwordval DWORD?. Code main PROC;set text color to blue bottom yellow word movEAX, yellow+ (blue* -) PagerSetTextColorPagerCLRSCR;using the Dumpmem procedure to display the contents of an array movEsi,offset ArraydmovEcx,lengthof ArraydmovEbx,type ArraydPagerDumpmem;Displays the contents of an array in 16 binary mode PagerCrlf;line Break ;Prompt user to enter a decimal integer movEdx,offset prompt1PagerWriteString;displaying strings in the console PagerReadInt;Enter an integer movDwordval,eax;displays integers in decimal, hexadecimal, and binary numbers PagerCrlfPagerWriteintPagerCrlfPagerWritehexPagerCrlfPagerWritebinPagerCrlfPagerwaitmsg;To set the console window as the default color moveax,lightgray+ (black* -) PagerSetTextColorPagerCLRSCR exit main ENDP END main
Random32: Generates a 32-bit random integer and returns it in EAX.
Eg2
Randomly generates 10 unsigned integers within range 0~4294967294, then randomly regenerates to 10 signed integers between range -50~+49
Include Irvine32.IncTAB =9 ;the ASCII code of the TAB key. Code main PROCPagerRandomize;initialize the random number generator PagerRand1PagerRand2 exit main ENDPRand1 PROCmovEcxTenL1: PagerRandom32;Generate random numbers PagerWritedec;display in unsigned decimal number format movAl,tab;Horizontal Tab PagerWritechar;Show Horizontal TabsLoop L1PagerCrlfretRand1 ENDPRand2 PROCmovEcxTenL2: moveax - PagerRandomrangeSubeax - PagerWriteintmovAL, TAB;Horizontal Tab PagerWritechar;Output Horizontal TabLoop L2PagerCrlfretRand2 Endpend Main
EG3: Performance Metrics
The getmseconds process returns the number of milliseconds elapsed since the property, and in the 3rd test program, the Getmseconds process was called first before the loop, then the nested loop was executed about 17 billion times, and the getmseconds process was called again after the loop ended and the elapsed time was reported:
Include Irvine32.IncOuter_loop_count =3. Data startTime DWORD? MSG1 BYTE"Please wait ...", 0dh,0ah,0MSG2 BYTE"Elapsed milliseconds:",0. Code main PROCmovEdx,offset MSG1Pagerwritestring;Save start Time PagerGetmsecondsmovStarttime,eaxmovEcx,outer_loop_count;Execution Loop L1: PagerInnerloop Loop L1PagerGetmsecondsSubEax,starttimemovEdx,offset MSG2PagerwritestringPagerWritedecPagerCrlf exit main ENDP innerloop PROCPushecxmovecx, 0FFFFFFFFHL1: movEax,eax Loop L1Popecxretinnerloop ENDP END main
5.4 Stack operations
There are several important uses for the stack in the program:
1. When used for a variety of purposes, the stack can be conveniently used as a temporary storage area, and after the register is in use, the stack can restore its original value
When the 2.CALL instruction executes, the CPU uses the stack to hold the return address of the currently called procedure.
3. After invoking the procedure, the input value can be passed through the stack
4. Local variables within the procedure are created on the stack, and when the process is finished, these variables are discarded
5.4.2PUSH and Pop
Push R/M16
Push R/m32
Push Imm32
In protected mode, the number is always 32 bits, in the field address mode, if the. 386 or higher processor pseudo-instruction is not used, the immediate number is 16 bits by default.
Pop Instructions:
The pop instruction first copies the stack element referred to by ESP into a 16-bit or 32-bit intent operand. Then increase the value of the ESP. If the operand is 16 bits, esp plus 2, if the operand is 32 bits, esp plus 4
POP R/M16
Pop R/m32
The fifth chapter--Process