call subroutines conditionally in assembly

后端 未结 3 1412
面向向阳花
面向向阳花 2020-12-18 01:13

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 addr

3条回答
  •  不思量自难忘°
    2020-12-18 01:54

    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.

提交回复
热议问题