I am implementing a rotate function in AT&T 64 bit assembly and am having trouble getting the correct output in my code.
The function I'm trying to implement is
unsigned long rotate(unsigned long val, ul num, ul dir);
Val
is value I wanted rotated, num
is how many times and direction is left or right, 0 being right, 1 being left.
My assembly code:
.global rotate
rotate: #value = %rdi
#num = %rsi
#direction = %rdx
mov %rsi, %r10 #puts num into register
test %rdx, %rdx
jz right #if rdx is 0 jump to rotate right
#else go to loop right below which rotates left
loop:
test %r10, %r10 #if num is 0 I am done
jz done
rol $1, %rdi #rotate left 1 bit
dec %r10 #decrement my count
jmp loop #jump to top of loop
right: #same logic as left
test %r10, %10
jz done
rol $1, %rdi
dec %r10
jmp loop
done:
mov %rdi, %rax
ret
My C code:
#include <stdio.h>
extern unsigned long rotate(unsigned long val, unsigned long num, unsigned long direction);
int main()
{
unsigned long v,n,d;
v = 0xDEADBEEFDEADBEEF;
n = 2;
d = 1;
printf("%x\n ", rotate(v,n,d));
}
When I compile and run, I get the value 0x7AB6FBBF
, when I am supposed to get 0x7AB6FBBF7AB6FBBF
.
Is there something wrong with my instructions not sending in unsigned long
s or somethings?