mirror of
				https://github.com/cfenollosa/os-tutorial.git
				synced 2025-06-13 12:54:24 +00:00 
			
		
		
		
	lesson 3, boot sector with memory addressing
This commit is contained in:
		
							parent
							
								
									81e9f2c974
								
							
						
					
					
						commit
						bdca5e65da
					
				
							
								
								
									
										
											BIN
										
									
								
								03-bootsector-memory/.boot_sect_memory.asm.swp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								03-bootsector-memory/.boot_sect_memory.asm.swp
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							@ -1,3 +1,38 @@
 | 
			
		||||
TBD
 | 
			
		||||
*Concepts you may want to Google beforehand: memory offsets, pointers*
 | 
			
		||||
 | 
			
		||||
Our new boot sector will refer to memory addresses and labels
 | 
			
		||||
The only goal of this lesson is to learn where the boot sector is stored
 | 
			
		||||
 | 
			
		||||
Please open page 14 [of this document](
 | 
			
		||||
http://www.cs.bham.ac.uk/~exr/lectures/opsys/10_11/lectures/os-dev.pdf)<sup>*</sup>
 | 
			
		||||
and look at the figure with the memory layout.
 | 
			
		||||
 | 
			
		||||
I could just go ahead and tell you that it starts at `0x7C00`, but it's
 | 
			
		||||
better with an example.
 | 
			
		||||
 | 
			
		||||
We want to print an X on screen. We will try 4 different strategies
 | 
			
		||||
and see which ones work and why.
 | 
			
		||||
 | 
			
		||||
First, we will define the X as data, with a label:
 | 
			
		||||
```nasm
 | 
			
		||||
the_secret:
 | 
			
		||||
    db "X"
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Then we will try to access `the_secret` in many different ways:
 | 
			
		||||
 | 
			
		||||
1. `mov al, the_secret`
 | 
			
		||||
2. `mov al, [the_secret]`
 | 
			
		||||
3. `mov al, the_secret + 0x7C00`
 | 
			
		||||
4. `mov al, 2d + 0x7C00`, where `2d` is the actual position of the X in the binary
 | 
			
		||||
 | 
			
		||||
Take a look at the code and read the comments.
 | 
			
		||||
 | 
			
		||||
Compile and run the code. You should see a string similar to `1[2¢3X4X`, where
 | 
			
		||||
the bytes following 1 and 2 are just random garbage.
 | 
			
		||||
 | 
			
		||||
If you add or remove instructions, remember to compute the new offset of the X
 | 
			
		||||
by counting the bytes, and replace `0x2d` with the new one.
 | 
			
		||||
 | 
			
		||||
~~~~~
 | 
			
		||||
This whole tutorial is heavily inspired on that document. Please read the
 | 
			
		||||
root-level README for more information on that.
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										47
									
								
								03-bootsector-memory/boot_sect_memory.asm
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								03-bootsector-memory/boot_sect_memory.asm
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,47 @@
 | 
			
		||||
mov ah, 0x0e
 | 
			
		||||
 | 
			
		||||
; attempt 1
 | 
			
		||||
; Fails because it tries to print the memory address (i.e. pointer)
 | 
			
		||||
; not its actual contents
 | 
			
		||||
mov al, "1"
 | 
			
		||||
int 0x10
 | 
			
		||||
mov al, the_secret
 | 
			
		||||
int 0x10
 | 
			
		||||
 | 
			
		||||
; attempt 2
 | 
			
		||||
; It tries to print the memory address of 'the_secret' which is the correct approach.
 | 
			
		||||
; However, BIOS starts loading at address 0x7c00
 | 
			
		||||
; so we need to add that padding beforehand. We'll do that in attempt 3
 | 
			
		||||
mov al, "2"
 | 
			
		||||
int 0x10
 | 
			
		||||
mov al, [the_secret]
 | 
			
		||||
int 0x10
 | 
			
		||||
 | 
			
		||||
; attempt 3
 | 
			
		||||
; Add the BIOS starting offset 0x7c00 to the memory address of the X
 | 
			
		||||
; and then dereference the contents of that pointer
 | 
			
		||||
mov al, "3"
 | 
			
		||||
int 0x10
 | 
			
		||||
mov bx, the_secret
 | 
			
		||||
add bx, 0x7c00
 | 
			
		||||
mov al, [bx]
 | 
			
		||||
int 0x10
 | 
			
		||||
 | 
			
		||||
; attempt 4
 | 
			
		||||
; We try a shortcut since we know that the X is stored at byte 0x2d in our binary
 | 
			
		||||
mov al, "4"
 | 
			
		||||
int 0x10
 | 
			
		||||
mov al, [0x7c2d]
 | 
			
		||||
int 0x10
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
jmp $
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
the_secret:
 | 
			
		||||
    ; ASCII code 0x58 is stored just before the zero-padding
 | 
			
		||||
    ; on this code that is at byte 0x2d (check it out using xdd)
 | 
			
		||||
    db "X"
 | 
			
		||||
 | 
			
		||||
times 510-($-$$) db 0
 | 
			
		||||
dw 0xaa55
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user