Skip to content

Instantly share code, notes, and snippets.

@nelsbrock
Last active August 6, 2022 20:02
Show Gist options
  • Save nelsbrock/b13aaa1872b31d0164697445f4c08eaa to your computer and use it in GitHub Desktop.
Save nelsbrock/b13aaa1872b31d0164697445f4c08eaa to your computer and use it in GitHub Desktop.
rainbowboot: x86 boot sector program that prints a rolling rainbow pattern to screen
;; rainbowboot.s
;; x86 boot sector program that prints a rolling rainbow pattern to screen
;; Copyright © 2021 Niklas Elsbrock
;;
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the “Software”), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
%define CHAR '#' ; printed ASCII character
%define DELAY 40000 ; time delay between lines in microseconds
%define CCOUNT 7 ; amount of colors
;; colors (see https://en.wikipedia.org/wiki/BIOS_color_attributes):
%define COLORS 0x04, 0x0C, 0x0E, 0x0A, 0x0B, 0x09, 0x05
%define DELAY_L (DELAY & 0xFFFF)
%define DELAY_H (DELAY >> 16)
bits 16
org 0x7C00
start:
cli ; disable hardware interrupts
xor bh, bh ; set page number to 0
mov al, 0x03 ; set video mode
xor ah, ah ; --- ...
int 0x10 ; --- interrupt
mov ah, 0x01 ; hide cursor
mov ch, 0x3F ; --- ...
int 0x10 ; --- interrupt
mov ah, 0x02 ; move cursor to bottom
mov dh, 24 ; --- row 24
xor dl, dl ; --- column 0
int 0x10 ; --- interrupt
xor si, si ; set color index to 0
.loop mov ah, 0x09 ; print 80 colored characters
mov cx, 80 ; --- set count to 80
mov al, CHAR ; --- set character
mov bl, [colors+si] ; --- set attribute (color)
int 0x10 ; --- interrupt
mov ah, 0x86 ; sleep
mov cx, DELAY_H ; --- set microseconds (cx:dx)
mov dx, DELAY_L ; --- ...
int 0x15 ; --- interrupt
mov ah, 0x0E ; print line feed
mov al, 0x0A ; --- set character to line feed
int 0x10 ; --- interrupt
inc si ; increment color index
cmp si, CCOUNT ; --- if color index is inside bounds
jl .loop ; --- -> repeat loop
xor si, si ; --- else, set color index to 0
jmp .loop ; repeat loop
colors: db COLORS
times 510 - ($ - $$) db 0
dw 0xAA55
@nelsbrock
Copy link
Author

You can test this using Yasm and QEMU:

yasm -o rainbowboot.bin rainbowboot.s
qemu-system-x86_64 -drive file=rainbowboot.bin,format=raw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment