带文本和图形的Bootloader ... howto?(with nasm)

时间:2016-02-02 20:40:02

标签: nasm bootloader

下面我有一个代码,用3种不同颜色绘制3个圆圈..很好.. 但是如何添加文本字符串?谢谢。 我在virtualbox(windows)上做了一些测试,但没有运气.. 任何想法让它成功吗?



BITS 16
ORG 100h

push 0a000h           ;Video memory graphics segment
pop es

mov ax, 0013h         ;320x200@8bpp
int 10h


push 0Eh              ;Blue
push 10               ;cX
push 10               ;cY
push 10               ;Radius
call drawFilledCircle

push 02h              ;Blue
push 40               ;cX
push 40               ;cY
push 30               ;Radius
call drawFilledCircle

push 06h              ;Blue
push 140              ;cX
push 100              ;cY
push 70               ;Radius
call drawFilledCircle



;Wait for a key
xor ah, ah
int 16h
loc   db "KERNEL  IMG"

.LOOP:
          push    cx
          mov     cx, 0x000B                          ; eleven character name
          mov     si, loc
          ; image name to find
          push    di
     rep  cmpsb                                       ; test for entry match
          pop     di
          ;je      LOAD_FAT
          pop     cx
          add     di, 0x0020                          ; queue next directory entry
          loop    .LOOP
          ;jmp     FAILURE
;Restore text mode
mov ax, 0003h
int 10h

;Return
mov ax, 4c00h
int 21h


;Color
;cX
;cY
;R
drawFilledCircle:
 push bp
 mov bp, sp

 sub sp, 02h

 mov cx, WORD [bp+04h]   ;R

 mov ax, cx              
 mul ax                  ;AX = R^2
 mov WORD [bp-02h], ax   ;[bp-02h] = R^2



 mov ax, WORD [bp+06h]
 sub ax, cx              ;i = cY-R
 mov bx, WORD [bp+08h]
 sub bx, cx              ;j = cX-R

 shl cx, 1
 mov dx, cx              ;DX = Copy of 2R

.advance_v:
 push cx
 push bx

 mov cx,  dx

.advance_h:
  ;Save values
  push bx
  push ax
  push dx

  ;Compute (i-y) and (j-x)
  sub ax, WORD [bp+06h]
  sub bx, WORD [bp+08h]

  mul ax                  ;Compute (i-y)^2

  push ax
  mov ax, bx             
  mul ax
  pop bx                  ;Compute (j-x)^2 in ax, (i-y)^2 is in bx now

  add ax, bx              ;(j-x)^2 + (i-y)^2
  cmp ax, WORD [bp-02h]   ;;(j-x)^2 + (i-y)^2 <= R^2

  ;Restore values before jump
  pop dx
  pop ax
  pop bx

  ja .continue            ;Skip pixel if (j-x)^2 + (i-y)^2 > R^2

  ;Write pixel
  push WORD [bp+0ah]
  push bx
  push ax
  call writePx


.continue:

  ;Advance j
  inc bx
 loop .advance_h

 ;Advance i
 inc ax


 pop bx            ;Restore j
 pop cx            ;Restore counter

loop .advance_v

 add sp, 02h


 pop bp
 ret 08h



;Color
;X
;Y
writePx:
 push bp
 mov bp, sp

 push ax
 push bx

 mov bx, WORD [bp+04h]
 mov ax, bx
 shl bx, 6
 shl ax, 8
 add bx, ax       ;320 = 256 + 64

 add bx, WORD [bp+06h]
 mov ax, WORD [bp+08h]

 ;TODO: Clip

 mov BYTE [es:bx], al

 pop bx
 pop ax

 pop bp
 ret 06h





times 510-($-$$) db 0	; Fill the rest with zeros
dw 0xAA55		; Boot loader signature
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

  

但是如何添加文本字符串?

您决定使用320x200 256色视频模式13h 要输出文本,您可以使用处理文本输出的每个BIOS功能,就像在文本视频屏幕上一样。

此视频模式使用8x8字体,因此您可以将光标定位在任何40x25 = 1000个字符单元格中。

在屏幕中央写下红色大写 B 的示例:

mov dx, 0C14h  ;DH=12  row,               DL=20 column
mov bh, 0      ;BH=0   display page
mov ah, 02h    ;AH=02h set cursor position function
int 10h        ;video BIOS interrupt

mov bx, 000Ch  ;BH=0   display page,      BL=12 red
mov ax, 0E42h  ;AH=0Eh teletype function, AL=66 capital B
int 10h        ;video BIOS interrupt

答案 1 :(得分:-1)

BITS 16

[ORG 0x7C00]




;ORG 100h

 

push 0a000h           ;Video memory graphics segment
pop es

mov ax, 0013h         ;320x200@8bpp
int 10h


