1
\$\begingroup\$

In short, this bootloader code loads sectors from the disk into memory, sets the video mode and prints a message on the screen. After that, it goes into an infinite loop, keeping the system running.

[ORG 0x7C00]
    mov ah, 0x02
    mov al, 0x01
    mov ch, 0x00
    mov cl, 0x02
    mov dh, 0x01
    mov es, [EXTENSION]
    mov bx, 0x00
    int 13h
    jmp PRINT
         
MSG: db  'Welcome to my OS',0xa,13,10,0x00

times 510-($-$$) db 0
db 0x55
db 0xAA

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
EXTENSION:
mov ah, 0x00; Contains the type of function
        mov al, 0x13
        int 0x10
;     PARAMETERS
    mov al, 0x01 ; The pixel color
        mov bh, 0x00 ; The page number 
        mov cx, 0x00 ; X Position
        mov dx, 0x00 ; Y Position

        
LOOP:
    mov ah, 0x0C
        int 0x10 ; Video function
        je RESETCOLLOR
        inc cx ; Advance to the right pixel
        cmp cx, 0x0140 ; Checks if it is 320
        jne LOOP ;  Continued if not 320
        mov cx, 0x00 ; Returns to position 0 of the X axis
        inc dx ;  Advance to the next line
        cmp dx, 0xC8 ; Checks if it reached the last line 
        jne LOOP 
        mov dx, 0x00  ; Back to the first line
        inc al ; Jump to the next color 
        cmp al, 0xFF ; Check if it is the last color
        je RESETCOLLOR
        jmp LOOP
RESETCOLLOR:
    mov al, 0x00 ; Collor reset
    jmp LOOP
PRINT:
    xor ax, ax    ; Reset acumulator
        mov ds, ax
        mov si, MSG
CHAR_PRINT: lodsb
    or al, al ; Checks if the value is 0
        jz WAITNG  ; Loop until you turn off the computer
        mov ah, 0x0E 
        int 0x10
        jmp CHAR_PRINT ; Jump to the next character
WAITNG:
    jmp WAITNG

made for educational purposes

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

In short, this bootloader code loads sectors from the disk into memory, sets the video mode and prints a message on the screen. After that, it goes into an infinite loop, keeping the system running.

I would say even shorter: this bootloader code loads one (random) sector from the disk into memory, and then crashes the computer, keeping the system running.

There are a lot of errors that need taking care of here:

  • The instruction mov dh, 0x01 sets the Head value to 1. The bootsector itself is always stored in the very first sector on the drive (CHS=001). Therefore your 'extension' that immediately follows would logically reside at CHS=002, so Head=0.

  • The instruction mov es, [EXTENSION] loads the word (2 bytes) at the address EXTENSION. Provided that the DS segment register was setup correctly to DS=0, this would lead to loading ES with the value 00B4h (which is the encoding of mov ah, 0).

  • Together with the setup for ES, the instruction mov bx, 0x00 results in a far pointer to 00B4h:0000h. That's a destination address that your jmp PRINT instruction can't reach!

It's nice that you're making this for educational purposes, and so I hope you'll study next improved version:

  [ORG 7C00h]
  cld             ; For correct operation of LODSB
  xor  ax, ax     ; To match ORG 7C00h, we set DS=ES=0
  mov  ds, ax
  mov  es, ax

  mov  dh, 0      ; BIOS gave us DL
  mov  cx, 0002h
  mov  bx, EXTENSION
  mov  ax, 0201h  ; BIOS.LoadSector
  int  13h
  jc   Stop       ; Unexpected error occurred
  mov  si, MSG
  jmp  PRINT

Stop:
  jmp  Stop
         
MSG: db  'Welcome to my OS',13,10,0

times 510-($-$$) db 0
dw 0AA55h

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
EXTENSION:

  ...

PRINT:
  lodsb
  test al, al
  jz   WAITNG    ; Loop until you turn off the computer
  mov  ah, 0Eh   ; BIOS.Teletype
  int  10h
  jmp  PRINT
WAITNG:
  jmp  WAITNG

The part of changing the color of the screen in a loop seems a somewhat unrelated task. This is a revised code (the first je RESETCOLLOR was out-of-place! and zeroing a register is better done using xor reg, reg):

  mov  ax, 0013h ; Set 320x200 256-color screen
  int  10h

  mov  ah, 0Ch   ; The function number
  mov  bh, 0     ; The page number
  xor  cx, cx    ; X Position
  xor  dx, dx    ; Y Position
LOOP_:
  mov  al, 1     ; Pixel color
LOOP:
  int  10h       ; Plot pixel using colors [1,254]
  inc  cx
  cmp  cx, 320
  jne  LOOP
  xor  cx, cx
  inc  dx
  cmp  dx, 200 
  jne  LOOP
  xor  dx, dx
  inc  al        ; Next color
  cmp  al, 255   ; Check if it is the last color
  jne  LOOP
  jmp  LOOP_     ; Go reset color
\$\endgroup\$
1
  • \$\begingroup\$ Thanks for the tip, I'll study better. \$\endgroup\$ Commented Jun 13 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.