无法将第k位切换为1

问题描述 投票:0回答:1

enter image description here

// 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,但不起作用。

assembly x86-64 att
1个回答
0
投票
没有将第k位设置为1,而是将第k + 1位设置为1。将movq $0x10000, %rdx更改为movq $0x8000, %rdx
© www.soinside.com 2019 - 2024. All rights reserved.