汇编中的复杂IF语句

14 x86 assembly conditional-statements

我应该如何if在汇编中写下这样的陈述?

if ((a == b AND a > c) OR c == b) { ...
Run Code Online (Sandbox Code Playgroud)

平台:Intel 32位机器,NASM语法.

更新

对于变量类型和值,请使用更容易理解的内容.我想,整数对我来说会很好.

pax*_*blo 20

在通用程序a集中,它基本上是这样的(in ax,bin bx,cin cx):

    cmp  bx, cx
    jeq  istrue
    cmp  ax, cx
    jle  isfalse
    cmp  ax, bx
    jeq  istrue
isfalse:
    ; do false bit
    jmp  nextinstr
istrue:
    ; do true bit

nextinstr:
    ; carry on
Run Code Online (Sandbox Code Playgroud)

如果没有错误位,可以简化为:

    cmp  bx, cx
    jeq  istrue
    cmp  ax, bx
    jne  nextinstr
    cmp  ax, cx
    jle  nextinstr
istrue:
    ; do true bit

nextinstr:
    ; carry on
Run Code Online (Sandbox Code Playgroud)


Fle*_*exo 10

您需要将if语句分解为一系列比较和跳转.就像在C中你可以这样写:

int test = 0;

if (a == b) {
  if (a > c) {
    test = 1;
  }
}

// assuming lazy evaluation of or:
if (!test) {
  if (c == b) {
    test = 1;
  }
}

if (test) {
  // whole condition checked out
}
Run Code Online (Sandbox Code Playgroud)

这将复杂的表达分解为你的asm同样会做的组成部分,尽管你可以通过跳转到仍然相关的部分在asm中更清晰地写出来.

假设a,b和c正在堆栈中传递给你(如果他们没有显然从其他地方加载它们)

        mov     eax, DWORD PTR [ebp+8] 
        cmp     eax, DWORD PTR [ebp+12] ; a == b?
        jne     .SECOND                 ; if it's not then no point trying a > c 
        mov     eax, DWORD PTR [ebp+8]
        cmp     eax, DWORD PTR [ebp+16] ; a > c?
        jg      .BODY                   ; if it is then it's sufficient to pass the
.SECOND:
        mov     eax, DWORD PTR [ebp+16]
        cmp     eax, DWORD PTR [ebp+12] ; second part of condition: c == b?
        jne     .SKIP
.BODY:
        ; .... do stuff here
        jmp     .DONE
.SKIP:
        ; this is your else if you have one
.DONE:
Run Code Online (Sandbox Code Playgroud)