Author Topic: jmp short ...  (Read 6407 times)

nobody

  • Guest
jmp short ...
« on: March 17, 2005, 07:20:15 AM »
Writing this code:

bits 32
    org 0x401182
    jmp short 0x401191

Generates this error:

nasm -f bin -o test.bin test.s
test.s:4: error: short relative jump outside segment
test.s:4: error: short jump is out of range

The fix:

bits 32
    org 0x401182
    jmp near 0x401191

Is not satisfactory, because it generates these bytes:

E9 0A 00 00 00

What I want it to generate is this:

EB 0D

So far, I haven't been able to accomplish this.  Making the jmp plain:
    jmp 0x401191

And using the -O2 switch doesn't work either.

So what am I missing?  Is this even possible?  I've looked for an answer but haven't found one yet...

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2379
  • Country: us
Re: jmp short ...
« Reply #1 on: March 17, 2005, 09:46:30 PM »
I don't know why Nasm doesn't like "short" there - I suspect it's because the raw number doesn't have a "segment" associated with it. Is there some reason why you don't just put a label where you want to jump to? If you know the number of bytes you need to jump, "jmp short $ + 0Dh" will produce the code you want also. Using a label would be the most flexible, but perhaps you're doing something "unusual"... Hmmm...

jmp short 0x401191 + $ - 0x401182

...seems to be acceptable to Nasm (just another way of writing "$ + 0Dh"). Will that work for you? (maybe "%define __ORIGIN__ 0x401182" and use "__ORIGIN__" rather than hard-code the number...) If all else fails, you can write "db"s for the code you want (since you seem to know what code you want), but there ought to be a more "elegant" way. I still think using a label "as usual" is the best bet, unless there's some reason you can't...

Best,
Frank

nobody

  • Guest
Re: jmp short ...
« Reply #2 on: March 19, 2005, 03:53:30 AM »
Admittedly, I am trying to do something unusual.  I dug through the code a bit to find the root cause of the problem, and found that the assembler module was the one doing the range checking, but it was the output module (outbin) that handles the "org" directive.  Hence, the assembler has no way of computing the correct relative offset -- it defers that to the output code.

The "$ + offset" syntax is a good workaround.  I wish I had thought of it.  :)

Thanks for the reply.

nobody

  • Guest
Re: jmp short ...
« Reply #3 on: March 19, 2005, 03:57:38 AM »
Admittedly, I am trying to do something a little unusual.  I looked through the code to find the root cause.  It turns out the assembler module is the one that does the range checking, but its the output module (outbin) that handles the 'org' directive.  Hence, the assembler module has no way of computing the correct offect -- it defers that to the output module.

The "$ + offset" syntax is a good workaround.  I wish I had thought of it.  :)

Thanks for the reply.