proeng
proeng

Reputation: 21

nasm x86, conditional jumps to a dynamic address

Hello I have run into a problem, seems like nasm is getting in my way, but I want to ask if there is a solution I am not aware of in nasm. I'm programming in x86 intel making COM files.

In my program the location of things are not static, so I am using bp to calculate the correct offset of things. For example if I want to access a variable buffer I use lea dx, [bp + buffer]

So in my code I want to use conditional jumps to specific labels in the code, but since the location of the program changes I wanted to do something like this: jc [bp + EXIT], loading into a register does not work either.

Where in this example EXIT would be the part where the program is wrapping up. But I want to jump to many different places in the code not just exit.

So what I am asking is, is there anything in nasm that allows me to do conditional jumps to memory addresses that are dynamic relative to bp? That is they work even if the code location changes in memory later? If not what would be the best solution to get that behavior?

Upvotes: 0

Views: 786

Answers (2)

ecm
ecm

Reputation: 2755

Conditional jumps can only encode a short or (on 386+) near, constant jump destination. To allow you to jump indirectly with a certain condition, you can reverse the condition you want to jump for. This reverse condition can then be used in a jump around another jump instruction. The first jump will effectively let you skip the other jump, if your intended indirect jumping condition is not met. (Only the jcxz conditional cannot directly be inverted.)

Here's your example done this way. I used a local label.

    jnc .skip
        ; here if Carry Flag is set
    jmp near [bp + EXIT]
.skip:

If you prefer, you can keep the original sense of your condition, but use the constant jump destination to relay execution to an indirect jump instruction placed somewhere out of the way.

    jc jmp_bp_exit
        ; other code to continue execution here


jmp_bp_exit:
        ; here if Carry Flag is set
    jmp near [bp + EXIT]

Upvotes: 2

proeng
proeng

Reputation: 21

After some trial and error, I discovered that I could use cmovc instead.

Upvotes: 1

Related Questions