// assembly
.globl _sqrt2
_sqrt2:
movl $0, %eax # place your code for Q3 here
movq $16, %rcx
movq $0x10000, %rdx
loop:
cmpq $0, %rcx
jle end
xorl %eax, %edx
movl %eax, %esi
imull %esi, %esi
cmpq %rdi, %rsi
jle continue
xor %eax, %edx
continue:
dec %rcx
shrq $1, %rdx
jmp loop
end:
ret
//main.c
#include <stdio.h>
unsigned int sqrt2(unsigned int);
void main () {
unsigned int i;
for (i = 0; i < 30; i++) {
printf("The integer square root of %u is %u.\n", i, sqrt2(i));
}
return;
}
结果:
The integer square root of 4 is 0.
The integer square root of 5 is 0.
The integer square root of 6 is 0.
The integer square root of 7 is 0.
The integer square root of 8 is 0.
The integer square root of 9 is 0.
我正在尝试将此算法转换为程序集,但我认为位切换部分不起作用。 xorl %eax, %edx
应该将第k位设置为1,但不起作用。
movq $0x10000, %rdx
更改为movq $0x8000, %rdx
。