鉴于您没有为 asm 指定特定的编译器,我将向您发布一个 asm51 编译器的示例,用于 intel uC 8051:
org 00H ;start directive
VAL equ R2 ;assuming that value of interest is in R2 register
COUNTER equ R3
mov COUNTER,#8D ;initialise the counter
mov A,VAL ;store in the accumulator
;the value you want to check if is power of two
LOOPi: jb Acc,7,NEXT ;if bit 7 of value is 1 jump to NEXT tag
LOOPj: rl A ;rotate bit 7 of acumulator to left
djnz COUNTER,LOOPi ;decrease COUNTER and check another bit if counter != 0
jmp FINISH ;jump to finish if counter == 0
NEXT: inc COUNTER
jmp LOOPj
FINISH:cjne COUNTER,#1D,SOME ;if counter == 1 then value is power of 2
;;;;;;here you do whatever you have to do if is power of two
SOME:
;;;;;;here you do whatever you have to do if it is not power of two
基本上我在这里做的是检查我的 8 位中只有一个 1 的情况,如果是这样的话,我有两个的幂:
记住:
0000 0001 = 1 = power(2,0)
0000 0010 = 2 = power(2,1)
0000 0100 = 4 = power(2,2)
0000 1000 = 8 = power(2,3)
0001 0000 = 16 = power(2,4)
0010 0010 = 32 = power(2,5)
0100 0010 = 64 = power(2,6)
1000 0000 = 128 = power(2,7)
您可能已经注意到,此示例仅限于 2 的 7 次方,但对于其他体系结构而言,由于寄存器的大小不同,在这种特殊情况下,8051 寄存器只有 1 个字节。希望能帮助到你。干杯