|
|
Flat vs. Segmented Address Spaces Continued from 16-bit vs. 32-Bit Integers A 32-bit application's ability to handle large numbers more efficiently isn't the only advantage it enjoys over 16-bit applications. Its capacity for dealing with large quantities of data is superior, too. A 16-bit application can deal with only 64K of data at a time, but a 32-bit application can work with up to 4GB. The reason for this has to do with the mechanics of memory addressing. All Intel processors use a segment:offset addressing scheme to read and write memory. A segment is like a window into memory. Even though the 8088 can address 1MB of memory, it can address only 64K at a time. A special register known as a segment register is loaded with a value that points to the base of the segment, which can lie at any location whose address is a multiple of 16 bytes anywhere in the 1MB address space. A 16-bit offset holding an address relative to the base of the segment then identifies a specific location in memory, as shown in Figure 1. The 64K limit on segment size comes from the fact that a 16-bit offset can hold only 64K different values, so offsets can't be greater than 64K-1. FIGURE 1: One of the advantages that 32-bit applications enjoy over their 16-bit counterparts is seamless access to large data objects. 16-bit applications address memory in segments of no more than 64K at a time, which means more work must be done to read and write large quantities of data. 32-bit applications, on the other hand, use a "flat" address space in which any location from 0 to 4GB can be accessed in a single step. Segmented addressing is also used in 32-bit Intel processors, but since a 32-bit number can represent values from 0 to 4,294,967,295, the maximum segment size is 4GB. Therefore, a segment register can be pointed to the bottom of memory, and any byte within the processor's 4GB address space can be accessed using a 32-bit offset. Programmers call this a flat address space. Windows 95 uses a flat address space; so do OS/2 and Windows NT. Now the big question: Why does a flat address space matter? When an application is dealing with arrays, data structures, and other objects smaller than 64K in length, it doesn't matter. But when the data size exceeds 64K, a flat address space pays great dividends, because an application doesn't have to load and reload segment registers continually to access all the data. Here's a simple example illustrating the difference. A 32-bit application utilizing a flat address space can copy 256K of data from one location in memory to another with just five instructions: cld mov esi,source mov edi,destination mov ecx,10000h rep movsd A 16-bit application, even if its code is highly optimized, requires about three times as many instructions to do the same job: cld mov cx,4 mov si,0 mov di,0 start: push cx mov cx,8000h rep movsw mov ax,ds add ax,1000h mov ds,ax mov ax,es add ax,1000h mov es,ax pop cx loop start The 16-bit version of this routine actually executes 48 instructions, because everything after the line labeled start: is executed four times. (This example assumes that the processor is running in real mode. If the processor were running in protected mode instead, the segment registers would not be incremented directly but would be loaded with specially formatted selector values from a descriptor table.) The 32-bit version won't run 10 times faster than the 16-bit version, because memory access time is the limiting factor in this example. But it will run twice as fast--in large part because the 32-bit version copies the data 4 bytes at a time while the 16-bit version copies only 2 at a time, but also because the 32-bit code requires no segment register manipulations. Published as Tutor in the 11/07/95 issue of PC Magazine. |
|
TOP |
Copyright (c) 1997 Ziff-Davis Inc. |