实现流程“(1) if {...} else if {...} ... (2)"在大会 [英] Implementing a flow "(1) if {...} else if {...} ... (2)" in Assembly

查看:46
本文介绍了实现流程“(1) if {...} else if {...} ... (2)"在大会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C 中有以下流程:

I have the following flow in C:

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


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

// some stuff2

我想知道,我如何在汇编中对其进行编码?我的意思是,不是确切的说明,而是流程.我应该使用标签跳转到里面的内容 if (something1) { ...} 和else if (something2)"吗?我将如何返回到//some stuff2"?

I wonder, how can I encode that in Assembly? I mean, not exact intructions, but the flow. Should I use labels for jumping to what's inside if (something1) { ...} and "else if (something2)"? How would I return then to "// some stuff2"?

  ; some stuff1
  ; and then 

  cmp [some_struc], SOME_CONST
  je .... ????

  cmp [some_struc], SOME_CONST2
  je .... ????


  ; some stuff2
  ; how to better get back here?
  cmp rax, 0 

或者我应该将它们称为函数?那么如果第一个是真的,我将如何跳过第二个else if (something2) {"?

Or should I call them as functions? Then how would I skip the 2nd "else if (something2) {" if the 1st one is true?

我可以以某种方式实施,但我想知道如何更好地做到这一点.

I can implement somehow, but I want to know how to better do that.

推荐答案

我认为这在很大程度上取决于您在这些 {...} 块中有多少代码.
如果其中的代码有限,请使用:

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

如果有更多代码:

    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

最好使用call.我不建议先跳到 Part1Part2,然后再跳回 EndIf.
这将创建意大利面条式代码.可读性较差,而且很快就会变得不易维护.

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.

这篇关于实现流程“(1) if {...} else if {...} ... (2)"在大会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