⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tut12.html

📁 WINDOWS程序员使用指南--汇编基础
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <meta name="Author" content="Iczelion">
   <meta name="GENERATOR" content="Mozilla/4.51 [en] (Win95; I) [Netscape]">
   <title>Iczelion's Win32 Assembly Tutorial 12: Memory Management and File I/O</title>
</head>
<body text="#FFFFFF" bgcolor="#000000" link="#FFFF00" vlink="#C0C0C0" alink="#C0FFC0">

<center>
<h1>
<font face="Arial,Helvetica"><font color="#999900"><font size=+2>Tutorial
12: Memory Management and File I/O</font></font></font></h1></center>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>We will
learn the rudimentary of memory management and file i/o operation in this
tutorial. In addition we'll use common dialog boxes as input-output devices.</font></font></font><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Download
the example <a href="files/tut12.zip">here</a>.</font></font></font>
<h3>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Theory:</font></font></font></h3>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Memory
management under Win32 from the application's point of view is quite simple
and straightforward. Each process owns a 4 GB memory address space. The
memory model used is called flat memory model. In this model, all segment
registers (or selectors) point to the same starting address and the offset
is 32-bit so an application can access memory at any point in its own address
space without the need to change the value of selectors. This simplifies
memory management a lot. There's no "near" or "far" pointer anymore.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Under
Win16, there are two main categories of memory API functions: Global and
Local. Global-type API calls deal with memory allocated in other segments
thus they're "far" memory functions. Local-type API calls deal with the
local heap of the process so they're "near" memory functions. Under Win32,
these two types are identical. Whether you call GlobalAlloc or LocalAlloc,
you get the same result.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Steps
in allocating and using memory are as follows:</font></font></font>
<ol>
<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Allocate
a block of memory by calling <b>GlobalAlloc</b>. This function returns
a handle to the requested memory block.</font></font></font></li>

<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>"Lock"
the memory block by calling <b>GlobalLock</b>. This function accepts a
handle to the memory block and returns a pointer to the memory block.</font></font></font></li>

<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>You can
use the pointer to read or write memory.</font></font></font></li>

<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>"Unlock"
the memory block by calling <b>GlobalUnlock</b> . This function invalidates
the pointer to the memory block.</font></font></font></li>

<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Free the
memory block by calling <b>GlobalFree</b>. This function accepts the handle
to the memory block.</font></font></font></li>
</ol>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>You can
also substitute "Global" by "Local" such as LocalAlloc, LocalLock,etc.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>The
above method can be further simplified by using a flag in GlobalAlloc call,
GMEM_FIXED. If you use this flag, the return value from Global/LocalAlloc
will be the pointer to the allocated memory block, not the memory block
handle. You don't have to call Global/LocalLock and you can pass the pointer
to Global/LocalFree without calling Global/LocalUnlock first. But in this
tutorial, I'll use the "traditional" approach since you may encounter it
when reading the source code of other programs.</font></font></font><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>File
I/O under Win32 bears remarkable semblance to that under DOS. The steps
needed are the same. You only have to change interrupts to API calls and
it's done. The required steps are the followings:</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>&nbsp;
<ol>
<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Open or
Create the file by calling <b>CreateFile</b> function. This function is
very versatile: in addition to files, it can open communication ports,
pipes, disk drives or console. On success, it returns a handle to file
or device. You can then use this handle to perform operations on the file
or device.</font></font></font></li>

<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Move
the file pointer to the desired location by calling <b>SetFilePointer</b>.</font></font></font>
<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Perform
read or write operation by calling <b>ReadFile</b> or <b>WriteFile</b>.
These functions transfer data from a block of memory to or from the file.
So you have to allocate a block of memory large enough to hold the data.</font></font></font></li>

<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Close
the file by calling <b>CloseHandle</b>. This function accepts the file
handle.</font></font></font></li>
</ol>

<h3>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Content:</font></font></font></h3>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>The program
listed below displays an open file dialog box. It lets the user select
a text file to open and shows the content of that file in an edit control
in its client area. The user can modify the text in the edit control as
he wishes, and can choose to save the content in a file.</font></font></font><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.386</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.model
flat,stdcall</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>option
casemap:none</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>WinMain
proto :DWORD,:DWORD,:DWORD,:DWORD</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\windows.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\user32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\kernel32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\comdlg32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>includelib
\masm32\lib\user32.lib</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>includelib
\masm32\lib\kernel32.lib</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>includelib
\masm32\lib\comdlg32.lib</font></font></font></b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.const</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>IDM_OPEN
equ 1</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>IDM_SAVE
equ 2</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>IDM_EXIT
equ 3</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>MAXSIZE
equ 260</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>MEMSIZE
equ 65535</font></font></font></b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>EditID
equ 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
; ID of the edit control</font></font></font></b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.data</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>ClassName
db "Win32ASMEditClass",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>AppName&nbsp;
db "Win32 ASM Edit",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>EditClass
db "edit",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>MenuName
db "FirstMenu",0</font></font></font></b>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -