mirror of
				https://github.com/cfenollosa/os-tutorial.git
				synced 2025-06-13 12:54:24 +00:00 
			
		
		
		
	lesson 6, segmentation
This commit is contained in:
		
							parent
							
								
									9bc3cd33e6
								
							
						
					
					
						commit
						ee0eefad66
					
				@ -5,7 +5,7 @@ function calling, strings*
 | 
			
		||||
 | 
			
		||||
We are close to our definitive boot sector.
 | 
			
		||||
 | 
			
		||||
In lesson 6 we will start reading from the disk, which is the last step before
 | 
			
		||||
In lesson 7 we will start reading from the disk, which is the last step before
 | 
			
		||||
loading a kernel. But first, we will write some code with control structures,
 | 
			
		||||
function calling, and full strings usage. We really need to be comfortable with
 | 
			
		||||
those concepts before jumping to the disk and the kernel.
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										25
									
								
								06-bootsector-segmentation/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								06-bootsector-segmentation/README.md
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,25 @@
 | 
			
		||||
*Concepts you may want to Google beforehand: segmentation*
 | 
			
		||||
 | 
			
		||||
**Goal: learn how to address memory with 16-bit real mode segmentation**
 | 
			
		||||
 | 
			
		||||
If you are comfortable with segmentation, skip this lesson.
 | 
			
		||||
 | 
			
		||||
We did segmentation
 | 
			
		||||
with `[org]` on lesson 3. Segmentation means that you can specify
 | 
			
		||||
an offset to all the data you refer to.
 | 
			
		||||
 | 
			
		||||
This is done by using special registers: `cs`, `ds`, `ss` and `es`, for
 | 
			
		||||
Code, Data, Stack and Extra (i.e. user-defined)
 | 
			
		||||
 | 
			
		||||
Beware: they are *implicitly* used by the CPU, so once you set some
 | 
			
		||||
value for, say, `ds`, then all your memory access will be offset by `ds`.
 | 
			
		||||
[Read more here](http://wiki.osdev.org/Segmentation)
 | 
			
		||||
 | 
			
		||||
Furthermore, to compute the real address we don't just join the two
 | 
			
		||||
addresses, but we *overlap* them: `segment << 4 + address`. For example,
 | 
			
		||||
if `ds` is `0x4d`, then `[0x20]` actually refers to `0x4d0 + 0x20 = 0x4f0`
 | 
			
		||||
 | 
			
		||||
Enough theory. Have a look at the code and play with it a bit.
 | 
			
		||||
 | 
			
		||||
Hint: We cannot `mov` literals to those registers, we have to
 | 
			
		||||
use a general purpose register before.
 | 
			
		||||
							
								
								
									
										27
									
								
								06-bootsector-segmentation/boot_sect_segmentation.asm
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								06-bootsector-segmentation/boot_sect_segmentation.asm
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
			
		||||
mov ah, 0x0e ; tty
 | 
			
		||||
 | 
			
		||||
mov al, [the_secret]
 | 
			
		||||
int 0x10 ; we already saw this doesn't work, right?
 | 
			
		||||
 | 
			
		||||
mov bx, 0x7c0 ; remember, the segment is automatically <<4 for you
 | 
			
		||||
mov ds, bx
 | 
			
		||||
; WARNING: from now on all memory references will be offset by 'ds' implicitly
 | 
			
		||||
mov al, [the_secret]
 | 
			
		||||
int 0x10
 | 
			
		||||
 | 
			
		||||
mov al, [es:the_secret]
 | 
			
		||||
int 0x10 ; doesn't look right... isn't 'es' currently 0x000?
 | 
			
		||||
 | 
			
		||||
mov bx, 0x7c0
 | 
			
		||||
mov es, bx
 | 
			
		||||
mov al, [es:the_secret]
 | 
			
		||||
int 0x10
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
jmp $
 | 
			
		||||
 | 
			
		||||
the_secret:
 | 
			
		||||
    db "X"
 | 
			
		||||
 | 
			
		||||
times 510 - ($-$$) db 0
 | 
			
		||||
dw 0xaa55
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user