flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > FASM 1.67.5, ORG directive & jumps, possibly a bug.

Author
Thread Post new topic Reply to topic
Serke



Joined: 26 Aug 2006
Posts: 3
Location: Russia
Serke
I’ve written some code that had to be inserted in some specific executable file. I used ORG directive to give FASM a clue about where in memory my code should appear. I tried to compile the code with FASM 1.67.5. No luck. I won’t put there my code because it’s too big and boring, but here’s an example that should give you an idea about the nature of the problem:

Code:
; Binary file

ORG  401000h

 jmp  NewReadDataDrive

; here lies nothing

NewReadDataDrive:    



FASM 1.67.5 refuses to compile the code, giving the following error message: “Error: value out of range.”
FASM 1.65.12 compiles the code just fine.

Any ideas?
Post 26 Aug 2006, 01:03
View user's profile Send private message Reply with quote
UCM



Joined: 25 Feb 2005
Posts: 285
Location: Canada
UCM
It is because FASM defaults to 16-bit mode and "org 401000h" is out of range for 16-bit. You should add "use32" at the beginning of the source.
Post 26 Aug 2006, 01:30
View user's profile Send private message Reply with quote
Serke



Joined: 26 Aug 2006
Posts: 3
Location: Russia
Serke
I'm an idiot. Thanks a lot, UCM.
Post 26 Aug 2006, 01:39
View user's profile Send private message Reply with quote
Alphonso



Joined: 16 Jan 2007
Posts: 294
Alphonso
UCM wrote:
It is because FASM defaults to 16-bit mode and "org 401000h" is out of range for 16-bit. You should add "use32" at the beginning of the source.


I would have to agree with Serke's original assumption that this is indeed a bug. The jump is a 'relative' one and it should not matter what the ORG address is.

Sorry for the late reply Rolling Eyes but I have this same problem now since I want to mix 32bit and 16bit code, seems all the jumps in the 16bit code have to be encapsulated with use32 / use16, looks messy.
Post 07 Feb 2008, 14:35
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7797
Location: Kraków, Poland
Tomasz Grysztar
Read about the jumps assembly handling applied since the 1.65.20 release here: http://board.flatassembler.net/topic.php?t=5162

Using "use32"/"use16" switching for a single instructions is a very bad thing to do, those directives are not for such purpose. You should simply do "jmp dword ...".
Post 07 Feb 2008, 14:44
View user's profile Send private message Visit poster's website Reply with quote
Alphonso



Joined: 16 Jan 2007
Posts: 294
Alphonso
Okay, I see that works but, now I have a jump with an operand override prefix where one is not required.
ie 66 EB 08 (jmp fwd 8 bytes) instead of plain EB 08 (jmp fwd 8 bytes) Confused

Also noticed jmp r32 is not assembled correctly under 16bit mode, ie jmp ecx '66 FF E1' assembles as jmp cx 'FF E1', this time the '66' prefix is omitted. Using 1.67.23
Post 07 Feb 2008, 15:46
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7797
Location: Kraków, Poland
Tomasz Grysztar
Alphonso wrote:
Okay, I see that works but, now I have a jump with an operand override prefix where one is not required.
ie 66 EB 08 (jmp fwd 8 bytes) instead of plain EB 08 (jmp fwd 8 bytes) Confused

If the jump is to address above 0FFFFh, the prefix is required, otherwise the instruction would clear the upper 16 bits of EIP, and thus would not really jump 8 bytes forward, but many bytes backward instead.
Post 07 Feb 2008, 16:06
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Quote:

Also noticed jmp r32 is not assembled correctly under 16bit mode, ie jmp ecx '66 FF E1' assembles as jmp cx 'FF E1', this time the '66' prefix is omitted. Using 1.67.23

You're right, even with "jmp dword ecx" stills assembles as "jmp cx"
Post 07 Feb 2008, 16:12
View user's profile Send private message Reply with quote
Alphonso



Joined: 16 Jan 2007
Posts: 294
Alphonso
Tomasz Grysztar wrote:
If the jump is to address above 0FFFFh, the prefix is required, otherwise the instruction would clear the upper 16 bits of EIP, and thus would not really jump 8 bytes forward, but many bytes backward instead.

Appreciate that, so it's designed that way. Would have liked an option to use just the 'normal' jump though, such as recommending to use JMP dword LABEL? but if you want just the plain jump use JMP LABEL?. However, it would seem to maybe create more problems than solve.

Idea I can use JMP short LABEL? - $$ Smile or even just JMP LABEL? - $$
EDIT: 13-Feb Appears to only work if Origin is aligned on a 64k boundary and jmp doesn't go out of that segment.
i.e.
Code:
org 8000000h ;Ok
org 8010000h ;Ok
org 8000100h ;Not Ok    

For your info, I was playing with an ELF file and trying to make it run as an ELF 32bit under Linux and as a 16bit under DOS just to see if it was possible. Under DOS it would run as a .COM file so 8/16bit jumps are fine. Perhaps mixing the two (Linux / DOS) is not such a good idea anyway. Rolling Eyes

P.S. You've probably heard it a thousand or more times before but, thank you for what IMHO is a great assembler. Cool[/code]


Last edited by Alphonso on 13 Feb 2008, 16:25; edited 1 time in total
Post 08 Feb 2008, 16:39
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
http://www.deater.net/weave/vmwprod/asm/

Quote:

dual is a quick hack that creates a Hello World binary that can run, withouth modification, on both Linux and MS-DOS/Windows systems. That is, it is both an ELF and DOS COM executable at the same time. get the source or the 150byte binary.


There's also a FASM thread of similar dual-nature programs here.
Post 13 Feb 2008, 04:10
View user's profile Send private message Visit poster's website Reply with quote
Alphonso



Joined: 16 Jan 2007
Posts: 294
Alphonso
Thanks rugxulo,
I had already written something but unless AX can be guaranteed to be initialised '0', then there is a very small possibility the program could not work. If the COM version gets past the AX fixup it should be OK but can't guarantee this 100%. Seem to recall reading DOS versions below 3 did not do much in regards to initialising registers.

Had a look at 'dual', thanks for the link, it appears to rely solely on the flags being set to take the jump, in FreeDOS and PTSDOS, it will probably run into problems as the flags, at least when I tested, are set not to jump.

If your interested in the code I could post it to Linux or DOS, probably Linux is better. Confused
Post 13 Feb 2008, 05:59
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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.