Fill the screen with pixels x86 EGA Assembly

Having too much time ?

  1. Paste the code bellow in boot.asm;
  2. compile with nasm boot.asm -f bin -o boot.bin
  3. Prepare a fake floppy disk, dd if=/dev/zero of=floppy.img bs=512 count=2880
  4. Copy as boot.bin as boot sector on floppy: dd if=boot.bin of=floppy.img bs=512 count=1
  5. Boot a x86 virtual machine with floppy.img in A: (the first floppy drive)
  6. Profit

[bits 16]
[org 0x7c00]

jmp short begin
times 0x3B db 0

; Setup our Data Stack to 0
mov ax, 0x0000
mov ds, ax

; Setup our stack at 0x8000
mov bp, 0x8000
mov sp, bp

; Switch to graphics mode
mov ah, 0x00
mov al, 0x10
int 0x10

; Put a few pixels
mov ah, 0x0c ; Operation Write Pixel
mov al, 0xff ; Color
mov cx, 0x00 ; X
mov dx, 0x00 ; Y
mov bh, 0x00 ; Page

int 0x10 ; Call the BIOS !
cmp cx, 639 ; Compare cx to 639 (max X pixels)
je .hit_end_of_x ; If they're equal jump to .hit_end_of_x
add cx, 0x01 ; Add 1 to cx
jmp .put_pixel ; Jump back to .put_pixel

mov cx, 0x00 ; CX = 0
add dx, 0x01 ; Next line, DX = DX + 1
cmp dx, 349 ; Compare dx to 349 (max Y lines)
je .hit_end_of_y ; If they're equal jump to hit_end_of_y
jmp .put_pixel ; Jump back to .put_pixel

mov dx, 0x00 ; DX = 0
add al, 0x01 ; Next color (add 1 to al) [al = lower 8 bits of ah]
jmp .put_pixel ; Jump back to .put_pixel

; We should not reach here
hlt ; HALT (lower power)
jmp $ ; Jump here (infinite loop)

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

One thought on “Fill the screen with pixels x86 EGA Assembly

  1. Christophe says:

    A cool example! I ran it in VirtualBox and it produced the expected result. Thanks for sharing it with us.

    Please note, the first dd command is superfluous, as the second dd resets the file size to 512. You can use conv=notrunc to avoid this.

Leave a comment