Author Topic: Why can't I use ORG twice?  (Read 9802 times)

Offline ben321

  • Full Member
  • **
  • Posts: 185
Why can't I use ORG twice?
« on: February 12, 2023, 09:14:51 PM »
I'm trying to write a program for DOS that switches to 32-bit protected mode, and have the segment start at physical address 0x00000000. But there's a problem. When in protected mode, the addressing is no longer done based on the 16-byte segment address that real mode uses. Instead it's based on the the start of the much larger segment defined in the GDT. The result of this is that the portion of the code that is written for protected mode needs a separate ORG statement, due to it using a completely different addressing method. But if I have 2 ORG statements in my ASM file, NASM refuses to assemble the code. How do I fix this?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Why can't I use ORG twice?
« Reply #1 on: February 12, 2023, 10:12:24 PM »
Hi Ben,

You have only one origin in your code: where it starts. I seriously doubt if you want it to be at physical 0! That's where your Interrupt Vector Table lives. Think again about what you really want to do...

Best,
Frank


Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 224
  • Country: gb
    • GPUZoo
Re: Why can't I use ORG twice?
« Reply #2 on: February 12, 2023, 10:17:48 PM »
I'm not sure, as it's a long time since I wrote relevant code, but I think you need to jump to the new CS:IP.
My graphics card database: www.gpuzoo.com

Offline ben321

  • Full Member
  • **
  • Posts: 185
Re: Why can't I use ORG twice?
« Reply #3 on: February 12, 2023, 10:30:37 PM »
I'm not sure, as it's a long time since I wrote relevant code, but I think you need to jump to the new CS:IP.

Already got that part but the problem is doing the correct far jump. You get that wrong, and bad things happen.

Offline ben321

  • Full Member
  • **
  • Posts: 185
Re: Why can't I use ORG twice?
« Reply #4 on: February 12, 2023, 10:37:55 PM »
The problem comes from this. Even the code that loads from my normal DOS COM file (the address is offset 0x100 and some code segment other than 0). The result is an absolute address for the start of the code that's at (CS*16)+0x100 for the start of the real mode code. However when switching to progressive mode, there's no guaranty that it will be where it should be. It can easily end up NOT where it should be. The result is that as soon as you switch to progressive mode, the addresses represented by the labels in the assembly code are useless. So you can't use the labels anymore, and you need to know the exact physical location of the labels, which requires you doing a bunch of address math, unless there's a way to set the ORG in the assembly code in some way to handle this situation.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Why can't I use ORG twice?
« Reply #5 on: February 13, 2023, 02:19:53 AM »
Hi Ben,

"ORG" is origin. It is where your code starts, and it only starts in one place.

I thought progressive was a politician.

I've got a couple problems: 1 my memory"s shot. 2 all my "good stuff" is on IDE drives and my accursed new computer won't take an IDE drive.

As such, I don't have a GDT to show you...

Code: [Select]
bits 16
org 100h ; ?
; lgdt [mygdt]
; no example :(
mov eax, cr0
or al, 1
mov cr0, eax
jmp 8:pmstart
bits 32
pmstart:
; ...
Untested!

That's the jump I remember used to work for me. (but see problem 1)

Best,
Frank