9

I'm learning x86 assembly. I was wondering how you perform call a subroutine conditionally. As far as I understand, jumping to a label doesn't work because the return address is not stored and therefore it does not know where to return.

 cmp bx, 0
 jz zero ; how do I do this correctly ?
 ; do something else and exit

 zero:
 ; do something
 ret
Jon
  • 99
  • 1
  • 1
  • 2
  • 2
    I haven't written assembly in a while but I think I remember pushing addresses either on the stack or in a register and jumping to that value at the end of the subroutine. – Manny D Sep 04 '11 at 19:44

4 Answers4

7

The clean way to do it is simply:

    cmp bx,0
    jnz notzero
    ; handle case for zero here
    jmp after_notzero
notzero:
    ; handle case for not zero here
after_notzero:
    ; continue with rest of processing

I know no better way for an if-else situation. Ok, if both branches must return directly afterward, you can do:

    cmp bx,0
    jnz notzero
    ; handle case for zero here
    ret

notzero:
    ; handle case for not zero here
    ret

If some processing must take place before the ret (e.g. popping values previously pushed), you should use the first approach.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • You can do an if/else in asm with no taken branches on one of the paths. The conditional jump goes to a block after the `ret` in the function, so the fast-path doesn't need to jump over it. So the fast-path has one not-taken branch, and the slow-path has two taken branches. (It jumps back to wherever you want.) gcc makes code like this all the time: little blocks of code with one or more insns and then a `jmp` back up into the main part of the function. Good point that conditional tail-calls are possible. [gcc doesn't, but could](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42497) – Peter Cordes Apr 25 '16 at 08:47
6

Well it works if you don't need to return to that address. Often times you can structure your code such that this is the case.

Otherwise you'll have to do the branching with Jxx instructions that jump around the call site or in other ways structure your code around this limitation. In this case inverting the test should work:

    cmp bx, 0
    jnz not_zero
    call zero
    ; fall through here, return or do what you want
not_zero:
    ; do something else and exit
    ; ...
    ret 

zero:
    ; do something
    ret

EDIT 2016-04-25: As @Peter Cordes mentions in the comments, the below code will probably perform terribly. See e.g. this article for an explanation why.

@Manny Ds suggestion in the comments inspired me to write the following. It might not be cleaner or better, but it's another way to structure it:

    push back_from_zero ; Push where we want to return after possibly branching to 'zero' 
    cmp bx, 0
    jz zero ; branch if bx==0
    add esp, 4 ; adjust stack in case we didn't branch
back_from_zero: ; returning from the zero branch here or continuing from above
    ; do something else and exit
    
zero:
    ; do something
    ret

It explicitly pushes the return address on the stack so the zero function can return or pops the value (add esp, 4) from the stack if we don't call the function (to readjust to stack). Note that you need to do some slight adjustments if you want this to work in either 16- or 64-bit mode.

James Martin
  • 140
  • 7
user786653
  • 29,780
  • 4
  • 43
  • 53
  • Unfortunately, I have an `if-else` situation so I don't think I can restructure like that. I can probably have that bit of code in a separate subroutine just for that particular case, but it seems a bit hacky. I was hoping there was a clean way to do it. – Jon Sep 04 '11 at 19:54
  • 5
    Never use the `push back_from_zero` trick if performance matters at all. Modern CPUs keep a return-address prediction stack, and a mis-matched `call`/`ret` breaks that stack, leading to a branch-mispredict on every `ret` for maybe the next 15 levels of returns. Avoid this by having the called function return manually (without `ret`): e.g. pop the return addr and `jmp edx`. Or in a 64bit ABI with a red-zone, you could also adjust the stack and then do a memory-indirect `jmp [rsp - 8]`. That indirect jmp probably won't have a BTB entry, but won't destroy performance for other code after. – Peter Cordes Apr 25 '16 at 08:24
  • It's actually fine *if* `zero` ends with `pop edx` / `jmp edx` or something. But that only works if the function is only ever called without a `call` insn. If there's only one call site, Rudy's answer of how to properly structure the branches to conditionally run something is a much better approach. – Peter Cordes Apr 25 '16 at 16:45
1

I believe the right way to do it is with the call instruction. This is equivalent to a function call in a higher programming language. The PC is stored on the stack, and therefore the ret at the end of your zero: subroutine does what it's supposed to.

dandan78
  • 13,328
  • 13
  • 64
  • 78
-1

if I understand correctly what you are looking for is for example if CMP yields zero then make a CALL instead of JMP. one workaround is using a temp subfunction to call the intended function

example: below program at #4 you do CMP then Jz to (made_up) label, then use that (made_up) label to call the actual function you want.

    #1 instruction xxx,xxx
    #2 instruction xxx,xxx
    #3 instruction xxx,xxx
    #4 CMP XXX , XXX 
    #5 JZ Made_up
    #6 Made_up:
    #7  call your_function
    #8 next instruction address will be pushed to stack as your return address.


    cmp xxx, xxx
    jz Made_up

    Made_UP:
        call your_Function
    ;now you will have the return address saved onto the stack to return to.

hope this helps...

DChiTown
  • 1
  • 2
  • 1
    Execution reaches `Made_up:` whether or not the `jz` is taken. Put the `call` somewhere that won't be reached if the branch is not taken, not like you've shown where it's the next instruction so will be reached by either fall-through or taken cases. Or as shown in other answers, jump forward *over* a `call`, with the opposite branch condition (`jnz`). – Peter Cordes Mar 30 '23 at 05:35