Instantly share code, notes, and snippets.

@shikhin /Selfer.asm
Last active Dec 15, 2015

What would you like to do?
Following is Selfer, which is an attempt at a stand-alone system to create, well, new systems!
; Selfer.
; Shikhin Sethi, the author of selfer, has dedicated the work to the public domain
; by waiving all of his or her rights to the work worldwide under copyright law,
; including all related and neighboring rights, to the extent allowed by law.
; You can copy, modify, distribute and perform the work, even for commercial purposes,
; all without asking permission.
; 16 bits, starting at 0x7C00.
ORG 0x7C00
%define SECTOR_SIZE 512
%define HEADS 2
%define TRACKS 80
%define READ_SECTOR 0x00
%define WRITE_SECTOR 0x01
%define LEFT_SCANCODE 75
%define UP_SCANCODE 72
%define DOWN_SCANCODE 80
%define FREE_SPACE 0x500
%define MEANING_OF_LIFE 0x42
%define NOP_OPCODE 0x90
%define F_OPCODE(no) 0x3A + no
%define LINE_LENGTH 40
; Output an entire sector.
; EBP -> contains the base address of the sector to output.
; ESI -> current index.
; Restore ES. We depend upon the fact for the rest of Selfer that ES is set to 0
; but we make it 0xB800 for the display code, so, let's save it.
push es
; Set ES for screen output.
mov ax, 0xB800
mov es, ax
; Memset the entire display memory (till the point we display) to 0x0F300B30,
; such that colors are: white & light cyan.
xor di, di
mov cx, 512
mov eax, 0x0F300B30
rep stosd
; Store BP in BX, and get exact index in AX.
mov bx, bp
add bp, si
xchg bp, ax
; At si * 4 (since every si represents two bytes, i.e. two characters,
; and there are two bytes for each character in display memory) get red color.
shl si, 2
mov dword [es:si], 0x0C300C30
; Display the address.
; We start outputting from 0xF06 into display memory, and move down.
mov si, 0xF06
; Two bytes (a word address).
mov cx, 2
; Get the ASCII values and store it.
call HexToASCII
mov [es:si], dl
mov [es:si - 2], dh
; Move 4 down (two bytes displayed).
sub si, 4
; Get the next byte in address.
shr ax, 8
loop .LoopAddress
; Start from index 0, and display the entire sector.
xor si, si
; Get the byte in DL, and call HexToASCII.
mov al, [bx]
call HexToASCII
; Store DH & DL in display memory.
mov [es:si], dh
mov [es:si + 2], dl
; Increment BX, to get to the next byte.
inc bx
; Add 4 to si (for reasons described above), and if we've done 2048,
; that is, one sector, then we're done.
add si, 4
; If below 2048, then loop.
cmp si, 2048
jb .LoopIndex
; Get back ES and return.
pop es
CPU 8086
; Main entry point where BIOS leaves us.
; DL -> Expects the drive number to be present in dl.
; CS:IP -> Expects CS:IP to point to the linear address 0x7C00.
jmp 0x0000:.FlushCS ; Some BIOS' may load us at 0x0000:0x7C00, while others at 0x07C0:0x0000. Let's just make this uniform.
; If the CPU isn't 386, display '$'.
mov al, '$'
; General error function.
; Print whatever is in AL via the BIOS function.
xor bx, bx
mov ah, 0x0E
int 0x10
; Halt!
jmp $
; Flush segments.
; Set up segments.
xor bx, bx
; Stack.
mov ss, bx
mov sp, Main
; DS and ES segments.
mov ds, bx
mov es, bx
; Check if CPU is 80386 or not.
; Invert the IOPL bits, since on 8086/80186 they are hardwired to 1.
; In real mode on the 286, they are always 0, though.
pop ax
xor ah, 0x30
; Get them back.
push ax
; Get flags to check if anything changed.
; Get new flags in CX.
pop cx
; Test if bits changed, or not.
xor ax, cx
test ah, ah
jnz .Error386
CPU 386
; Clear direction flag.
; Store the boot drive number.
mov [FREE_SPACE], dl
; Set to mode 0x03, or 80x25 text mode.
; AH should be zero as used above.
mov al, 0x03
int 0x10
; Hide the hardware cursor.
mov ch, 0x26
mov ah, 0x1
int 0x10
xor si, si
; Read everything from 0x7C00 to 0x10000.
; Start from the end, i.e. 0x10000 - 0x200, and
; LBA 0x41.
mov bp, 0x10000 - 0x200
xor di, di
mov bx, MEANING_OF_LIFE - 1
call RWSector
; Read till all not read.
dec bx
jnz .NextSector
; If there is a NOP, we'd need the address to jump to in BX.
mov bx, 0x7E00
; If the value at 0x7E00 is a NOP, then simply jump to it.
cmp byte [0x7E00], NOP_OPCODE
je EventLoop.CallCode
; Events.
; Display the current sector.
xor ah, ah
; Get input.
int 0x16
; BX points to the exact index.
mov bx, bp
add bx, si
; Left key (previous byte)
jne .Right
; Go to previous byte.
dec si
jmp .Next
; Right key (next byte)
jne .NextLine
; Go the next byte.
inc si
jmp .Next
; Go to the next line.
jne .PreviousLine
jmp .Next
; Go to the previous line.
jne .Retain
jmp .Next
; Retain.
cmp al, 'K'
jne .Move
; Save the current byte pointing too.
mov [FREE_SPACE + 1], bx
jmp .Next
; Sort-of like memmove.
cmp al, 'M'
jne .Paste
jmp .CommonPaste
; Paste.
cmp al, 'P'
jne .Run
; Get the address in DI.
mov di, [FREE_SPACE + 1]
; Get the byte into CL, and then into current index.
mov cl, [di]
mov [bx], cl
; If it was memmove, then we decrement pointer.
cmp al, 'M'
je .DecrementPointer
; Or increment pointer, for paste.
inc si
inc word [FREE_SPACE + 1]
jmp .Next
; Move one point below, and decrement the byte pointing to at.
dec si
dec word [FREE_SPACE + 1]
jmp .Next
; Run from the current cell.
cmp al, 'R'
jne .Save
; Save everything important.
push bp
push si
push ds
push es
call bx
pop es
pop ds
pop si
pop bp
jmp .Next
; Save the current sector.
cmp al, 'S'
jne .Write
; Write.
xor di, di
inc di
; Get the LBA into BX.
shr bx, 9
sub bx, 0x3E
call RWSector
jmp .Next
; Write's all the sectors.
cmp al, 'W'
jne .NextSector
push bp
; Write everything from 0x7C00 to 0x10000.
; Again, we do it in reverse order here.
mov bp, 0x10000 - 0x200
xor di, di
inc di
mov bx, MEANING_OF_LIFE - 1
call RWSector
; Loop till all sectors not done.
dec bx
jnz .LoopSector
pop bp
jmp .Next
; Next sector.
cmp al, 'X'
jne .PreviousSector
; Are we going beyond our region?
cmp bp, 0x10000 - SECTOR_SIZE
je .Next
; If not, move one sector above.
jmp .Next
; Previous sector.
cmp al, 'Z'
jne .Input
; Are we going beyond our region?
cmp bp, 0x7C00
je .Next
; If not, move one sector below.
; Mask of SI.
and si, 0x1FF
jmp EventLoop
; Get another keystroke.
shl eax, 16
int 0x16
; Arrange both into AX.
xchg ah, al
shr eax, 8
; If second key was ESC, then throw away input.
cmp al, 0x1B
je .Next
mov cx, 2
; Get '0' to '9'.
sub al, 48
; If larger, subtract 7 to get hex.
cmp al, 9
jbe .NextChar
sub al, 7
; Do next character, now.
xchg al, ah
loop .GetHexLoop
; Display whatever was outputted.
shl al, 4
shr ax, 4
mov [bx], al
; Go to next byte.
inc si
jmp .Next
; Read/write a sector.
; BX -> logical block address.
; EBP -> where to read/write to/from.
; EDI -> 0x00 for read; 0x01 for write.
; Get the LBA into AX.
xchg ax, bx
mov bx, bp
; Three tries.
mov si, 3
; Get CHS.
; CH -> cylinder number.
; CL -> sector number.
; DH -> head number.
xor dx, dx
div cx
; Get sector.
mov cl, dl
inc cl
; Get head number.
mov dh, al
and dh, 0x1
; Get track number.
shr ax, 1
mov ch, al
mov dl, [FREE_SPACE]
shl di, 8
; Prepare for interrupt.
mov ax, 0x0201
add ax, di
int 0x13
; If successful, return.
jnc .Return
; Else, try to reset.
xor ah, ah
int 0x13
dec si
jnz .Loop
; Get in the character.
mov al, '@'
jmp Main.Error
; Converts a byte to a ASCII hexadecimal value.
; AL -> the byte to convert.
; Returns:
; DX -> the output.
movzx dx, al
shl dx, 4
shr dl, 4
mov al, 2
cmp dl, 9
jg .Char
add dl, 48
jmp .Next
add dl, 55
xchg dh, dl
dec al
jnz .Loop
; Padding.
times 510 - ($ - $$) db 0
dw 0xAA55
; Pad to floppy disk.
times (1440 * 1024) - ($ - $$) db 0

