winuall
winuall

Reputation: 369

Assembly bit shifting

Given a 32 bit decimal number in %ecx and %edx, when trying to shift left and right each copy of the original number using shl and shr then moving the carry (the bit that was just cut) to a 8 bit register ah and al to compare if equal, I wrote:

movl    $32, %esi   #counter
xor     %eax, %eax
.LOOP:
shl     %ecx
setb    %ah
shr     %edx
setb    %al
cmp     %ah, %al
jnz     .np
decl    %esi
jnz     .LOOP

What am I doing wrong?

Upvotes: 1

Views: 3281

Answers (1)

SomeWittyUsername
SomeWittyUsername

Reputation: 18368

You are comparing 2 numbers from opposite sides. Your program continues looping while ecx[i] == edx[32-i]. So what you're actually doing is checking whether ecx contains an inverted value (i.e. with reordered bits) of edx. E.g., it will return true for ecx with value 0x200000 and edx with value 0x400.

Upvotes: 1

Related Questions