mirror of
				https://github.com/cfenollosa/os-tutorial.git
				synced 2025-06-13 12:54:24 +00:00 
			
		
		
		
	lesson 4, manipulating the stack
This commit is contained in:
		
							parent
							
								
									0522ad727e
								
							
						
					
					
						commit
						dde61c13e7
					
				
							
								
								
									
										13
									
								
								04-bootsector-stack/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								04-bootsector-stack/README.md
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,13 @@
 | 
			
		||||
*Concepts you may want to Google beforehand:stack*
 | 
			
		||||
 | 
			
		||||
The usage of the stack is important, so we'll write yet another boot sector
 | 
			
		||||
with an example.
 | 
			
		||||
 | 
			
		||||
Remember that the `bp` register stores the base address (i.e. bottom) of the stack,
 | 
			
		||||
and `sp` stores the top, and that the stack grows downwards from `bp` (i.e. `sp` gets
 | 
			
		||||
decremented)
 | 
			
		||||
 | 
			
		||||
This lesson is quite straightforward, so jump ahead to the code.
 | 
			
		||||
 | 
			
		||||
I suggest that you try accessing in-stack memory addresses by yourself, 
 | 
			
		||||
at different points in the code, and see what happens.
 | 
			
		||||
							
								
								
									
										42
									
								
								04-bootsector-stack/boot_sect_stack.asm
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								04-bootsector-stack/boot_sect_stack.asm
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,42 @@
 | 
			
		||||
mov ah, 0x0e
 | 
			
		||||
 | 
			
		||||
mov bp, 0x8000 ; this is an address far away from 0x7c00 so that we don't get overwritten
 | 
			
		||||
mov sp, bp
 | 
			
		||||
 | 
			
		||||
push 'A'
 | 
			
		||||
push 'B'
 | 
			
		||||
push 'C'
 | 
			
		||||
 | 
			
		||||
; to show how the stack grows downwards
 | 
			
		||||
mov al, [0x7ffe] ; 0x8000 - 2
 | 
			
		||||
int 0x10
 | 
			
		||||
 | 
			
		||||
; however, don't try to access [0x8000] now, because it won't work
 | 
			
		||||
; you can only access the stack top so, at this point, only 0x7ffe (look above)
 | 
			
		||||
mov al, [0x8000]
 | 
			
		||||
int 0x10
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
; recover our characters using the standard procedure: 'pop'
 | 
			
		||||
; We can only pop full words so we need an auxiliary register to manipulate
 | 
			
		||||
; the lower byte
 | 
			
		||||
pop bx
 | 
			
		||||
mov al, bl
 | 
			
		||||
int 0x10 ; prints C
 | 
			
		||||
 | 
			
		||||
pop bx
 | 
			
		||||
mov al, bl
 | 
			
		||||
int 0x10 ; prints B
 | 
			
		||||
 | 
			
		||||
pop bx
 | 
			
		||||
mov al, bl
 | 
			
		||||
int 0x10 ; prints A
 | 
			
		||||
 | 
			
		||||
; data that has been pop'd from the stack is garbage now
 | 
			
		||||
mov al, [0x8000]
 | 
			
		||||
int 0x10
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
jmp $
 | 
			
		||||
times 510-($-$$) db 0
 | 
			
		||||
dw 0xaa55
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user