首页 > 解决方案 > 条件 if/else 语法

问题描述

从上一个关于将 n 最低有效位设置为 1的问题以及从中得到很好的答案,我编写了一个函数来查找正整数是否为 2 的幂:

# Is 16 a multiple of 8 (2^3)? Yes
mov $16, %edi
mov $3, %esi
call pow_2_short

pow_2_short:
    mov $1, %eax
    mov %sil, %cl # move low byte of %esi (the power of 2) into %cl for the shift
    shl %cl, %eax
    dec %rax
    test %eax, %edi
    jz _set_true; jnz _set_false
  _set_false:
    xor %eax, %eax
    ret
  _set_true:
    mov $1, %eax
    ret

我对此的主要问题是条件部分,这对我来说似乎有点奇怪。上述模式是一种基本的常用方法:

if x:
    // do this
else:
    // something else

如果不是,那么有什么更惯用的方法呢?

标签: assemblyx86x86-64

解决方案


推荐阅读