Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 1
x86 Memory Management
• Reviewing Some Terms• New Terms• Translating Addresses• Converting Logical to Linear Address• Page Translation
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 2
Reviewing Some Terms
• Multitasking permits multiple programs (or tasks) to run at the same time. The processor divides up its time between all of the running programs.
• Segments are variable-sized areas of memory used by a program containing either code or data.
• Segmentation provides a way to isolate memory segments from each other. This permits multiple programs to run simultaneously without interfering with each other.
• A segment descriptor is a 64-bit value that identifies and describes a single memory segment: it contains information about the segment’s base address, access rights, size limit, type, and usage.
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 3
New Terms
• A segment selector is a 16-bit value stored in a segment register (CS, DS, SS, ES, FS, or GS).• provides an indirect reference to a memory segment
• A logical address is a combination of a segment selector and a 32-bit offset.
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 4
Translating Addresses
• The x86 processor uses a one- or two-step process to convert a variable's logical address into a unique memory location.
• The first step combines a segment value with a variable’s offset to create a linear address.
• The second optional step, called page translation, converts a linear address to a physical address.
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 5
Converting Logical to Linear Address
The segment selector points to a segment descriptor, which contains the base address of a memory segment. The 32-bit offset from the logical address is added to the segment’s base address, generating a 32-bit linear address.
Selector Offset
Logical address
Segment Descriptor
Descriptor table
+
GDTR/LDTR
(contains base address ofdescriptor table)
Linear address
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 6
Indexing into a Descriptor TableEach segment descriptor indexes into the program's local descriptor table (LDT). Each table entry is mapped to a linear address:
Logical addresses
0018 0000003A
(unused)
DRAM
SS ESP
001A0000
0002A000
0001A000
00003000
Local Descriptor Table
0010 000001B6
0008 00002CD3
LDTR register
DS
18
10
08
00
(index)
Linear address space
IP
offset
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 7
Paging (1 of 2)
• Paging makes it possible for a computer to run a combination of programs that would not otherwise fit into memory.
• Only part of a program must be kept in memory, while the remaining parts are kept on disk.
• The memory used by the program is divided into small units called pages.
• As the program runs, the processor selectively unloads inactive pages from memory and loads other pages that are immediately required.
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 8
Paging (2 of 2)
• OS maintains page directory and page tables• Page translation: CPU converts the linear address
into a physical address• Page fault: occurs when a needed page is not in
memory, and the CPU interrupts the program • OS copies the page into memory, program resumes
execution
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 9
MS-DOS and the IBM-PC
• Real-Address Mode• MS-DOS Memory Organization• MS-DOS Memory Map• Redirecting Input-Output• Software Interrupts• INT Instruction• Interrupt Vectoring Process• Common Interrupts
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 10
Real-Address Mode
• Real-address mode (16-bit mode) programs have the following characteristics:• Max 1 megabyte addressable RAM• Single tasking• No memory boundary protection• Offsets are 16 bits
• IBM PC-DOS: first Real-address OS for IBM-PC• Has roots in Gary Kildall's highly successful Digital
Research CP/M• Later renamed to MS-DOS, owned by Microsoft
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 11
MS-DOS Memory Organization
• Interrupt Vector Table• BIOS & DOS data• Software BIOS• MS-DOS kernel• Resident command processor• Transient programs• Video graphics & text• Reserved (device controllers)• ROM BIOS
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 12
MS-DOS Memory Map
ROM BIOS
Reserved
Video Text & Graphics
Video Graphics
Resident Command Processor
DOS Kernel, Device Drivers
Software BIOS
BIOS & DOS Data
Interrupt Vector Table
FFFFF
00400
A0000
B8000
C0000
F0000
00000
Address
640K RAM
Transient Program Area(available for application programs)
Transient Command Processor
VRAM
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 13
Redirecting Input-Output (1 of 2)
• Input-output devices and files are interchangeable• Three primary types of I/O:
• Standard input (console, keyboard)• Standard output (console, display)• Standard error (console, display)
• Symbols borrowed from Unix:• < symbol: get input from• > symbol: send output to• | symbol: pipe output from one process to another
• Predefined device names:• PRN, CON, LPT1, LPT2, NUL, COM1, COM2
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 14
Redirecting Input-Output (2 of 2)
• Standard input, standard output can both be redirected• Standard error cannot be redirected• Suppose we have created a program named
myprog.exe that reads from standard input and writes to standard output. Following are MS-DOS commands that demonstrate various types of redirection:
myprog < infile.txt
myprog > outfile.txt
myprog < infile.txt > outfile.txt
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 15
INT Instruction
• The INT instruction executes a software interrupt.• The code that handles the interrupt is called an
interrupt handler.• Syntax:
INT number
(number = 0..FFh)
The Interrupt Vector Table (IVT) holds a 32-bit segment-offset address for each possible interrupt handler.
Interrupt Service Routine (ISR) is another name for interrupt handler.
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 16
Interrupt Vectoring Process
mov...int 10hadd...
F000:F0653069 F000:AB62
F000:F065 F066 F067 F068 . .
sti cld push es . . IRET
1 2
3Calling program
(entry for INT 10)
Interrupt Vector Table
Interrupt Handler
4
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 17
Common Interrupts
• INT 10h Video Services• INT 16h Keyboard Services• INT 17h Printer Services• INT 1Ah Time of Day• INT 1Ch User Timer Interrupt• INT 21h MS-DOS Services
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 18
What's Next
• MS-DOS and the IBM-PC• MS-DOS Function Calls (INT 21h)• Standard MS-DOS File I/O Services
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 19
MS-DOS Function Calls (INT 21h)
• ASCII Control Characters• Selected Output Functions• Selected Input Functions• Example: String Encryption• Date/Time Functions
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 20
INT 4Ch: Terminate Process
• Ends the current process (program), returns an optional 8-bit return code to the calling process.
• A return code of 0 usually indicates successful completion.
mov ah,4Ch ; terminate processmov al,0 ; return codeint 21h
; Same as:
.EXIT 0
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 21
Selected Output Functions
• ASCII control characters• 02h, 06h - Write character to standard output• 05h - Write character to default printer• 09h - Write string to standard output• 40h - Write string to file or device
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 22
ASCII Control Characters
• 08h - Backspace (moves one column to the left)• 09h - Horizontal tab (skips forward n columns)• 0Ah - Line feed (moves to next output line)• 0Ch - Form feed (moves to next printer page)• 0Dh - Carriage return (moves to leftmost output
column)• 1Bh - Escape character
Many INT 21h functions act upon the following control characters:
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 23
INT 21h Functions 02h and 06h: Write Character to Standard Output
Write the letter 'A' to standard output:
mov ah,02hmov dl,’A’int 21h
Write a backspace to standard output:
mov ah,06hmov dl,08hint 21h
or: mov ah,2
Top Related