NASM+OSDev Help

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Paeron

NASM+OSDev Help

Post by Paeron »

Hello there!

I am working on an OS in NASM and I am a newbie so therefore I have run into a problem. It's hard to explain because I dont know whats wrong so I will put the code here.

Both the bootloader part and the kernel are put together in one file with this code:

Code: Select all

; Drummer disk image

%include 'drummer.asm'
%include 'drummker.asm'
This is the bootloader:

Code: Select all

; Drummer bootstrap

[ORG 0]

   jmp 07C0h:start
; DATA
   msg       db 'Wellcome to Drummer 0.0.1',13,10,0
   msg2      db 'This is a simple OS I made for learning purpouses.',13,10,'To Find out more, type info in the kernels commandprompt.',13,10,13,10,0
   msg3      db 'Loading the kernel...',13,10,0
   err      db 'NO! An error occured! DAMN!',0
   

message:         ; Dump ds:si to screen.
   lodsb

   cmp al, 0
   je done

   mov ah, 0Eh
   mov bx, 7
   int 10h

   jmp message

done:
   ret


start:
   mov ax, cs
   mov ds, ax
   mov es, ax

   mov si, msg
   call message

   mov si, msg2
   call message

   mov si, msg3
   call message

load:            ; Load the Kernel!
   mov ax, 1000h
   mov es, ax
   mov bx, 0

   mov ah, 2      ; Load disk data to ES:BX
   mov al, 5      ; Load 5 sectors
   mov ch, 0      ; Cylinder=0
   mov cl, 2      ; Sector=2
   mov dh, 0      ; Head=0
   mov dl, 0      ; Drive=0
   int 13h         ; Read!

   jc error      ; On error display an error message


   jmp 1000h:0000      ; Jump to the kernel

error:
   mov si, err
   call message
hang1:
   jmp hang1   

times 510-($-$$) db 0
dw 0AA55h
And this is the kernel code:

Code: Select all

jmp kernel

; Kernel Data

   welcome      db 'Kernel Loaded OK! Have a nice day!',13,10,0
   prompt      db 'Drummer:',0

message2:         ; Dump ds:si to screen.
   lodsb

   cmp al, 0
   je done2

   mov ah, 0Eh
   mov bx, 7
   int 10h

   jmp message2

done2:
   ret


kernel:
   mov ax, 1000h
   mov ds, ax
   mov es, ax


   mov si, prompt      ; This won't work.... I don't know why!
   call message2      ; <<---------------------------------<<

   mov ah, 9      ; print "===="
   mov al, '='      ;
   mov bx, 7      ;
   mov cx, 4      ;
   int 10h         ;


hang:
   jmp hang
I hope that someone knows whats wrong and is willing to help me...

//Martin Hult?n Ashauer
sion

Re:NASM+OSDev Help

Post by sion »

Hi, well i can't really give you an in-depth explination but basicly the line that gives the problem is 'mov dx, ax' you'll find that if you put your printing function right before that line it'll work...i had to learn that the hard way moving things around. So yea i believe it has to do with the fact that [DS:SI] are related in a way and when you move 1000h into DS and then a string into SI it doesn't work properly. I don't really know why since im pretty new to assembly myself so if anyone else has an idea why please let me know.

Sion
Curufir

Re:NASM+OSDev Help

Post by Curufir »

You've setup to have all your labels refer to offsets from the start of the file (The ORG 0 statement). Now when you jump into your kernel you load up a new segment (0x1000), but the label to your string is pointing to an offset from the start of the whole file, not the start of the kernel part (So it could be practically anything, maybe 600 bytes offset or so). Needless to say when your string function goes looking for 0x1000:0x0600 it will be looking in the wrong place. This is also the case for any of your jumps/calls, in fact anything that uses a label in the kernel part.

Simplest solution woudl be for you to split the two files apart, assemble seperately with ORG 0 at the top and use partcopy or something to dump them on the appropriate sectors of the disk.

As for other errors, who knows, I found this one and stopped looking.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:NASM+OSDev Help

Post by Pype.Clicker »

oh, btw, be nice : set up a stack :)

Any of your calls can make any piece of your code damaged as long as you didn't make sure the stack isn't somewhere you put code or static data ...

It shouldn't do any harm as long as you're in the bootloader (because bios manufacturer aren't dumb enough to make the stack overwrite the bootsector image in memory ;) ), but once the kernel is loaded ... well ...
sion

Re:NASM+OSDev Help

Post by sion »

Here's a solution to you problem. When you first set up the offset:segment memory when read the kernel do this:
mov ax, 1000h
mov ds, ax '<---- add this
mov es, ax
mov bx, 0

Thats in your boatloader. Then remove the code loading 1000h in your kernel and thats it...now it should work fine...oh and pype's right you should set up a stack in your boatloader....hope that helps...sion
Paeron

The Stack?

Post by Paeron »

Ok.... I will setup a stack... but since I am new to OSDev and ASM I dont know how that is done. Could anyone tell me that procedure.

Thanx alot you others for the help!

//Martin "Paeron" Hult?n Ashauer
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:NASM+OSDev Help

Post by distantvoices »

example:

say you have defined segment 0x10 as your stack segment.
say you want to have your esp point to 0xffff.

mov ax,0x10
mov ss,ax
mov esp,0xffff

and voila, you have set up a stack. think about good locations for your stack and remind: the stack grows to the lower regions of your memory and it is dword aligned (this I have learned recently - even three years of special education didn't reveal this small but importend thing to me).
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
Paeron

Re:NASM+OSDev Help

Post by Paeron »

Ok.... but could you tell me what the ss and esp registers are for??
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:NASM+OSDev Help

Post by distantvoices »

paeron:

perica is right, but lets say it simple: ESP is the STACK-pointer. It points to the actual item you have pushed to the stack. Upon POP ... ESP is incremented towards higher Memory areas.

SS is the Stack segment. If you use an segmented memory management approach, you create such a stack segment in your Global Descriptor Table as well as for each Process in its Local Descriptor Table. In this stack segment, your stack resides.

You need the stack for function calls and kernel transitions and so forth on a stack based system as the ix86 architecture is.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:NASM+OSDev Help

Post by Pype.Clicker »

note: using 0x10 for the stack segment is typical from protected mode. as you're in a bootsector, you're more likely to need a real-mode stack. SS=0x9000 and SP=0xFFFE should do the trick.

Btw, if don't know what the stack is for i *strongly* suggest you look for a tutorial on assembly programming before you start... even if you plan to write your kernel in a HLL, stack-knowledge is *mandatory* for system programming ...
Post Reply