;*****************************************************************************
;显示boot区的例子。本程序是show pci 的副产品：）				     *
;                                             By Z&Z                         *
;调用bios中断读取，显示。							     *
;                                                  Mar 31st  2004            *
;*****************************************************************************

	 .286
	 .model  Tiny
         .data
	   shOffset  db  0ffh
	   shWX      db  0					;显示窗口左上x坐标
	   shWY      db  0					;显示窗口左上y坐标
	   shCPosX   db  0					;经过计算的绝对坐标
	   shCPosY   db  0					
	   shDB      db  100h dup(?) 
	   shX       db  0					;窗口内的相对坐标
	   shY       db  0
	   shChosenX db  0
	   shChosenY db  0
         .code
.startup

		   mov		ah,2h
                   mov		al,1h
		   mov		ch,0h
		   mov		cl,00000001b
		   mov		dl,10000000b
		   mov		dh,0h
		   mov		bx,offset(shDB)	
		   int		13h
;DISK - READ SECTOR(S) INTO MEMORY int 13h
;AH = 02h
;AL = number of sectors to read (must be nonzero)
;CH = low eight bits of cylinder number
;CL = sector number 1-63 (bits 0-5)
;high two bits of cylinder (bits 6-7, hard disk only)
;DH = head number
;DL = drive number (bit 7 set for hard disk)
;ES:BX -> data buffer

		   call         ClrScr				;清屏
                   call		HindCur			;隐藏光标
		   call         DrawOffset			;画左上角偏移
		   call         DrawFirstLine
		   call		DrawLeftCol
		   call		DrawContext
	           jmp          TheEnd
ClrScr        proc
                   pusha
		   mov		ah,6h 
                   mov		al,0
		   mov		bh,00011110b
		   mov          cx,0
		   mov		dh,18h
		   mov		dl,4fh
		   int          10h
		   popa
		   ret
ClrScr        endp
HindCur       proc						     ;隐藏光标	
                   push		ax
		   push         cx
		   mov          ah,1h
		   mov          cl,20h
		   mov          ch,20h
		   int          10h
		   pop		cx
		   pop		ax
		   ret
HindCur       endp
GetPos        proc						     ;计算取得实际位置	
                   push		ax
		   mov		ah,shX
		   mov		al,shY
		   add          ah,shWX
		   add		al,shWY
		   mov		shCPosX,ah
		   mov		shCPosY,al
		   pop		ax
		   ret
GetPos        endp
SetCur        proc						     ;设置光标位置 	
		   pusha
		   call         GetPos
		   mov		ah,02h
		   mov		bx,0h
		   mov		dl,shCPosX
		   mov		dh,shCPosY
		   int          10h
		   popa
		   ret
SetCur        endp
DrawOffset    proc
                   pusha
		   mov		shX,0
		   mov          shY,0
		   call		SetCur
		   mov          bl,00011110b
		   mov          al,shOffset
		   call         ShowAl

		   call		SetCur
                   mov		ah,09h
		   mov		bh,0h
		   mov		al,'h'
		   mov		bl,00011110b
		   mov		cx,1h
		   int		10h

		   add		shX,2
		   call		SetCur
                   mov		ah,09h
		   mov		bh,0h
		   mov		al,':'
		   mov		bl,00011011b
		   mov		cx,1h
		   int		10h

		   popa
		   ret
DrawOffset    endp
;Input AL,Bl,No Output
ShowAL proc
		   pusha
	           mov  dl,al      ;保存AL
	           mov  cl,04       
	           shr  al,cl      ;AL高4位移至低4位
	           mov  cx,02h     ;循环2次

Low4bit:           cmp  al,09
	           jbe  larger         ;低4位超过9
	           add  al,07
larger:	           add  al,30h

	           push  cx
		   mov  bh,0h
		   mov  cx,1h
	           mov  ah,09h
	           int  10h
	           pop  cx
	           
		   add  shX,1
		   call SetCur
		   mov  al,dl      ;恢复保存
                   and  al,0Fh
	           loop  Low4bit
	           popa
		   ret
