flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Boot loader....w/o the...boot loader?

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
ryanazar



Joined: 28 Jul 2005
Posts: 1
ryanazar
Greetings everyone!

My name is Ryan and I stumbled upon this site a few weeks back. I have been toying with the idea of starting with a simple CLI operating system, and slowly building it up into something more advanced.

Today I made the plunge, and wrote a little something from various resources around the net. FASM is just so awesome Very Happy

Here is my code, its very well documented, may help some other newbies like myself:

Code:
; CactOS Boot Loader
; a work in progress :^)

org 0x7c00                                           ; load bootsector from this mem address
use16                                                ; go into 16 bit mode

start:                                               ; start the boot loader
        mov ax, 0x0000                               ; load ds register with current segment
        mov ds, ax

        mov si, String1                              ; Write String1 to the screen
        call PrintMsg                                ; calls function PrintMsg

        mov si, String2                              ; Write String2 to the screen
        call PrintMsg                                ; calls function PrintMsg

        jmp $                                        ; End of program loop

PrintMsg:
        mov ah, 0x0E                                 ; change screen to teletype mode
        mov bh, 0x00                                 ; we are writing on page number 0
.nextchar:
        lodsb                                        ; load [si] to al and increment si
        or al, al                                    ; set zero flag if al = 0
        jz .return                                   ; if zero flag is set, jump to break
        int 0x10                                     ; call bios video function
        jmp .nextchar                                ; loop around to write next character
.return:
        ret                                          ; return

String1: db "CactOS boot loader", 13, 10, 0          ; 13, 10 means 'new line'
String2: db "version 0.1", 0                         ; zero at end means end of a string

times 510-($-start) db 0                             ; fill the empty spaces to make 512 bytes
dw 0xaa55                                            ; we are finished!    


Basically, when i boot with a floppy with this code on it, the compuer displays:
[tons of bios junk here]
CactOS boot loader
version 0.1

Now, I have three questions, and (hopefully) they are simple!

1) Is there a simple way to clear the screen before i display my two strings?

2) I want to build my OS around a floppy drive, so it is easy to carry around and test on different machines. Would this mean i would create code to load a few fat12 sectors (or fat16??) off of the floppy? Also, I haven't researched it in-depth yet, is this an easy task to accomplish?

3) Is there a table so i can find out what jz, ds, si, and similar commands do? This would be very helpful Smile

Thanks for your input. I hope this simple code will help other new people to ASM and OS DEV like myself!

Greetz,

-Ryan
Post 28 Jul 2005, 04:17
View user's profile Send private message Reply with quote
Night Rider



Joined: 28 Jul 2005
Posts: 72
Night Rider
Quote:
1) Is there a simple way to clear the screen before i display my two strings?
I think that simpliest way is to print 25 blank strings before two neede.
Ofcourse, you can just clear videomemory - its faster and simpier, BUT
then you've got to change cursosr position. With first method, your messsages will be printed on the bottom of the screen.
So, best way(but not simpliest) to clean videomemory and call BIOS function to put cursor on location 0,0.
Quote:
2)Would this mean i would create code to load a few fat12 sectors (or fat16??) off of the floppy? Also, I haven't researched it in-depth yet, is this an easy task to accomplish?

FAT12.
In reality, that's not right question. It depends on some factors. How do you plan to put files and other stuff on floppy? If you want to copy files with ms-dos or windows, then you need to use fat12 formatted diskettes. That is NOT hard to do code for it. But there is other way, too. For example, i create my own os on diskette with my own file system.(it is faster than fat12). So i've made special program, which 'understands' my diskettes. All tehnical stuff - boot sectors etc you can put with Disk Editor (NU) or with qview((hex editor)to disk image, writing it then to disk. With rawrite or special program for Windows - i don't remember name of it. Maybe, windows image manager or something like that...)

Quote:
3) Is there a table so i can find out what jz, ds, si, and similar commands do? This would be very helpful

Hm... what can i say... You do not have much expirience with Assembeler, as i see... That is nothing. I started OS developement to study Assembler.

