2

Currently I'm working on an assembly project. For some reason I get the error:

#Error 02: Jump>128.

The code segment is as follows:

morechar:
        .
        .
        .
        cmp dl, 0D
        je prep_for_write ;The error is given here
        .
        .
        ;Approximately 150 lines of code in-between
prep_for_write:
        mov ax, 0
        mov bx, 0
        pop ax
        
        cmp ax, 0
        je print_zero
        jmp write_stack
.
.
.

How do I solve this problem?

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • 2
    Select a cpu model that supports longer jumps. Alternatively reverse the condition and use a `jmp`. – Jester May 06 '22 at 14:23
  • I assume you are using A86? – Michael Petch May 06 '22 at 14:40
  • @MichaelPetch yep it's A86 – Emre Türker May 06 '22 at 14:58
  • @Jester what do you mean by reversing the condition, can you elaborate on that please – Emre Türker May 06 '22 at 15:00
  • 3
    Instead of `je foo` you can do `jne skip jmp foo skip:`, and similarly for any other condition. – Michael May 06 '22 at 15:13
  • 2
    The idea is to use a conditional branch to go somewhere closer. You can move the target code closer. Or, because unconditional branches have longer reach, you can conditionally branch to an unconditional branch that goes where you want, or conditionally branch over an unconditional branch as Jester & Michael are suggesting. In the case of the latter you invert the condition b/c you're effectively telling it when to stay in the same sequence vs. telling it when to leave. – Erik Eidt May 06 '22 at 16:59

1 Answers1

1

Well, for those of you who don't want fancy solutions: You can simply create a dummy label which only contains a jmp statement. Just like:

source:
     .
     .
     je dummy_label
     .
     .
dummy_label:
     jmp target
     .
     .
     .

target:
     .
     .
  • I would not call [Michael's suggestion](https://stackoverflow.com/questions/72142971/how-to-solve-error-02-jump128-assembly#comment127469678_72142971) a *fancy solution*. Your solution still has to limit the amount of bytes between `je dummy_label` and `dummy_label:` (just like it was in the original question!) – Sep Roland May 08 '22 at 09:18
  • @SepRoland: This is actually more efficient if not-taken is the normal case, though. The purely-local `jncc` over a `jmp` trick means you have a taken branch every time, but this trampoline strategy makes the not-taken case still just a single not-taken `jcc`, same code size in the fast path and no disruption to code-fetch. And you can stick the trampoline nearby anywhere unreachable. Of course, it obviously can't actually be right before `target:` or it'd be a nop, so I guess this answer is missing a `...` after the `jmp target` to show that's where most of the distance is. – Peter Cordes May 08 '22 at 10:36
  • @PeterCodes yup you are right. I actually added that "..." in my mind but guess I forgot to add it to the solution, so I editted my answer. – Emre Türker May 09 '22 at 04:12