News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

have at it. break and bash.

Started by ninjarider, August 30, 2006, 01:25:36 AM

Previous topic - Next topic

ninjarider


DOSSEG
.186
.MODEL SMALL
.STACK 200h
.CODE

org 07c00h

START:
jmp Begin
nop
BD_OEMNAME DB "SMITH  ", 0
BS_BYTESPERSEC DW 512
BPB_SECPERCLUS DB 1
BPB_RSVDSECCNT DW 1
BPB_NUMFATS DB 2
BPB_ROOTENTCHT DW 224
BPB_TOTSEC16 DW 2880
BPB_MEDIAID DB 0F0H
BPB_FATSZ16 DW 9
BPB_SECPERTRK DW 18
BPB_NUMHEADS DW 2
BPB_HIDDSEC DD 0
BPB_TOTSEC32 DD 0

Begin:
xor ax, ax
mov ds, ax
mov es, ax
mov di, offset CommandLine

mov si, offset Version
call PrintString
mov si, offset Prompt
call PrintString

OsLoop:
xor ax, ax
int 16h

cmp al, 8
je OsBackSpace

cmp al, 37
je OsMoveBackChar

cmp al, 39
je OsMoveForwardChar

cmp al, 13
je OsEnter

mov ah, 0eh
int 10h

mov byte ptr [di], 0
stosb
mov byte ptr [di], 0
jmp OsLoop

OsMoveBackChar:
mov ah, 0eh
mov al, 8
int 10h
dec di
jmp OsLoop

OsMoveForwardChar:
mov al, byte ptr [di]
test al, al
jz OsLoop
mov ah, 0eh
int 10h
inc di
jmp OsLoop


OsBackSpace:
mov ax, offset CommandLine
cmp di, ax
jbe OsLoop
push offset OsLoop
mov si, offset BackSpace
dec di
mov byte ptr [di], 0
jmp PrintString

OsEnter:
push offset OsLoop
mov si, offset CarraigeReturn
call PrintString
mov si, offset CommandLine
call PrintString
mov si, offset CarraigeReturn
call PrintString
mov si, offset CommandLine
lodsb
cmp al, "?"
je PrintHelp
cmp al, 32
je OsBeginMath
mov si, offset Invalid
call PrintString
OsReturnToEnter:
mov si, offset CarraigeReturn
call PrintString
mov si, offset Prompt
call PrintString
mov di, offset CommandLine
ret

OsBeginMath:
mov di, offset MathLine
push OsReturnToEnter
lodsb
cmp al, "b"
je MathB
cmp al, "o"
je MathO
cmp al, "h"
je MathH
cmp al, "B"
je MathB
cmp al, "O"
je MathO
cmp al, "H"
je MathH
dec si
mov dx, 10

MathLoop:
push ax
xor ax, ax
lodsb
mov cx, ax
pop ax
test cl, cl
jz ExitMath
cmp cl, 42
jb ExitMathInvalid
cmp cl, 48
jb MathSymbol
cmp cl, 48
jb ExitMathInvalid
cmp cl, 57
ja ExitMathInvalid
sub cl, 48
mul dx
add ax, cx
jmp MathLoop

MathSymbol:
stosw
sub cl, 42
stosw
jmp MathLoop

MathB:
mov dx, 2
jmp MathLoop

MathO:
mov dx, 8
jmp MathLoop

MathH:
mov dx, 16
jmp MathLoop

ExitMathInvalid:
mov si, offset InvalidMath
jmp PrintString

ExitMath:
ret

PrintHelp:
mov si, offset HelpScreen
push offset OsReturnToEnter

PrintString:
mov ah, 0eh

StrLoop:
lodsb
test al, al
jz ExitStrLoop
int 10h
jmp StrLoop

ExitStrLoop:
ret

HelpScreen db 10, 13, "?-Help Screen", 10, 13, " B-Binary Math", 10, 13, " O-Octal Math", 10, 13, " H-Hexadecimal Math", 10, 13, 0

BackSpace db 8, 32, 8, 0
InvalidMath db "Invalid Operand", 10, 13, 0
Invalid db "That Is An Invalid Instruction", 10, 13, 0
Prompt db ">", 0
CarraigeReturn db 10, 13, 0
CommandLine db 0

Padding1 dd 0,0,0,0

Padding11 db 0,0,0,0,0,0,0,0,0,0
Padding12 db 0,0

MathLine dw 0
Version db "Welcome To Indigo V0.0.1X", 10, 13, "By Tommy", 10, 13, 0
BootSig dw 0aa55h
END START


not sure how long its been since anyone has had fun with new code. this is an os that resides on the boot sector that i have been writing.


anything that anyone can suggest that would either save space or speed it up. your comments are appreciated.

Tedd

You can gain extra bytes by recoding certain instructions..

6 bytes..
31C0    xor ax, ax
8ED8    mov ds, ax
8EC0    mov es, ax


4 bytes!
0E      push cs
0E      push cs
1F      pop ds
07      pop es

The second is actually more correct too - as you can't be certain that bios dumped you at 0000:7c00, it could just as easily be 07c0:000 :P



4 bytes..
B40E    mov ah, 0E
B008    mov al, 08


3 bytes..
B8080E  mov ax, 0E08
More compact :wink



There will be others, and more complex ones.
If you really want to gain space, just shorten the strings - they don't have to be unreadable, but you can make use of abbreviations.
(And I wouldn't call it an OS as such :bdg)
No snowflake in an avalanche feels responsible.

mnemonic

Quote from: Tedd on August 30, 2006, 10:53:36 AM
The second is actually more correct too - as you can't be certain that bios dumped you at 0000:7c00, it could just as easily be 07c0:000 :P
That is why you should execute a far JMP to 0000:7c00 as the first thing, then the assumption is correct and as a side effect it will work in both cases. :8)
Be kind. Everyone you meet is fighting a hard battle.--Plato
-------
How To Ask Questions The Smart Way

ninjarider

well its not an os yet but concidering the amount of work i have done on it.
that and i wanted to try and cram as much as possible into 512 and found how easy it was to fill it.

ninjarider

mnemonic: you said about making a jump to 0000:7c00. how would that be acomplished since that would put me at the begining of my code. wouldn't i want to jump to the address of begin.

mnemonic

ninjarider,

good observed. :U

Have a look in here for that special case: http://www.mega-tokyo.com/osfaq/BootSequence

That page is just great, even if I don't bother with rolling my own OS and stuff I enjoyed reading the articles there as they are well written and easy to understand and to follow.
Make sure you have it bookmarked: http://www.mega-tokyo.com/osfaq2/index.php

:8)
Be kind. Everyone you meet is fighting a hard battle.--Plato
-------
How To Ask Questions The Smart Way

MichaelW

With the boot sector starting with an org 07c00h, and with DS and ES initialized to zero, I can only see two conditions where it could matter whether the BIOS loaded your boot sector at 0000:7c00 or 07c0:0000, both of them unusual. The first would be if you needed to access more than about 32KB of the segment. The second would be if your code used an absolute offset address for a jump or call destination. As the boot sector above is coded, the jump and call destinations are encoded as displacements, so they will work correctly, regardless of the load address. Because DS and ES are set to match the assumed load segment, the encoded data addresses will work correctly, regardless of the load address.

eschew obfuscation