This comment has been minimized.


shikhin commented Mar 23, 2013

Here's the first extension written entirely via Selfer, from DavidCooper:

90 B8 00 00 8E C0 8E D8 66 B8 56 42 45 32 66 A3 00 80 B8 00 4F BF 00 80 06 1E CD 10 1F 07 66 A1 00 80 66 3D 56 45 53 41 74 01 C3 FC BE 0E 80 AD 50 AD 8E D8 5E BF 00 82 B9 00 02 B8 00 00 8E C0 F3 A4 B8 00 00 8E D8 90 BE 00 82 BF 00 84 AD 3D FF FF 75 01 C3 91 B8 01 4F 56 57 06 1E CD 10 1F 07 5F 5E B8 00 02 03 F8 EB E4

His own explanation for it is: What it does is collect VESA tables to let you explore them before you try to write code to handle their content. Use the down-cursor key to scroll through them. At 8000h we have the table returned by function 4F00h int 10h. At 8200 we have a copy of the mode list, just in case the BIOS put it somewhere where Selfer can't view it. [The location where the mode list was actually put by the BIOS is found at the address and segment stated at byte 0E of the table loaded to 8000h, and on my one of my old 486s (neither work any more so I can't check) I think it was put somewhere in the screen memory area which Selfer can't reach.] From 8400 onwards we have the tables returned by function 4F01h int 10h, one for each mode in the list.

If you want, you can append "0x" to each byte, add some commas and a db, copy-paste into Selfer.asm after the first sector (i.e., after "dw 0xAA55"), assemble it and boot it, and it should just work too.

It's just more cool to type it directly into Selfer, though.

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