Memory management and file input/output for the Assembly tutorial

Source: Internet
Author: User
Tags win32

In this lesson, we will learn about the basics of memory management and file input/output operations. In addition, we will use the general dialog of the class as our display "device."

Theory:

From the user's point of view, WIN32 's memory management is very simple and straightforward. Each application has its own independent 4G address space, this memory pattern is called the "flat" type of address mode, all of the segment registers or descriptors point to the same starting address, all address offsets are 32-bit length, so that an application without the transformation selector can access their own up to 4G of address space. This memory management pattern is very concise and manageable, and we don't have to deal with annoying "near" and "far" pointers.

There are two main types of APIs under W16: Global and Local. The "global" APIs are allocated in other segments so that they are "far" (far) functions or called far process calls from the memory point of view, and the "local" APIs, as long as they deal with the heap of processes, call them "near" (near) functions or near-process calls. In WIN32, the two memory patterns are the same, regardless of whether you call GlobalAlloc or LocalAlloc, the result is the same.

The process of allocating and using memory is the same:

The GlobalAlloc function is called to allocate a chunk of memory that returns the allocated memory handle.

Call the GlobalLock function to lock a block of memory that takes a memory handle as a parameter and then returns a pointer to the locked block of memory.

You can use this pointer to read and write memory.

Call the GlobalUnlock function to unlock previously locked memory, which invalidates the pointer to the memory block.

Call the GlobalFree function to free the memory block. You must pass the function a memory handle.

In WIN32 you can also use "local" to replace the memory allocation API function with the word "global" in the function "global", that is, with LocalAlloc, LocalLock, and so on.

Using the GMEM_FIXED flag bit when calling function GlobalAlloc can further simplify the operation. With this flag, GLOBAL/LOCALALLOC returns a pointer to the allocated memory instead of a handle, so that you do not have to call Global/locallock to lock the memory, as long as you can simply call Global/localfree when you release the memory. But in this lesson we only use the traditional approach, because there are a lot of source code written in this way elsewhere.

WIN32 's file input/output APIs are almost the same as those seen in DOS (translator note: Perhaps no matter how different the internal implementations may be, it is conceivable that all file systems should have the same functionality as the interface to the application writer, except that the DOS interrupt mode is used to process the file input. The output becomes a call to an API function. The following are the basic steps:

Call the CreateFile function to generate a file that can be applied in many ways, and can be used to open communication ports, pipes, drivers, or consoles in addition to disk files. If successful, a handle to the file or device is returned. You can then use the handle to complete the operation of the file or device.

Call SetFilePointer to move the file pointer to where you want to read and write ...

Then call ReadFile or WriteFile to complete the actual reading and writing. These functions handle the data transfer between the file and the memory itself, so that you do not have to do the miscellaneous chores of allocating memory.

Call CloseHandle to close the file. This function accepts a file handle that was previously opened.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.