240 likes | 411 Vues
This guide explores essential concepts in x86 memory management, including multitasking, segmentation, and address translation. It defines key terms such as segment descriptors, segment selectors, and logical addresses, emphasizing their role in the memory management process. The document explains how logical addresses are converted to linear addresses and the importance of paging for efficient memory use. Additionally, it covers MS-DOS memory organization and real-address mode characteristics, illustrating how input and output redirection works in an assembly language context, based on Kip R. Irvine's "Assembly Language for x86 Processors."
E N D
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.
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.
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.
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.
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. Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
Indexing into a Descriptor Table Each segment descriptor indexes into the program's local descriptor table (LDT). Each table entry is mapped to a linear address: Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
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.
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.
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.
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.
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.
MS-DOS Memory Map Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
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.
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.
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.
Interrupt Vectoring Process Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
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.
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.
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.
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 process mov al,0 ; return code int 21h ; Same as: .EXIT 0 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
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.
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.
Write a backspace to standard output: mov ah,06h mov dl,08h int 21h INT 21h Functions 02h and 06h: Write Character to Standard Output Write the letter 'A' to standard output: mov ah,02h mov dl,’A’ int 21h or: mov ah,2 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.