Writing OS in Assembly (BTW: I'm Crazy)
Writing OS in Assembly (BTW: I'm Crazy)
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)
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)
phillid - Newbie-ish operating system developer with a toy OS on the main burner
-
- Member
- Posts: 255
- Joined: Tue Jun 15, 2010 9:27 am
- Location: Flyover State, United States
- Contact:
Re: Writing OS in Assembly (BTW: I'm Crazy)
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.- How do I have variables in assembly? Do I have to write to memory addresses and use several of them as variables?
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
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.
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 store-up input from the user (using int 0x16) in a variable (or address)?
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:- How do I perform conditional IF statements? All I have found is '%IF' which is only for the compiler to run.
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)
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...
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...
My hero, is Mel.
Re: Writing OS in Assembly (BTW: I'm Crazy)
Hi ,
Cheers.
How do I have variables in assembly? Do I have to write to memory addresses and use several of them as variables?
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.How do I store-up input from the user (using int 0x16) in a variable (or address)?
Code: Select all
Mov byte [SomeVariable] , 3 ; this will move 3 to the address specified by SomeVariable
SomeVariable:
db 0
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 likeHow do I perform conditional IF statements? All I have found is '%IF' which is only for the compiler to run.
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.
Even the smallest person could change the course of the future - Lord Of The Rings.
In the end all that matters is what you have done - Alexander.
Even after a decade oh god those still gives me the shivers.
In the end all that matters is what you have done - Alexander.
Even after a decade oh god those still gives me the shivers.
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Writing OS in Assembly (BTW: I'm Crazy)
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.BTW: Why is it that High Level coders think ASM is so hard anyway...
Re: Writing OS in Assembly (BTW: I'm Crazy)
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)
There are several of us who are coding our OSes in ASM (combuster, brendan, and me, to list a few).
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.
Basically, the deal is:BTW: Why is it that High Level coders think ASM is so hard anyway...
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)
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.bewing wrote:and in 6 months you will forget how 10 percent of the code was supposed to work.
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)
Thanks guys
phillid - Newbie-ish operating system developer with a toy OS on the main burner
Re: Writing OS in Assembly (BTW: I'm Crazy)
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!
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)
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.
hope it helps
see you!
regards
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.
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 have variables in assembly? Do I have to write to memory addresses and use several of them as variables?
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 store-up input from the user (using int 0x16) in a variable (or address)?
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:- How do I perform conditional IF statements? All I have found is '%IF' which is only for the compiler to run.
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:
see you!
regards
Everyday schedule: wake up >> eat >> write OS >> eat >> sleep
Re: Writing OS in Assembly (BTW: I'm Crazy)
Excerpt from the song "Eternal Flame" by Julia Ecklar (full lyrics here, YouTube audio here):Combuster wrote: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.BTW: Why is it that High Level coders think ASM is so hard anyway...
I was taught assembler in my second year of school.
It’s kinda like construction work — with a toothpick for a tool.
Every good solution is obvious once you've found it.
- BOTOKILLER
- Member
- Posts: 28
- Joined: Tue Jan 04, 2011 10:25 am
- Location: Ukraine
Re: Writing OS in Assembly (BTW: I'm Crazy)
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))
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))