Author Topic: How do I directly write a relative jump in the source code?  (Read 10091 times)

Offline ben321

  • Full Member
  • **
  • Posts: 185
How do I directly write a relative jump in the source code?
« on: March 21, 2015, 02:03:34 AM »
If my code has
Code: [Select]
jmp 0x12345678
the assembler calculates the distance from the end of that opcode to the absolute address specified, and then writes a relative jump, that goes from the end of that opcode to the intended destination. However I want to be able to directly code a relative jump. I wish it were possible to do something like this
Code: [Select]
jmp relative 0x0100
So that if I already, at design time, know exactly how many bytes I will need to jump to get to a certain destination, I would be able to simply type in that relative offset myself. Unfortunately I can't figure out the correct opcode (or compiler directive that would instruct the compiler to treat the parameter after "jmp" as a relative address, rather than an absolute address). As it stands now, it treats the address 0x0100 as a shortened version of 0x00000100, which is of course an absolute address. It even treats it as the absolute address 0x00000100 such when I SPECIFICALLY let it know to treat it as a 2-byte address with the "word" directive like this "jmp word 0x0100" (despite the fact that a 2 byte address could NEVER be an absolute address, and must be a relative address). Is there any way to tell the compiler to take the address parameter in a specific instance of the "jmp" mnemonic as a relative address, rather than an absolute address? I already tried the above mentioned pseudo code "jmp relative 0x0100" in hopes that by pure chance the "relative" directive actually was what I needed. But it wasn't. So I don't know how to do what I'm trying to do.
« Last Edit: March 21, 2015, 02:05:29 AM by ben321 »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How do I directly write a relative jump in the source code?
« Reply #1 on: March 21, 2015, 07:52:34 AM »
Code: [Select]
db 0xE9
dd 0x100
Make a macro of it if you must. I doubt if you'll find that's really what you want to do. Things are done in the "usual way" for a reason.

There's something to keep in mind about addresses (besides that they're "virtual"). In linkable output formats like "-f win32", Nasm doesn't actually know what the final address will be. That's up to the linker. Nasm presents the linker with a list of offsets into the file that will need to be "patched" to the final place the linker puts it, so your gem can find "hello world". Let that temper your expectations of what Nasm can and can't do for you.

Best,
Frank