Implementing a flow “(1) if {…} else if {…} … (2)” in Assembly

前端 未结 2 988
北荒
北荒 2021-01-27 15:18

I have the following flow in C:

// some stuff1
//................


if (something1) {
    func1();
    func2();
} else if (something2) {
    func3();
    func4()         


        
2条回答
  •  遇见更好的自我
    2021-01-27 15:33

    I would say that it largely depends on how much code you have in these {...} blocks.
    If there's limited code in them use:

        cmp  [some_struc], SOME_CONST
        jne  Else
        {...}
        jmp  EndIf
    Else:
        cmp  [some_struc], SOME_CONST2
        jne  EndIf
        {...}
    EndIf:
        cmp  rax, 0
    

    If there's more code:

        cmp  [some_struc], SOME_CONST
        jne  Else
        call Part1
        jmp  EndIf
    Else:
        cmp  [some_struc], SOME_CONST2
        jne  EndIf
        call Part2
    EndIf:
        cmp  rax, 0
    
    Part1:
        {...}
        ret
    Part2:
        {...}
        ret
    

    Best use call. I would not advice to jump to Part1 or Part2 and then jump back to EndIf.
    This creates spaghetti code. Less readable and quickly becomes less maintainable.

提交回复
热议问题