码迷,mamicode.com
首页 > 编程语言 > 详细

汇编语言之加载器加载用户程序

时间:2021-02-04 12:14:41      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:cursor   ash   oop   ram   mamicode   img   screen   src   rect   

效果图:

技术图片

 

 

 

加载器代码:

	lbr_start equ 100

SECTION mbr align=16 vstart=0x7c00
	mov ax,0
	mov ss,ax 
	mov sp,ax 
	
	mov ax,[cs:phy_base]
	mov dx,[cs:phy_base+0x02]
	mov bx,16
	div bx 
	mov ds,ax 
	mov es,ax 
	
	xor di,di 
	mov si,lbr_start
	xor bx,bx 
	call read_hard_disk 
	
	mov ax,[0]
	mov dx,[2]
	mov bx,512
	div bx 
	cmp dx,0
	jnz next
	dec ax 
	
next:
	cmp ax,0
	jz direct
	push ds 
	mov cx,ax 
	
continue_read:
	mov ax,ds
	add ax,0x20
	mov ds,ax 
	inc si 
	xor bx,bx 
	call read_hard_disk
	loop continue_read
	pop ds 
	
direct:
    mov ax,[0x06]
    mov dx,[0x08]
	call my_segment
	mov [0x06],ax  
	
	mov cx,[0x0a] 
	mov bx,0x0c

remalloc:
	mov ax,[bx]
	mov dx,[bx+0x02]
	call my_segment
	mov [bx],ax 
	add bx,4
	loop remalloc
	
	jmp far [0x04]
	
read_hard_disk:
	push ax 
	push bx
	push cx 
	push dx 
	
	mov dx,0x1f2
	mov al,1
	out dx,al 
	
	inc dx 
	mov ax,si 
	out dx,al 
	
	inc dx 
	mov al,ah
	out dx,al 
	
	inc dx 
	mov ax,di 
	out dx,al 
	
	inc dx 
	mov al,0xe0
	out dx,al 
	
	inc dx 
	mov al,0x20
	out dx,al 
	
waits:
	in al,dx
	and al,0x88
	cmp al,0x08
	jnz waits
	
	mov cx,256
	mov dx,0x1f0
	
reads:
	in ax,dx 
	mov [bx],ax 
	add bx,2
	loop reads
	
	pop dx
	pop cx 
	pop bx 
	pop ax 
	
ret

my_segment:                   
    push bx  
    add ax,[cs:phy_base]
    adc dx,[cs:phy_base+0x02]
	mov bx,16
	div bx     
    pop bx 
ret





phy_base dd 0x10000

times 510-($-$$) db 0
db 0x55,0xaa 

 

用户程序代码:

SECTION header vstart=0
	program_length dd program_end
	code_entry     dw start
				   dd section.code.start

	segment_count  dw (segment_end-segment_start)/4 
	
	segment_start:
	code_segment   dd section.code.start
	data_segment   dd section.data.start
	stack_segment  dd section.stack.start
	segment_end:
	
SECTION code align=16 vstart=0
put_string:
		mov cl,[bx]
		or cl,cl 
		jz exit
		call put_char
		inc bx                         
        jmp put_string

exit:
         ret
put_char:
		push ax 
		push bx 
		push cx 
		push dx 
		push ds
		push es 
		
		mov dx,0x3d4
		mov al,0x0e 
		out dx,al 
		mov dx,0x3d5
		in al,dx 
		mov ah,al 
		
		mov dx,0x3d4
		mov al,0x0f
		out dx,al 
		mov dx,0x3d5
		in al,dx 
		mov bx,ax 
		
		cmp cl,0x0d 
		jnz jump_one
		mov ax,bx 
		mov bl,80
		div bl 
		mul bl 
		mov bx,ax 
		jmp set_cursor
		
jump_one:
		cmp cl,0x0a
		jnz jump_two 
		add bx,80
		jmp roll_screen
		
		
jump_two:
		mov  ax,0xb800
		mov  es,ax 
		shl  bx,1
		mov  [es:bx],cl 
		
		shr bx,1
		add bx,1
		
roll_screen:
		cmp bx,2000
		jl  set_cursor
		
		mov ax,0xb800
		mov ds,ax 
		mov es,ax 
		cld 
		
		mov si,0xa0
		mov di,0x00
		mov cx,1920
		rep movsw 
		
		mov bx,3840                    
        mov cx,80
		
cls:
        mov word[es:bx],0x0720
        add bx,2
        loop cls

        mov bx,1920
		
set_cursor:
		mov dx,0x3d4
		mov al,0x0e
		out dx,al 
		mov dx,0x3d5 
		mov al,bh 
		out dx,al 
		
		mov dx,0x3d4 
		mov al,0x0f
		out dx,al
		mov dx,0x3d5 
		mov al,bl 
		out dx,al 
		
		pop es 
		pop ds 
		pop dx 
		pop cx 
		pop bx 
		pop ax
		
ret 
start:
	mov ax,[stack_segment]
	mov ss,ax 
	mov sp,stack_end
	
	mov ax,[data_segment]
	mov ds,ax 
	
	mov bx,message
	call put_string
	
	
	
	
jmp $

SECTION data align=16 vstart=0
message db ‘Hello World‘  
		db 0x0d,0x0a 
		db 0
data_end:

 
SECTION stack align=16 vstart=0
resb 256
stack_end:
	
	
	
SECTION trail align=16
	program_end:

  

 

汇编语言之加载器加载用户程序

标签:cursor   ash   oop   ram   mamicode   img   screen   src   rect   

原文地址:https://www.cnblogs.com/SunShine-gzw/p/14370483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!