cmp后汇编x86调用

时间:2018-03-02 20:24:41

标签: assembly x86

我有以下代码来比较数字并增加变量,如果为true或false

failcheck1:
            mov eax, val1
            add ebx, 4
            cmp eax, 40
            jl failinc1

            mov eax, passes
            inc eax
            mov passes, eax
            jmp failcheck2

failinc1:
            mov eax, fails
            inc eax
            mov fails, eax


failcheck2 :
            mov eax, val2
            add ebx, 4
            cmp eax, 40
            jl failinc2

            mov eax, passes
            inc eax
            mov passes, eax
            jmp failcheck3

failinc2:
            mov eax, fails
            inc eax
            mov fails, eax

我有8张这样的支票。我想知道我是否用 ret 创建了一个单一的failinc和passinc子例程,如何在 cmp 之后调用它?

2 个答案:

答案 0 :(得分:2)

由于EBX是独立递增的,因此您可以通过添加add ebx, 4替换8 add ebx, 4*8

通过使用条件集指令setl(设置为较少),可以避免所有这些丑陋且耗时的条件跳转。

因为失败传递是互斥的,所以您可以推迟计算传递到最后。例如如果处理8个值给3 失败那么不可避免地必须有5个传递(8减3)。

 xor  ecx, ecx    ;Clear because SETL only operates on a byte
 xor  edx, edx    ;Clear temporary counter of fails

 cmp  val1, 40
 setl cl
 add  edx, ecx    ;Conditionally increment temporary counter
 cmp  val2, 40
 setl cl
 add  edx, ecx    ;Conditionally increment temporary counter
 cmp  val3, 40
 setl cl
 add  edx, ecx    ;Conditionally increment temporary counter
 cmp  val4, 40
 setl cl
 add  edx, ecx    ;Conditionally increment temporary counter
 cmp  val5, 40
 setl cl
 add  edx, ecx    ;Conditionally increment temporary counter
 cmp  val6, 40
 setl cl
 add  edx, ecx    ;Conditionally increment temporary counter
 cmp  val7, 40
 setl cl
 add  edx, ecx    ;Conditionally increment temporary counter
 cmp  val8, 40
 setl cl
 add  edx, ecx    ;Conditionally increment temporary counter

 add  fails, edx  ;Add temporary counter to actual variable
 sub  edx, 8
 sub  passes, edx ;Add complementary count to other variable (*)

 add  ebx, 4*8
  

我想知道我是否使用 ret 创建了一个单一的failinc和passinc子例程如何在 cmp 之后调用它?

现在是子程序

AddTempCount:     ;On input EFLAGS is set from CMP instruction
 setl cl
 add  edx, ecx    ;Conditionally increment temporary counter
 ret

可以稍微提高可读性。

 xor  ecx, ecx    ;Clear because SETL only operates on a byte
 xor  edx, edx    ;Clear temporary counter of fails

 cmp  val1, 40
 call AddTempCount
 cmp  val2, 40
 call AddTempCount
 cmp  val3, 40
 call AddTempCount
 cmp  val4, 40
 call AddTempCount
 cmp  val5, 40
 call AddTempCount
 cmp  val6, 40
 call AddTempCount
 cmp  val7, 40
 call AddTempCount
 cmp  val8, 40
 call AddTempCount

 add  fails, edx  ;Add temporary counter to actual variable
 sub  edx, 8
 sub  passes, edx ;Add complementary count to other variable (*)

 add  ebx, 4*8

(*)减去负数与添加正数相同。

答案 1 :(得分:0)

failcheck:
    mov eax, [eax]
    add ebx, 4
    cmp eax, ecx
    jl .L1
    incl passes
    ret
.L1:
    incl fails
    ret

failcheck1:
    lea eax, val1
    mov ecx, 40
    call failcheck

    lea eax, val2
    mov ecx, 40
    call failcheck