UPI Study › Courses › Introduction to Operating Systems › Free lessons › Lesson 4.6 Free with a free account
Address Spaces
Introduction to Operating Systems · Chapter 4: Memory ManagementSection 4.6 · lesson 30 of 838 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Introduction to Operating Systems.
Lesson complete
Earn the 3 college credits for Introduction to Operating Systems
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Virtual Memory →← Why Memory Management MattersNext lesson: Virtual Memory →
A free account plays all of Chapter 1 and 3 more lessons of Introduction to Operating Systems. Create a free account →
In this lesson
How base and limit registers translate and protect program addresses.
- Distinguish logical addresses from physical addresses
- Translate a logical address with base and limit registers and spot a trap
- Compare address binding at compile time, load time and run time
- Calculate field and element offsets that stay the same from run to run
Free study notes
Get this lesson's notes by email
The key points and 4 practice questions with answers, in your inbox. One email, free.
One email. Unsubscribe in one click.
Transcript
Everything said in the video, section by section (1,075 words). Read along, or search it.
Opening Analogy
House Numbers Need a Street
Every house on a street has a number. But number 12 means nothing, until you know which street. A program is in the same position. It thinks its first byte of memory is at address 0. Yet in real RAM, it might sit at 30,000, or at 60,000. Something has to translate between the numbers a program uses and where its bytes really are. And that same translation keeps programs apart.
Definitions
Physical and Logical Addresses
RAM chips hold bytes in numbered places. A place's number is its physical address: the address the memory hardware actually uses. Programs don't use physical addresses. Each one uses logical addresses, also called virtual addresses. They start at 0 and run up to the program's size. The whole range of logical addresses a program may use is its address space. It's the program's private street, numbered from 0, however the town is laid out.
Why Split Them
The Memory Management Unit Translates
Why split them? Because now the OS can put a program wherever there's room. And it can run several programs that all believe they start at 0. The translation from logical to physical is done by hardware: the memory management unit, or MMU. The operating system sets it up.
Base and Limit
Check the Limit, Then Add the Base
The simplest scheme uses two hardware registers, set for the program that's running. The base register holds the physical address where the program's space begins. The limit register holds the size of that space: how many addresses are allowed. On every memory access, the hardware does two steps. First comes the protection check: is the logical address below the limit? If not, the hardware raises a trap, as in lesson 2.3. The OS takes over, and usually ends the program. If the check passes, step two is translation: the physical address is the base, plus the logical address.
Worked Example
A Shop Program at Base 30,000
Try it. Sam, who runs a small online bakery, has a shop program loaded at base 30,000, with a limit of 12,000. Logical 500 passes the check, and becomes physical 30,500. Logical 11,999 is the last legal address. It lands at physical 41,999. Logical 12,000 is already out of range, so the hardware traps. So does 15,000.
Protection
Each Program Walled Into Its Range
Ivy, who looks after the computers at a school, runs an editor on the same machine. Its base is 50,000, and its limit is 5,000. The editor's logical 4,999 becomes physical 54,999. But a stray logical 20,000 is not below 5,000. It traps before it can touch any memory outside the editor's own range. Each program is walled into its own range.
Relocation
Change Only the Base Register
Now load the shop program at 60,000 instead. Logical 500 becomes physical 60,500. Nothing in the program changed. Only the base register did. That's the big advantage. A common mistake is to treat the limit as the highest physical address. It's a size, and it's compared with the logical address. With a limit of 12,000, the legal logical addresses are 0 to 11,999. So 12,000 is already outside.
Quick Check
Logical 6,500 With Base 42,000
Quick check. A program has base 42,000 and limit 8,000. It reads logical address 6,500. What happens? Does it read physical 35,500? Does the hardware trap? Or does it read physical 48,500? Pause, and decide. Physical 48,500. 6,500 is below the limit of 8,000, so the check passes. Then base plus logical: 42,000 plus 6,500 is 48,500. 35,500 subtracts instead of adding. And a trap would need a logical address of 8,000 or more.
Address Binding
Compile, Load or Run Time
When are addresses fixed? Source code uses names, like total, not numbers. Address binding is the step that ties each name to a real address. It can happen at compile time. If the compiler knows where the program will sit, it writes absolute physical addresses into the code. That's fast, but rigid: to move the program, you have to compile it again. It can happen at load time. The compiler writes relocatable code, counted from the program's start, and the loader adds the starting address. Once running, the program can't move. Or it can happen at run time. The code keeps logical addresses all the way, and the MMU translates every access, as base and limit do. The OS can even move a program while it runs. Modern systems work this way.
Offsets
Distances Stay Fixed Across Runs
Because the OS chooses where a program lands, its raw addresses change from one run to the next. Modern systems also shuffle them on purpose, a security feature called ASLR, in lesson 9.7. What stays the same are distances inside the program's own space. They're called offsets, and the compiler fixes them. Compiled and run on a Mac, a small C program reports that an int takes 4 bytes. So element 3 of an int array sits 12 bytes after element 0, wherever the array lands.
Code Example
Struct Fields and Padding
The same program has a struct for one order: a 1-byte tag, a 4-byte count, and an 8-byte price. It reports them at offsets 0, 4 and 8, and the whole struct takes 16 bytes. The compiler leaves a small gap after the tag, so the fields line up. So when you debug, print the differences between addresses, not the addresses. Differences stay the same when the OS moves the program.
Quick Check
Finding a Price in an Array
Quick check. That program keeps an array of these 16-byte orders. Call the spot where the array starts A, since it changes every run. Counting from 0, where does the price of the order at index 2 sit? A plus 34, A plus 40, or A plus 10? Pause, and decide. A plus 40. Orders 0 and 1 come first, 16 bytes each, gap included, so order 2 starts 32 bytes in. Its price is 8 bytes further: 40. A plus 34 forgets the gap, as if each order took 13 bytes. A plus 10 forgets the orders that come before it.
Real Case
Why Addresses Change Each Run
Later, Sam asked Ivy why the same program showed a different address on every run. It lands in a different place each time, she said. Your program only knows distances from its own start. The OS and the MMU handle where that start really is. Like house numbers, said Sam. I don't need to know where the street is in town.
Key Takeaways
Translate, Protect and Bind
To recap. A logical address is the program's own number, counted from 0. A physical address is the real place in RAM. Base and limit translate it: check that the address is below the limit, then add the base. Anything else traps. Binding can happen at compile, load or run time, and modern systems bind at run time. And raw addresses move from run to run, while offsets stay put.
About Introduction to Operating Systems at UPI Study
Introduction to Operating Systems (Computer Science 354) is a self-paced online course of 83 video lessons in 10 chapters, from what an operating system does and process scheduling to concurrency, memory, devices, file systems, networks, the internet, security and virtual machines. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: Linux vs Operating Systems
Why take it here
Why students take Introduction to Operating Systems with UPI Study
Where the credit goes
Universities that work with NCCRS
Introduction to Operating Systems carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Address Spaces
What is the difference between a logical address and a physical address?
How do base and limit registers work?
What are the three types of address binding?
Is the limit register the highest physical address a program can use?
About the Introduction to Operating Systems course
How many college credits is Introduction to Operating Systems?
How much does Introduction to Operating Systems cost and what is included?
Which universities accept UPI Study credits?
How long does Introduction to Operating Systems take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Introduction to Operating Systems.
83 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
Updated
3 credits · NCCRSEnroll →