Re: the tutorials (#6)

If you are new to OS Development, plan on spending some time here first before going into the other forums.

Moderator: Moderators

Post Reply
scorpion007
Posts: 7
Joined: Sun Nov 18, 2007 9:32 am

Re: the tutorials (#6)

Post by scorpion007 » Sun Nov 18, 2007 9:42 am

In Tutorial 6, I was wondering why the bootloader sets org to 0, instead of 0x7c00, like you did previously?

You execute a jmp to main right after it, but won't the code be missed, since the BIOS requires the starting code to be at 0x7c00?

Code: Select all

org      0               ; we will set regisers later

start:   jmp   main               ; jump to start of bootloader

User avatar
Mike
Site Admin
Posts: 465
Joined: Sat Oct 20, 2007 7:58 pm
Contact:

Post by Mike » Sun Nov 18, 2007 4:54 pm

Hello,

The code will not be "missed". The org directive is only to let the assembler know what our expected base address is.

As long as the image is copied to the bootsector, the BIOS will load and execute the bootloader at 0x7c00.

Because of this, our code is still located at 0x7c00. Also, because the BIOS loads cs=>0x7c0 rip=>0 to jump to our bootloader, our code executes just fine ;)
Lead Programmer for BrokenThorn Entertainment, Co.
Website: http://www.brokenthorn.com
Email: webmaster@brokenthorn.com

User avatar
Warsome
Posts: 21
Joined: Sun Nov 18, 2007 3:13 am

can you clear this up, im a little confused.

Post by Warsome » Sun Nov 18, 2007 5:04 pm

There was something in the tutorial I noticed, maybe you could clear this up.

boot1.asm

Code: Select all

; read image file into memory (0050:0000)
     
  mov  ax, 0x0050
  mov  es, ax       ; destination for image
  mov  bx, 0x0000   ; destination for image
  push bx


I can understand that, the file contents is read
into address org 0x0050

but then when we go to stage2.asm

Code: Select all

bits   16

; Remember the memory map-- 0x500 through 0x7bff is unused above the BIOS data area.
; We are loaded at 0x500 (0x50:0)

org 0x500


your code says 0x500, i would of thought it would of been

Code: Select all

bits   16

; Remember the memory map-- 0x0050 through 0x7bff is unused above the BIOS data area.
; We are loaded at 0x0050 (0x0050:0)

org 0x0050


I am new to asm and might be wrong but in my first
asm test using code like this one i had this

boot16.asm

Code: Select all

      ; read image file into memory (0100:0000)
      ; destination of image CS
      mov     ax, 0x0100
      mov     es, ax

      ; destination for image IP
      mov     bx, 0x0000
      push    bx


test.asm

Code: Select all

[BITS 16]
[ORG 0x0000]

[SEGMENT .text]

   ; location where kernel is loaded
   mov   ax, 0x0100

User avatar
Mike
Site Admin
Posts: 465
Joined: Sat Oct 20, 2007 7:58 pm
Contact:

Post by Mike » Sun Nov 18, 2007 6:21 pm

Hello,

Remember that RMode uses the segment:offset memory model.

In this code:

Code: Select all

; read image file into memory (0050:0000)
     
  mov  ax, 0x0050
  mov  es, ax       ; destination for image
  mov  bx, 0x0000   ; destination for image
  push bx

0x50 is the segment (stored in ES), and 0x0 is the offset (stored in BX)

So, it becomes 0x50:0 in segment:offset notation.

OS Dev Series Bootloaders 2 tutorial gives a formula to convert this segment:offset address into a linear one:

Absolute (Exact) Memory Address = (Segment Address * 16(decimal)) + Offset


Following this formula, 0x50:0 is the same as 0x500.
Lead Programmer for BrokenThorn Entertainment, Co.
Website: http://www.brokenthorn.com
Email: webmaster@brokenthorn.com

User avatar
Warsome
Posts: 21
Joined: Sun Nov 18, 2007 3:13 am

Post by Warsome » Sun Nov 18, 2007 9:24 pm

So -> 0x0050:0 is the same as 0x500:0

what confused me is the fact that the other code I have keeps it as 0x0100, and your code follows the
same code principle but the value changes in stage2.asm

your example:

Code: Select all

  mov  ax, 0x0050 
  mov  es, ax       ; destination for image
  mov  bx, 0x0000   ; destination for image
  push bx


what i found on the net:

Code: Select all

  mov  ax, 0x0100 
  mov  es, ax       ; destination for image
  mov  bx, 0x0000   ; destination for image
  push bx


but calling that memory
address is different:

your example:

Code: Select all

bits   16
org 0x500


what i found on the net:

Code: Select all

bits 16
org 0x0000

[SEGMENT .text]
mov   ax, 0x0100


so its stage2.asm im questioning.

User avatar
Mike
Site Admin
Posts: 465
Joined: Sat Oct 20, 2007 7:58 pm
Contact:

Post by Mike » Sun Nov 18, 2007 9:52 pm

So -> 0x0050:0 is the same as 0x500:0

Not quite. 0x0050:0 is the same as 0x500 absolute liner address, NOT 0x500:0.

0x50:0 is the seg:offset address, 0x500 is the linear (Absolute) address.

0x500:0 is a seg:offset address representing 0x5000 absolute address, which is not what you want.

I am going to use the code that you got from the net for an example.

In this code:

Code: Select all

  mov  ax, 0x0100
  mov  es, ax       ; destination for image
  mov  bx, 0x0000   ; destination for image
  push bx

This is pointing to seg:offset address 0x100:0, which is 0x1000 (Absolute linear address.)

segment:offset addresses are different from absolute addresses.

Lets look at the other code you posted from the net to complete the example...

Code: Select all

bits 16
org 0x0000

[SEGMENT .text]
mov   ax, 0x0100

Remember that 0x100 is the segment address using segment:offset notation. So, this is 0x100:0.

Remembering the formula to convert this to an absolute address, 0x100:0 = 0x1000 (Linear address), NOT 0x1000:0.

Because this code is located at 0x1000 physical address, or (0x100:0 in seg:offset notation), all we need to do is insure that the segment registers are set to the segment to use (0x100)

The org directive is only needed to let the assembler know the base address. Because the code sets the segment registers during execution, the ORG can be 0.
Lead Programmer for BrokenThorn Entertainment, Co.
Website: http://www.brokenthorn.com
Email: webmaster@brokenthorn.com

User avatar
Warsome
Posts: 21
Joined: Sun Nov 18, 2007 3:13 am

Post by Warsome » Mon Nov 19, 2007 1:30 am

All I know is the assembly I have does work, the bootloader is from chaosOS calling kernel.bin on a fat12 floppy. The second part I added too, for example, I added commands clearscreen, help, reboot, flush, shutdown and modified the unknown command parameter, i am constantly adding too the code and testing it on a old computer before testing it on my main pc.

Im just a little confused by your bootloader calling
stage2.asm into 0x0050 but in stage2.asm it says the address is 0x500, if i tried that with the code I already have It finds the secont binary file and then does nothing but crash.

I will give it a run through again. Lovely
being new to assembly lol.

Would you like me to send through my code? maybe it has a potential bug inside it i am not aware of.

User avatar
Mike
Site Admin
Posts: 465
Joined: Sat Oct 20, 2007 7:58 pm
Contact:

Post by Mike » Mon Nov 19, 2007 2:04 am

Im just a little confused by your bootloader calling
stage2.asm into 0x0050 but in stage2.asm it says the address is 0x500

Not quite. The bootloader is loading (and executing) stage 2 at 0x500 (NOT 0x0050), which is how it works ;)

It sounds to me you may be confusing the segment:offset and absolute memory models.

if i tried that with the code I already have It finds the secont binary file and then does nothing but crash.

Hm... would you mind elaborating on this for us?

Also, can you please post your log file from Bochs? (We only need the part where the problem is at.)

Would you like me to send through my code? maybe it has a potential bug inside it i am not aware of.

Yes, that would be very helpful :)
Lead Programmer for BrokenThorn Entertainment, Co.
Website: http://www.brokenthorn.com
Email: webmaster@brokenthorn.com

User avatar
Warsome
Posts: 21
Joined: Sun Nov 18, 2007 3:13 am

Post by Warsome » Mon Nov 19, 2007 4:44 am

I will pick it up in the end :) everyone starts somewhere

Post Reply