Page 1 of 1
Writing OS in Assembly (BTW: I'm Crazy)
Posted: Mon Jan 31, 2011 6:20 pm
by phillid
Yes, that's right. I'm attempting to write an OS in assembly. I DO know there's such a thing as C, but I'm simply insane. Never mind why I'm using assembly and don't go rambling on about how much easier it is to write an OS in C. I know all of that stuff and have decided to learn assembly for the sake of it.
I am using NASM to compile my assembly code files into binaries which I put on a floppy disk. I have a bootloader (BOOT12) that I have put onto the disk. It will pick-up 'LOADER.BIN' (my OS binary) and run it etc, etc, you know the rest. As I am a noob, I have a few questions:
- How do I have variables in assembly? Do I have to write to memory addresses and use several of them as variables?
- How do I store-up input from the user (using int 0x16) in a variable (or address)?
- How do I perform conditional IF statements? All I have found is '%IF' which is only for the compiler to run.
Thanks for any useful replies!
phillid (the nut who codes in assembly)
Re: Writing OS in Assembly (BTW: I'm Crazy)
Posted: Mon Jan 31, 2011 6:33 pm
by Tosi
- How do I have variables in assembly? Do I have to write to memory addresses and use several of them as variables?
If you do not know how to store variables using assembly, I highly recommend not writing an OS to learn. Here is an example for NASM any way, if you can follow it maybe you can ignore my advice.
Code: Select all
SECTION .text
; Return the value in some_variable, callable from C code
GLOBAL some_function
some_function:
mov eax, [some_variable]
ret
; Increment the value in some_variable, NOT callable from C code
; since it wasn't declared as GLOBAL
some_other_function:
inc dword [some_variable]
ret
SECTION .data
some_variable dd 0
However, I would recommend getting more experience in assembly before jumping head-first into OS development.
Write some user mode programs in assembly first, figure out how to interface NASM with whatever compiler you use (which output format to use, whether to have dependancies, etc.). And you should at least know some high-level languages before learning assembly, it will make it easier.
As for the rest of your questions.
- How do I store-up input from the user (using int 0x16) in a variable (or address)?
int 0x16 only works in real mode where you can access the BIOS functions. Unless you want to write an OS that runs in an outdated operating mode, I would recommend using protected mode. There you can't use the BIOS, so you need to write your own interrupt handler and keyboard driver. Search the wiki, there's a relatively decent article on it.
- How do I perform conditional IF statements? All I have found is '%IF' which is only for the compiler to run.
There are no conditional IF statements in NASM like some other assemblers, unless you write your own macros. Instead, use the processor's own CMP and Jr operations:
Code: Select all
TestIfPointerIsNull:
mov eax, [a_pointer]
test eax, eax
jz .is_null ; The dot makes it a local label
; Is not null, do something
.is_null:
ret
Re: Writing OS in Assembly (BTW: I'm Crazy)
Posted: Mon Jan 31, 2011 9:24 pm
by Coty
You might be crazy, don't worry, I lost my sanity at the age of eight when I wired a simple circuit.
I would sugjust learning the basics of assembly before continuing, I started with these:
http://www.programmersheaven.com/downlo ... nload.aspx
9 of 10 asm tuts (The last one is missing) to get you started. Otherwise... well... you wont get anyware
BTW: Why is it that High Level coders think ASM is so hard anyway...
Re: Writing OS in Assembly (BTW: I'm Crazy)
Posted: Tue Feb 01, 2011 1:07 am
by Muneer
Hi ,
How do I have variables in assembly? Do I have to write to memory addresses and use several of them as variables?
How do I store-up input from the user (using int 0x16) in a variable (or address)?
When you store a value in a memory address, that is what is meant by a variable. you can specify your own addresses as variables like "Mov byte [0xDC00] , 3" or in nasm you can specify labels.
Code: Select all
Mov byte [SomeVariable] , 3 ; this will move 3 to the address specified by SomeVariable
SomeVariable:
db 0
How do I perform conditional IF statements? All I have found is '%IF' which is only for the compiler to run.
As Tosi pointed out, conditional IF statements are actually a logical combination of the various JMP & CMP statements . There is no such IF to processor. You arrive at it by some combination of statements like
Code: Select all
Mov Ax , 3
Cmp Ax , 3
Je Equal ; if ax = 3 then code will jump to Equal, otherwise it wont
JMP $
Equal:
Some statements.
Cheers.
Re: Writing OS in Assembly (BTW: I'm Crazy)
Posted: Tue Feb 01, 2011 4:37 am
by Combuster
BTW: Why is it that High Level coders think ASM is so hard anyway...
It's not hard, it just takes much, much more time to code the same things you can do quickly in a high-level language.
Re: Writing OS in Assembly (BTW: I'm Crazy)
Posted: Wed Feb 02, 2011 2:08 pm
by MDM
Not to mention, unless you are a pretty experienced assembly programmer (which no offense, you don't sound like one), your code isn't going to be nearly as fast as code produced by a good compiler.
Re: Writing OS in Assembly (BTW: I'm Crazy)
Posted: Thu Feb 03, 2011 8:08 am
by bewing
There are several of us who are coding our OSes in ASM (combuster, brendan, and me, to list a few).
BTW: Why is it that High Level coders think ASM is so hard anyway...
Basically, the deal is:
if you get in the zone in C, you can write a couple thousand lines of nice code in an evening, and it will contain one bug that you will have to find later.
If you get in the zone in ASM, you can write a couple hundred lines of nice ASM in an evening. It will be equivalent to a couple hundred lines of C code in what it accomplishes, it will be 10 times smaller than the equivalent C code, it will run 10 times faster than the equivalent C code, it will have TWO bugs in it that will be extremely hard to find later, and in 6 months you will forget how 10 percent of the code was supposed to work.
Re: Writing OS in Assembly (BTW: I'm Crazy)
Posted: Thu Feb 03, 2011 10:34 am
by quok
bewing wrote:and in 6 months you will forget how 10 percent of the code was supposed to work.
And that is exactly what commenting your code is all about. To help you 6 months later, or the guy that replaces you after you got hit by a bus, or to help a new team member... to help find those hard to find bugs, to make it easier for other forum members to help you when you post your broken code on the forums, etc.
As for that last part, you'll find that I personally don't respond to the "what's wrong with my code?" threads because in 99.999% of those posts, the included code dump contains NOT A SINGLE LINE OF COMMENTS. If you don't comment your code, chances are you don't understand it yourself.
Re: Writing OS in Assembly (BTW: I'm Crazy)
Posted: Sat Feb 05, 2011 3:50 pm
by phillid
Thanks guys
Re: Writing OS in Assembly (BTW: I'm Crazy)
Posted: Sat Feb 12, 2011 4:43 pm
by Sergio
When I started with my OS about 6 months ago I was pretty much uncapable of even writing a loop in x86 assembly language. I learned what it had to be learned quickly, viewing the bootstrap from FreeBSD. I started loving (I still do) doing everything "manually" in assembly code, but I realized I had become skilled enought and I didn't have to prove myself over and over again I could write just about anything in assembly language, obviously with twice the precaution and concentration. I figured I learned what I had to learn and decided to keep going with C, as it automates what I already understand. Currently assembly code is just 512-bytes for mbr, another 512-bytes for the first program at the beginning of the partition, and about 1000 more code lines while bootstrapping, most of it could easily be rewritten in C. When I wrote the terminal-handling facilities in assembly was when I realized that it was enough and I could just keep going writing everything in assembly, which I saw as lacking a real point, since it was turning more or less in a mechanical process. I just thought I had written enought (hopefully clever) assembly language code/hacks.
There are a couple of other OSes written in pure assembly that I am aware of that has some advanced features (support for multiprocessing, multithreading, lots of drivers, TCP/IP stack, lots of user-level applications, and even a GUI), MenuetOS being perhaps the best example.
http://menuetos.net/
KolibriOS is a fork from MenuetOS.
http://kolibrios.org/
These folks are pretty much as crazy as you!
HAVE FUN!
Re: Writing OS in Assembly (BTW: I'm Crazy)
Posted: Sun Mar 27, 2011 11:03 am
by lup0
hi phillid!
who told you that you are insane writing OS in assembly?! simply saying you are not!
assembly language is far more *intimately* connected to the processor than C could ever be atleast in the next couple of millenniums! C is bounded by voluntary and involuntary optimizations and takes away "grass root" control out of your hands.
if you are a **real** programmer assembly should appear cooler to you than C. It means you are a very good programmer.. well atleast theoretically.
- How do I have variables in assembly? Do I have to write to memory addresses and use several of them as variables?
you don't have C like variables in assembly (though in GAS - GNU Assembler you can have variable like stuff). you define memory locations, initialized and uninitialized, using
db directive for byte ("unsigned char" in C),
dw for word ("unsigned int" in C) and others like
dd for doubleword (simply "a couple of
words". "unsigned long" in C). like "var db 0x10". so here you defined a initialized memory location "var" with value "0x10". google assembly tutorials and you find lots of simply explaining tutorials.
- How do I store-up input from the user (using int 0x16) in a variable (or address)?
you can take input characters from user and store them into such a memory location character by character and end the string with a 0 (representing NULL). look into tutorials i suggested and you'll find answers in detail.
- How do I perform conditional IF statements? All I have found is '%IF' which is only for the compiler to run.
traditionally and practically there are no "if" constructs in assembly by name "if". the "%if" you are referring to is nasm specific conditional (macro like i guess) so not much useful in most situations. what you can (and should) do in assembly is to create loops and conditional jumps to *simulate* C IFs in assembly code. trust me they are the real *optimized* form of code generated by your C compiler also when you tell it to optimize your code. so you can pre-optimize your code using assembly
! cool eh? an example:
Code: Select all
test_condition:
mov ax,0x0ff ; moving 0x0ff into ax register
cmp ax,0x0ff ; compare value in ax reg to 0x0ff
je .1 ; is ax = 0x0ff? if YES jump to label ".1". if NO continue on next line..
mov ax,0x0bad ; its not! move 0x0bad (meant "bad") into ax..
jmp .2 ; we are done. so lets go to the *end* instead of passing through ".1"..
.1:
mov ax,0x0c001 ; it is! then move 0x0c001 (meant "cool" ;-) ) into ax..
.2:
hope it helps
see you!
regards
Re: Writing OS in Assembly (BTW: I'm Crazy)
Posted: Mon Mar 28, 2011 5:33 am
by Solar
Combuster wrote:BTW: Why is it that High Level coders think ASM is so hard anyway...
It's not hard, it just takes much, much more time to code the same things you can do quickly in a high-level language.
Excerpt from the song "Eternal Flame" by Julia Ecklar (
full lyrics here, YouTube audio
here):
I was taught assembler in my second year of school.
It’s kinda like construction work — with a toothpick for a tool.
Re: Writing OS in Assembly (BTW: I'm Crazy)
Posted: Tue Mar 29, 2011 6:10 am
by BOTOKILLER
phillid, you arent the only person, who is insane enough to try making OS in assembly))))
Im trying the same stuff, but I am still on bootloader stage(maybe it's because I always choose the hardest way to solve every problem I meet))