I am very new to x86 and I'm trying to write a program that computes the integer square root of a number, building it bit by bit from the most significant to least significant. The only scratch registers I have are %rax, %rcx, %rdx, %rdi, %rsi, %r8, %r9, %r10 and %r11. I am not allowed to use any others. I am not quite sure how to copy the value from %edi into an r register, and then once all calculations are complete, recopy that value in the r register back into %eax to be returned.
Variables: %edi contains the argument x (unsigned 32-bit). %eax will carry the return value
This is all the code I have, I am sure it is full of mistakes, I am very new to this.
.globl sqrt
sqrt:
movl $0, %eax #initializing return to 0
movslq %edi, %rdi #moving edi into rdi, not sure if this works
movq $0, %rax #initializing scratch return to 0
movq $15, %rcx #initializing loop counter to 15(start at 15th bit)
movq $0x80000, %rbx #creating bit mask (1000 0000 0000 0000)
loop:
xorq %rax, %rcx #set specific bit to 1 in rcx
pushq %rax #temporarily store rax value in stack
mulq %rax #rax=rax*rac
cmpq %rax, %rdi #rax<=rdi
popq %rax #restore original rax value
jbe keep_bit #keep bit if rax<=rdi
xorq %rax, %rcx #unset bit in rax if rdi>rax
keep_bit:
shr $1, %rcx #shift to the next bit
jnz loop #continue to loop until all bits are tried
I know I need a line in here to load %rax value back into %eax to be returned but I'm not sure how to do that