【问题标题】:Jump and Call on same line in assembly在汇编中的同一行上跳转和调用
【发布时间】:2017-03-14 22:38:00
【问题描述】:

我想知道是否有办法做出有条件的决定并根据该结果调用函数。

例如。我想比较一些东西。如果它们是偶数,我想做函数调用。但是,我编写函数的方式需要调用函数而不是跳转到它。 (基于我的函数处理堆栈的方式)有没有办法做到这一点?如图所示,我已经复制了我的代码,它无法编译。

.endOfForLoop: cmp  dword [ebp - 4], 1 ; compares the boolean to one
je call print_prime ; if it is one then prime needs to be printed
jmp call print_not_prime ; otherwise it is not prime

使用 NASM、x86 32 位汇编、linux、intel

【问题讨论】:

  • 跳跃和呼叫是相互排斥的,两者不会像您显示的那样一起出现。

标签: function assembly x86 call intel


【解决方案1】:

只需跳过函数调用,就好像你要实现一个 if-then-else:

.endOfForLoop:
    cmp dword [ebp-4],1
    jne .not_prime
    call print_prime
    jmp .endif
.not_prime:
    call print_not_prime
.endif:

您也可以使用函数指针和cmov 指令来使您的代码无分支,但我建议不要编写这样的代码,因为它更难理解并且实际上并不快,因为我知道的所有分支预测器都不会尝试预测完全间接跳转。

.endOfForLoop:
    cmp dword [ebp-4],1
    mov eax,print_prime
    mov ebx,print_not_prime
    cmovne eax,ebx
    call eax

【讨论】:

  • @frillybob,如果您在通话后什么都不做,那么您可以直接跳转到通话目的地,该通话的返回将从跳转例程返回给调用者,从而节省您调用-返回对。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多