push 0Eh              ;Blue
push 10              ;cX
push 10               ;cY
push 10          ;Radius
call drawFilledCircle

push 02h              ;Blue
push 40              ;cX
push 40               ;cY
push 30           ;Radius
call drawFilledCircle

push 06h              ;Blue
push 140              ;cX
push 100               ;cY
push 70           ;Radius
call drawFilledCircle




;Wait for a key
;xor ah, ah
;int 16h

 


main:		; Label for the start of the main program

 mov ax,0x0000	; Setup the Data Segment register
		; Location of data is DS:Offset
 mov ds,ax	; This can not be loaded directly it has to be in two steps.
		; 'mov ds, 0x0000' will NOT work due to limitations on the CPU

 mov si, HelloWorld	; Load the string into position for the procedure.


 call PutStr	; Call/start the procedure






;jmp $		; Never ending loop

; Procedures
PutStr:		; Procedure label/start
 ; Set up the registers for the interrupt call
 mov ah,0x0E	; The function to display a chacter (teletype)
 mov bh,0x00	; Page number
 mov bl,0x07	; Normal text attribute

.nextchar	; Internal label (needed to loop round for the next character)
 lodsb		; I think of this as LOaD String Block 
		; (Not sure if thats the real meaning though)
		; Loads [SI] into AL and increases SI by one
 ; Check for end of string '0' 
 or al,al	; Sets the zero flag if al = 0 
		; (OR outputs 0's where there is a zero bit in the register)
 jz .return	; If the zero flag has been set go to the end of the procedure.
		; Zero flag gets set when an instruction returns 0 as the answer.
 int 0x10	; Run the BIOS video interrupt 
 jmp .nextchar	; Loop back round tothe top
.return		; Label at the end to jump to when complete
; ret		; Return to main program
;Wait for a key
xor ah, ah
int 16h

push 01h              ;Blue
push 100              ;cX
push 90               ;cY
push 140           ;Radius
call drawFilledCircle


;ret
; Data

HelloWorld db '   WELCOME !!   Press Enter ',10,10,0



 loc   db "KERNEL  IMG"

.LOOP:
          push    cx
          mov     cx, 0x000B                          ; eleven character name
          mov     si, loc
                       ; image name to find
          push    di
     rep  cmpsb                                       ; test for entry match
          pop     di
          ;je      LOAD_FAT
          pop     cx
          add     di, 0x0020                          ; queue next directory entry
          loop    .LOOP
          ;jmp     FAILURE





;Restore text mode
mov ax, 0003h
int 10h

;Return
mov ax, 4c00h
int 21h


;Color
;cX
;cY
;R
drawFilledCircle:
 push bp
 mov bp, sp

 sub sp, 02h

 mov cx, WORD [bp+04h]   ;R

 mov ax, cx              
 mul ax                  ;AX = R^2
 mov WORD [bp-02h], ax   ;[bp-02h] = R^2



 mov ax, WORD [bp+06h]
 sub ax, cx              ;i = cY-R
 mov bx, WORD [bp+08h]
 sub bx, cx              ;j = cX-R

 shl cx, 1
 mov dx, cx              ;DX = Copy of 2R

.advance_v:
 push cx
 push bx

 mov cx,  dx

.advance_h:
  ;Save values
  push bx
  push ax
  push dx

  ;Compute (i-y) and (j-x)
  sub ax, WORD [bp+06h]
  sub bx, WORD [bp+08h]

  mul ax                  ;Compute (i-y)^2

  push ax
  mov ax, bx             
  mul ax
  pop bx                  ;Compute (j-x)^2 in ax, (i-y)^2 is in bx now

  add ax, bx              ;(j-x)^2 + (i-y)^2
  cmp ax, WORD [bp-02h]   ;;(j-x)^2 + (i-y)^2 <= R^2

  ;Restore values before jump
  pop dx
  pop ax
  pop bx

  ja .continue            ;Skip pixel if (j-x)^2 + (i-y)^2 > R^2

  ;Write pixel
  push WORD [bp+0ah]
  push bx
  push ax
  call writePx


.continue:

  ;Advance j
  inc bx
 loop .advance_h

 ;Advance i
 inc ax


 pop bx            ;Restore j
 pop cx            ;Restore counter

loop .advance_v

 add sp, 02h


 pop bp
 ret 08h



;Color
;X
;Y
writePx:
 push bp
 mov bp, sp

 push ax
 push bx

 mov bx, WORD [bp+04h]
 mov ax, bx
 shl bx, 6
 shl ax, 8
 add bx, ax       ;320 = 256 + 64

 add bx, WORD [bp+06h]
 mov ax, WORD [bp+08h]

 ;TODO: Clip

 mov BYTE [es:bx], al

 pop bx
 pop ax

 pop bp
 ret 06h




times 510-($-$$) db 0	; Fill the rest with zeros
dw 0xAA55		; Boot loader signature