Author Topic: How do I compute the offset for a jump backwards?  (Read 10632 times)

Offline scmaccal

  • Jr. Member
  • *
  • Posts: 5
How do I compute the offset for a jump backwards?
« on: October 02, 2013, 01:49:27 AM »
I am writing a flat binary program. How do I compute the offset for a jump backwards?
Code: [Select]
jmp word 0x0000:0x0017 does the trick now but I know that the offset for this jump will change as I add more lines of code. And yes I am aware of
Code: [Select]
jmp $ but I want to code this by hand.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How do I compute the offset for a jump backwards?
« Reply #1 on: October 02, 2013, 04:31:31 AM »
Count 'em, I suppose. The whole purpose of using a "symbolic" assembler (instead of something like DEBUG) is so you won't have to do that! Note that you've got a far jump in one case and a short jump in the other. 0x0000:0x0017 is an unlikely target to want to jump to (right in the middle of your IVT), but I suppose that's just an example...

Best,
Frank


Offline scmaccal

  • Jr. Member
  • *
  • Posts: 5
Re: How do I compute the offset for a jump backwards?
« Reply #2 on: October 02, 2013, 01:28:59 PM »
Frank,

Thank you for replying. Your mentioning of debug brought back memories. I did a search for a debug tutorial online to see if it described how to count them. I found one: http://www.japheth.de/Debug/DEBUG.TXT and it said,
Quote
Because DEBUG is not a symbolic assembler, you must code all operands that represent memory addresses manually. These memory addresses include data addresses and jump, loop, and call destination addresses. To do this reasonably you must assemble your code twice. In the script file for the first assembly, use a place holder value for each of these operands. Note that the placeholder values must be selected such that all displacements will be within the allowable range. Using the output from the first (error free) assembly, replace the placeholders in the script file with the actual addresses, and do the final assembly.
Is there not a human counting method that can be employed on the source code to know the correct offset? Surely each line of code has a consistent value. At least that is how it appears in the list file I produced.

Kind regards,
Scott

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How do I compute the offset for a jump backwards?
« Reply #3 on: October 02, 2013, 03:56:48 PM »
Hi Scott,

I don't think you'll find that each "line" of code has a consistent length, but each instruction has a consistent length. Memorize those, and you've got it made. There are quite a few of them.

Japheth's suggestion of making a "test" assembly with placeholder values would work too, of course. I don't think there's any magic formula.

Note that "jmp" and a number of other instructions use relative addressing. We write "jmp target" and it will disassemble as "jmp address_of_target", but if you look at the code that actually is emitted it's "jmp distance_to_target" (plus or minus). I don't know which of these numbers you'd want to code by hand.

Best,
Frank