pushl %ebp
movl %esp, %ebp
subl $36, %esp //allocate 36 btyes for local vars
movl 8(%ebp), %eax // eax = n
andl $1, %eax //how can u andl a parameter? parameter can be greater than 1
testl %eax, %eax
jmp .L4
cmpl $2, 8(%ebp)
jne .L6
.L4:
movl 8(%ebp), %eax //eax = n
movl %eax, -28(%ebp) //x = eax
movl $1431655766, -32(%ebp) //y = 1431655766
movl -32(%ebp), %eax //eax = y
imull -28(%ebp) //edx = x * eax
movl %edx, %ecx //ecx = edx
movl -28(%ebp), %eax //eax = x
sarl $31, %eax //eax = eax >> 31
movl %ecx, %edx //edx = ecx
subl %eax, %edx //edx = edx - eax
movl %edx, -24(%ebp) //z = edx
movl -24(%ebp), %eax //eax = z
addl %eax, %eax //eax = eax+eax
addl -24(%ebp), %eax //eax = z+eax
movl -28(%ebp), %ecx //ecx = x
subl %eax, %ecx //ecx = ecx-eax
movl %ecx, -24(%ebp) //z = ecx
cmpl $0, -24(%ebp) //compare z and 0
jne .L7 //if not equal jmp to .L7
cmpl $3, 8(%ebp) //compare n and 3
jne .L6 //if not equal jmp .L6
好吧,我有这个程序集片段,我想知道你怎么能和andl $ 1和一个参数,参数可以大于1.此外,testl似乎没用,因为jmp无论如何跳转。有什么想法吗?感谢。
编辑:我已更新代码,不确定是否正确推理所有代码。对imull -28(%ebp)
答案 0 :(得分:0)
AND
可以立即使用8位或32位,因此没有什么能阻止你使用ANDing eax和0x1
TEST
设置标志供以后使用,您不必立即使用它。你没有在L4提供代码,但可能会有一些指令检查
编辑:由于上面是一个未经优化的构建,不要期望它有效。
l
的 IMUL
将该参数与EAX相乘。如果要处理汇编,则需要阅读x86指令。
答案 1 :(得分:0)
$1
一个常量(数字)的简短形式,不带前导零。在32位环境(%eax!)中,它相当于0x00000001
,意思是:将除最右边的所有位设置为零,让最右边的位保持不变。这里它可能用于隔离布尔值(0 =假或1 =真)或确定奇数(0 =偶数,1 =奇数)。