Then You need to download some manual about it. Because, jz is command, but ds&si are registers. If you did not know it, you need to start with very beginning. Read Peter Norton's Assembler manuals. I have some manuals. If you rally need them, give a e-mail, i will try to dig them out from my hdd...

_________________
My best regards
Post 28 Jul 2005, 10:59
View user's profile Send private message ICQ Number Reply with quote
tom tobias



Joined: 09 Sep 2003
Posts: 1320
Location: usa
tom tobias
greetings ryanazar. Thanks for your interesting, and informative post. Well done. I like your attitude.
There is no such thing, in my opinion, as TOO much documentation.
You asked about clearing the screen.
This is actually a question concerning manipulation of the video display. Three methods:
1. DOS, if you are using it;
2. BIOS, the solution you employed to write your messages;
3. Write directly to the video buffer.
Since you are employing BIOS already, you may wish to rely upon function 0 of interrupt 10h (yields grey letters on black background, else, function 09h which gives colors. use mov al,' ', after placing a large value, e.g. 2000, into cx to serve as loop counter.
A couple of comments:
1. As you correctly pointed out, your submission to the forum is VERY helpful to novices. Yet, you have fallen into the trap of 99.9% of the forum members, by misusing a Boolean instruction, OR, to clear a register. No need. Just employ mov al,0 to accomplish the same goal, clearing the register, without gobbledygook. Yes, your method "looks" more professional, i.e. you emulate the mistakes made by the more experienced members of the forum. Stay true to your stated goals of submitting superbly documented elementary material. Eliminate the mystery. WRITE programs, not code, that only a handful of supposedly wise people can read and understand. It may seem trivial, even banal, to insist upon RESERVING Boolean operators for Boolean TASKS, but, in fact, by doing so, you are concurrently developing the skills needed later to become a programmer, instead of a coder. Most of the people on this forum can not understand my insistence on this point. Since you are just commencing, perhaps you will be able to comprehend this argument.
2. There remain two mysteries, notwithstanding your commendable effort to explain what you are doing, and why:
a. at the outset, you write:
org 0x7c00 ; load bootsector from this memory address.

This may not be evident to all. "org" is a FASM directive, right? (In my scheme, ALL fasm directives are CAPITALIZED, to differentiate them from Intel instructions.) How does ORG function? Where does this address, 7c00h, originate? Is it prescribed by DOS, by fat32, by convention, by Intel, by Microsoft, by the motherboard manufacturer's chipset, by all ide controllers, by the start up program on the eeprom?
Your comment, "load bootsector from this memory address" is not wrong, it is appropriate, but I think its amplification is compatible with your professed goal of writing a program that can assist novices. Perhaps you imagine that everyone knows the answer to these questions, so there is no need for further explanation. I doubt this perspective.
b. at the end of your otherwise excellent program, you write:

times 510-($-start) db 0 ; fill the empty spaces to make 512 bytes
dw 0xaa55
Well, this is again a question of preference, and of method.
1. "times" is another FASM directive, right? TIMES.
2. "$-start" is FASM gibberish. Explain what it means. It is NOT intuitive to any novice. Is "start" a specific location, or a relative, FASM determined location, or a DOS determined location, or an eeprom specified location?
3. 510, presumably because the last two bytes in this location contain 0aa55h, why do the last two bytes contain this value?
4. why does one need to fill the preceding 510 bytes at memory location ??? with zeros? What happens if these locations contain values other than zero? Why should it make any difference what value they contain? If 0aa55h is such an important number, why not place it at the beginning of the 512 byte sector of the storage device, rather than the end?
Establishing answers to these questions is for some (many, most, nearly all?) members of the forum a WASTE of time. It is not a waste of time for those who would like to actually comprehend the initial boot process, so as to be able, for example, create one's own operating system, or one's own assembler, perhaps even one's own FILE system! Clarifying the relationship between DOS, FAT, FASM, and the ide controller/eeprom in the boot process is one step in the direction of gaining independence from all of them.
Regards, tom
Post 28 Jul 2005, 11:08
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
You can try this simple code.
Code:
;*******************************************; Clear screen;*******************************************cls:        mov   ah,0Fh        int   10h        XOR   AH,AH        int   10h        ret;*******************************************; cursor;*******************************************cursor:        mov   ah,02h        XOR   BH,BH    ;= page number( 0 is normal).        XOR   DL,DL    ;= row        XOR   DH,DH    ;= coloum        int   10h        ret    


If you go here: http://alexfru.chat.ru/epm.html#bootprog
and get "bootprog" you may find the code helpfull.
Post 28 Jul 2005, 11:55
View user's profile Send private message Reply with quote
bubach



Joined: 17 Sep 2004
Posts: 341
Location: Trollhättan, Sweden
bubach
The simplest and smallest way of clearing the screen and setting the cursor to position 0,0 at the same time is to call int 0x10, a BIOS interrupt and specify that you want to change videomode to mode 3 (mode 3 is the standard textmode).
Code:
          mov     ah, 0                               ; Function 0 of int 0x10 ( set video mode)
          mov     al, 3                               ; Set mode 0x03
          int     0x10                               ; call bios code..
    


HTH, Christoffer..
Post 28 Jul 2005, 20:54
View user's profile Send private message Reply with quote
MichaelH



Joined: 03 May 2005
Posts: 402
MichaelH
tom tobias, thanks for that post, I learnt a lot and will from now on go out of my way to try and follow your thoughts as I often look at asm code and think, what the heck is that for?

To all you folk that help us newbies, thanks heaps!
Post 29 Jul 2005, 05:51
View user's profile Send private message Reply with quote
tom tobias



Joined: 09 Sep 2003
Posts: 1320
Location: usa
tom tobias
Thanks MichaelH, for your kind comments, much appreciated--actually, the "thoughts" you mention above, are derived from my reading of the Swiss Computer Scientist, Niklaus Wirth's books. Wirth's many contributions were noted by Dr. Nikolai Bezroukov at his web site:
http://www.softpanorama.org/Lang/assembler.shtml
as illustrated earlier today by T.Grysztar, creator of FASM, and founder of this forum.
In essence, Dr. Wirth's ideas are these:
Define all variables BEFORE using;
Explain in readily understood language the goal of any task;
create a program as though writing a novel;
avoid cryptic punctuation, jargon, acronyms;
SIMPLICITY = READABILITY = ELEGANCE.
With regard to learning assembly language, while I share Grysztar's and Bezroukov's admiration for the numerous accomplishments of Dr. Donald Knuth, I do not believe that his books are the BEST available tool to learn assembly language. Knuth's books, revisions of works from the 1950's and 1960's, are better thought of, in my opinion, as historical references, slightly outdated at present.
For example, in the 1997, 3rd revised edition of volume 1, "Fundamental Algorithms" of "The Art of Computer Programming", Knuth writes, on page ix: "For example, some combinatorial calculations need to be repeated a trillion [10^12] times, and we save about 11.6 days of computation for every microsecond we can squeeze out of their inner loop." Please refer to the main FASM forum from a couple of days ago, for an excellent discussion about computing the cosine. In Knuth's era, the cpu clock was operating in KILOHERTZ, consequently, saving a MICROSECOND was significant. Large megabyte sized lookup tables were impossible, because main memory was so small. Conflicts between cache and main memory were inconceivable. Elimination of ALL loops was just not on the menu.
So, if not in Knuth's books, where is the best place to learn ASSEMBLY language programming? I think this forum represents the best place to learn it!
regards, tom, usa Smile
Post 29 Jul 2005, 22:30
View user's profile Send private message Reply with quote
Night Rider



Joined: 28 Jul 2005
Posts: 72
Night Rider
IMHO. the best books round and about Assembler written by Peter Norton... That is the great Author. IMHO Wink

_________________
My best regards
Post 30 Jul 2005, 01:20
View user's profile Send private message ICQ Number Reply with quote
tom tobias



Joined: 09 Sep 2003
Posts: 1320
Location: usa
tom tobias
Night Rider wrote:
IMHO. the best books round and about Assembler written by Peter Norton... That is the great Author. ...

This could almost be a separate thread, on opinions concerning x86 assembly language programming books.
Norton: I admit, I do not own his original text. I do have the "Advanced Assembly Language" text from 1991 published as part of the Peter Norton Programming Library, by Steven Holzner and Peter Norton Computing. It is a singularly unimpressive tome, in my opinion--focus on DOS/fat/bios/real mode--basically useless.
Some books I find worthwhile:
1. top book, in my opinion, published ten years ago, "Assembly language Master Class" by a dozen Russians, (and a couple of Americans,) including, but not limited to, Chebotko and Kalatchin. (WHAT IS THAT BLOODY @@neven_cluster on page 349???) DON'T USE PUNCTUATION to create CODE--write with normal, human readable alphabetical characters to create PROGRAMS instead!!!!!Of course, if one does not know or understand the Roman alphabet, perhaps it is easier to read and understand @@ than ??? whatever. neven????? What's that, NOT EVEN??? Does he mean ODD???This chapter, nine, was authored by Malakhov and Kalatchin entitled, "Low-Level Disk Techniques", though, unfortunately, it is all about DOS and FAT, not programming the IDE controller directly, as I had hoped.....Anyone know of a good reference to this topic--programming the IDE controller directly, without using BIOS, or perhaps the code from the BIOS disassembled???
2. close second, though dated now, first published in 1993 (wish there were a second edition!) "i386/i486 Advanced Programming" by Sen-Cuo Ro, and Sheau-Chuen Her (vanNostrand Reinhold publishers).
One book I have recently read, and found uninformative, is "Professional Assembly Language" by Richard Blum, published in 2005 by Wiley. Perhaps my disenchantment is based upon his use of gas, one of my least favorite assemblers. I also dislike the book because of his omission of FASM. He does describe MANY other assemblers, including Hyde's HLA. For a book on this subject, published in 2005, to omit the most important assembler in use today, is simply ignorant. I give this illustration as a justification for my assertion, NOT ACCEPTED by everyone on the FASM forum, in fact, perhaps MANY people disagree with me, but I find no justification for use of NOP:
page 198:
" ...
_start:
nop
cvtps2dq value1, %xmm0
...."
I could almost write a whole book complaining about this one fragment. THIS IS CODE, not a program.
NEVER start a procedure/variable/module/function with an UNDERSCORE. This is SHEER NONSENSE: _start. It was nonsense when first used in the Bell Labs forty years ago, and it is STILL STUPIDLY used today. Why does BLUM include NOP? We don't know, because he doesn't document his code, nor does he explain his examples. I suspect he also does not know why he is using NOP. IF THERE IS A REFERENCE IN THE INTEL INSTRUCTION MANUAL attesting to the need to employ NOP prior to employing any of the conversion instructions, then I AM THE IGNORANT one--please teach me. NOP was used in programs back in the 1960's to ensure synchronization. Are we still using NOP in these days of prefetch, pipelines, cache, and branch prediction? Crying or Very sad
Post 30 Jul 2005, 15:15
View user's profile Send private message Reply with quote
Night Rider



Joined: 28 Jul 2005
Posts: 72
Night Rider
Maybe. But book (translation to russian i have read) about some methods of writing optimized code impressed me. It was about programming under MS-DOS, yeah, but tips and examples, specialities about 8086 &80286 were really good.
"by Steven Holzner and Peter Norton Computing." Yes, i mean original Norton's books, not from Symantec or Norton Computing library/corp.
"This is SHEER NONSENSE: _start" +. I think semicolon/underscore style came from C. Not best semantic/grammar to take example from, imho.
About readable code - in my programsa i often use names as /name-of-procedure/-1/2/3/etc. Because i write these programs not for anybody, and i recognise such names. (as well as transliterated russian words as names - for example - VREMA. And what?
But, i am agree that "@@neven_cluster" - ***t
"Anyone know of a good reference to this topic--programming the IDE controller directly: I know Very Happy But in russian language. VERY good book. about 350 pages about DIRECT IDE FDD ATAPI etc.
About nop - hmmm... maybe he tried to MANUALY(????) align code. Maybe it was disassembled(assembler aligned it with puting nop, but the author didn't understand it...
By the way, "@@neven_cluste" what this was used for? If there were russian author, it mean nevernyj nomer clastera - wrong cluster number??? Who knows... let philosophians think about it, we will keep programming. (nothing personal, just my thoughts)
Post 30 Jul 2005, 19:09
View user's profile Send private message ICQ Number Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 559
smiddy
tom tobias wrote:
...but I find no justification for use of NOP:
page 198: ...
I suspect he also does not know why he is using NOP. IF THERE IS A REFERENCE IN THE INTEL INSTRUCTION MANUAL attesting to the need to employ NOP prior to employing any of the conversion instructions, then I AM THE IGNORANT one--please teach me. NOP was used in programs back in the 1960's to ensure synchronization. Are we still using NOP in these days of prefetch, pipelines, cache, and branch prediction? Crying or Very sad


Hi Tom, smiddy here, damn good question. I asked this same question a few years back because, me being a hardware guy, was pretty sullen by the fact that a software (at the time I called them this) weiner (no offense, these were my thought then, they've changed...slightly) used NOP for timing issues, instead of writting a bit of code to do specific timing. Seven NOPs in a row, I was ... my friggin jaw hit the ground ... this is your timing routine, you have to be [explitive not entered, but is like excrementing] me. I have yet to find a clear reference on any platform, Intel, Motorola, whomever...as to why someone would put an instruction into a set that does nothing but use time off of the cpu.
Post 31 Jul 2005, 01:02
View user's profile Send private message Reply with quote
tom tobias



Joined: 09 Sep 2003
Posts: 1320
Location: usa
tom tobias
Night Rider wrote:
... But in russian language. VERY good book. about 350 pages about DIRECT IDE FDD ATAPI etc......, we will keep programming. (nothing personal, just my thoughts)

And very good thoughts too, well expressed.
Ummm, does this Russian book have a name, author, ISBN number, publisher, something that could serve as a reference so I can purchase one? Even if ALL the explanation is in Cyrillic, it is still better than no information at all.....I will purchase it, if it is available in a book store. If necessary, I will fly to Russia to get it. Spaseeba. Smile
Post 31 Jul 2005, 09:38
View user's profile Send private message Reply with quote
Night Rider



Joined: 28 Jul 2005
Posts: 72
Night Rider
You are welcome, maybe there is a translation.
ISBN 5-318-00623-X
code 9 785318 006234
publisher: Piter (www.piter.com)
Name&author(my translation& transliteration)
"Anathomy of PC" series: Programming disk systems of PC
Vladimir Kulakov 2002 y.
with diskette with sources on it.
I have bought it in Moscow on New Arbat street - Moscow House of Book (Dom Knigi) second floor, on the left of stairs.207 roubles.(2 y ago)
Green book with pictures of hdd wires&CPU, lense with hdd in it.
I used fdd refernce from this book to write fdd driver for my OS. So, it helped me well.All text in cyrillic, except examples.(they are in international language Assembler Very Happy). If questions appear, i can help U with translating in my spare time, if needed. Maybe there's translation, who knows...
Spasibo? Pozalujsta.
Post 31 Jul 2005, 14:05
View user's profile Send private message ICQ Number Reply with quote
tom tobias



Joined: 09 Sep 2003
Posts: 1320
Location: usa
tom tobias
Thanks Night Rider....This was a very helpful post.
http://www.xe.com/ucc/
207.00 Russia Rubles = 7.24 USD United States Dollars = 5.97 Euros
www.piter.com = http://shop.piter.com/eng/

The good news is that they seem to have the book in stock, looks like 59 copies, if "py6" means copies... Anyway, it is obvious, even to illiterate morons like me, that the author is kylakob, i.e. kulakov, as you so kindly noted. I have sent them an email, not sure if they will understand me.
Let's stay in touch, perhaps you may wish to send me your email address by private message. Wink
Post 31 Jul 2005, 17:49
View user's profile Send private message Reply with quote
THEWizardGenius



Joined: 14 Jan 2005
Posts: 382
Location: California, USA
THEWizardGenius
As he mentioned in the comment, "or al, al" is not to clear al, but to set the Zero Flag (CF) if AL==0. In order to clear AL, you can use the code "xor al, al", and I see no problem with that. To most people, "xor al, al" has only one purpose - to clear AL. XORing a register with itself always results in zero, and never anything else, so this is a simple speed/size optimization, which "sub al, al" can also accomplish.

The code is fine, although it technically is very primitive as it does not use a filesystem. I think it is foolish to want freedom from FAT12, tom tobias, because it is a simple and easy-to-implement filesystem, and also a standard. I don't like M$ but I see no problem with using their (old, outdated) filesystem. Using a hexeditor to make your own filesystem won't make you a "better programmer" or any such thing. It works, and I will probably do that when I make my own FS, but for a newbie using FAT12 is OK. Good luck ryanazar.
Post 01 Aug 2005, 16:38
View user's profile Send private message AIM Address Reply with quote
Night Rider



Joined: 28 Jul 2005
Posts: 72
Night Rider
Quote:

but I see no problem with using their (old, outdated) filesystem.

For FDD it is enough sophisticated and secured. For fdd...
Post 01 Aug 2005, 18:38
View user's profile Send private message ICQ Number Reply with quote
THEWizardGenius



Joined: 14 Jan 2005
Posts: 382
Location: California, USA
THEWizardGenius
Well yeah. It's stupid to use it on a hard drive.
Post 01 Aug 2005, 19:46
View user's profile Send private message AIM Address Reply with quote
Night Rider



Joined: 28 Jul 2005
Posts: 72
Night Rider
It's impossible to use FAT12 on hdd. (almost)
Post 01 Aug 2005, 21:02
View user's profile Send private message ICQ Number Reply with quote
tom tobias



Joined: 09 Sep 2003
Posts: 1320
Location: usa
tom tobias
THEWizardGenius wrote:
As he mentioned in the comment, "or al, al" is not to clear al, but to set the Zero Flag (CF) if AL==0. In order to clear AL, you can use the code "xor al, al", and I see no problem with that. To most people, "xor al, al" has only one purpose - to clear AL. XORing a register with itself always results in zero, and never anything else, so this is a simple speed/size optimization, which "sub al, al" can also accomplish.
....

Hi Wizard Genius,
First of all, THANK YOU, for discussing this question. I appreciate your many outstanding posts to this forum, and I am grateful to you for challenging my criticism. Some people are a little bit "thin skinned", and easily offended by harsh, uncomplimentary writing, but, I find that straightforward, honest expressions of one's opinion to be a sign of FRIENDLINESS, not disrespect.
Secondly, I MAY HAVE BADLY misunderstood Ryan's initial subission to the list:
Here's Ryan's code:
.nextchar:
lodsb ; load [si] to al and increment si
or al, al ; set zero flag if al = 0
jz .return ; if zero flag is set, jump to break
int 0x10 ; call bios video function
jmp .nextchar ; loop around

Now, AS I COMPREHEND this situation, (maybe incorrectly) Ryan seeks to TEST the condition of the al register, which he is using to control program flow. In particular, if al has a value of zero, then, the program should exit the loop, else, the loop should continue. Well, in my opinion, a better way to control program execution, in this or any other situation, is to simply ask, is the value of al = 0?. To my way of thinking, the proper way to do that, is with TEST. No need for boolean operators, and certainly no need to clear a flag in order to establish the contents of any register. That's my opinion, thanks for the discussion. regards, tom Smile
Post 03 Aug 2005, 23:45
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 559
smiddy
I like this:

Code:

.NextCharacter:

   lodsb              ; Load [SI] in AL and increment SI
   cmp al,0           ; Is AL = 0?
   je .Done           ; Yes, get out of here
   int 010h           ; No, call BIOS Video function
   jmp .NextCharacter ; Do it all again until [SI] = 0

.Done:

   ret

    


In my opinion this reads pretty straight forward to me.
Post 04 Aug 2005, 00:19
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.