ShowAL	endp 
DrawFirstLine proc
                   mov		dx,0h
		   add		shX,2
Next1:		   
		   add		shX,1
		   call		SetCur
                   mov		ah,09h
		   mov		bh,0h
		   mov		al,'x'
		   mov		bl,00011011b
		   mov		cx,1h
		   int		10h

		   mov		al,dl
		   cmp		al,0Ah
		   jns		Pass2
		   jmp		Pass3
Pass2:		   add		al,7h
Pass3:		   add		al,30h
		   add		shX,1
		   call		SetCur
                   mov		ah,09h
		   mov		bh,0h
		   mov		bl,00011011b
		   mov		cx,1h
		   int		10h

		   add		shX,2

                   cmp          dx,0fh
		   jz		Pass1
		   inc		dx
		   jmp		Next1
Pass1:		   
                   ret
DrawFirstLine endp
DrawLeftCol   proc
                   mov		dx,0

		   mov		shY,0
DLNext1:		
		   mov		shX,0
		   add		shY,1
		   call		SetCur
		   mov		al,dl
		   cmp		al,0Ah
		   jns		DLPass2
		   jmp		DlPass3
DLPass2:	   add		al,7h
DLPass3:	   add		al,30h
		   call		SetCur
                   mov		ah,09h
		   mov		bh,0h
		   mov		bl,00011011b
		   mov		cx,1h
		   int		10h

		   add		shX,1
		   call		SetCur
		   mov		ah,09h
		   mov		bh,0h
		   mov		al,'x'
		   mov		bl,00011011b
		   mov		cx,1h
		   int		10h

		   add		shX,1
		   call		SetCur
		   mov		ah,09h
		   mov		bh,0h
		   mov		al,'h'
		   mov		bl,00011011b
		   mov		cx,1h
		   int		10h

		   add		shX,2
		   call		SetCur
		   mov		ah,09h
		   mov		bh,0h
		   mov		al,':'
		   mov		bl,00011011b
		   mov		cx,1h
		   int		10h

		   cmp		dx,0fh
		   jz		DLPass1
		   inc		dl
		   jmp		DLNext1
DLPass1:	   ret
DrawLeftCol   endp
DrawContext   proc
                   jmp		DCNext1
		   x		db    0
		   y		db    0
DCNext1:            
		   mov		shX,5h
		   mov		shY,1h
		   call		SetCur
		   xor		di,di
DCNext2:
DCNext3:
		   mov		al,X
		   cmp		shChosenX,al
		   jnz		DCPass1
		   mov		al,Y
		   cmp		shChosenY,al
		   jnz		DCPass1

		   add		shX,1
		   call		SetCur
		   mov		ah,09h
		   mov		bh,0h
		   mov		al,'['
		   mov		bl,00111010b
		   mov		cx,1h
		   int		10h

		   add		shX,1
		   call		SetCur
		   mov		al,ShDB[di]
		   mov		bl,00111010b
		   call		ShowAL

		   mov		ah,09h
		   mov		bh,0h
		   mov		al,']'
		   mov		bl,00111010b
		   mov		cx,1h
		   int		10h
                   jmp		DCNext6
DCPass1:		
		   add		shX,1
		   call		SetCur
		   mov		ah,09h
		   mov		bh,0h
		   mov		al,' '
		   mov		bl,00111010b
		   mov		cx,1h
		   int		10h

		   add		shX,1
		   call		SetCur
		   mov		al,ShDB[di]
		   mov		bl,00111010b
		   call		ShowAL

		   mov		ah,09h
		   mov		bh,0h
		   mov		al,' '
		   mov		bl,00111010b
		   mov		cx,1h
		   int		10h
		   
DCNext6:           inc		di
		   cmp		X,0fh
		   jz		DCNext5
		   inc   	X
		   call		SetCur
		   jmp		DCNext2

DCNext5:	   
                   cmp		Y,0fh
		   jz		DCNext4
		   mov		X,0h
		   mov		shX,5h
		   inc		shY
		   inc		Y
		   call		SetCur
		   jmp		DCNext3
DCNext4:
		   ret
DrawContext   endp
TheEnd:
.exit
         end
