SAL and SAR by 0 errors

Posted by Roy McAvoy on Stack Overflow See other posts from Stack Overflow or by Roy McAvoy
Published on 2010-04-23T17:25:37Z Indexed on 2010/04/23 17:33 UTC
Read the original article Hit count: 408

Filed under:

I have discovered a bug in some assembly code I have been working with but can't figure how to fix it. When shifting left by 0 the result ends up being 0 instead of jut the number. The same applies when shifting to the right. Any and all help is much appreciated.

function sal(n,k:integer):integer;
begin
 asm
 cld
 mov    cx,     k
@1:
  sal n, 1
  loop @1
 end;
 sal:= n;
end;

function sar(n,k:integer):integer;
begin
 asm
  cld
  mov   cx,     k
@1:
  sar   n,      1
  loop  @1
 end;
 sar:=n;
end;

I have tried to changed them in the following way and it still does not work properly.

function sal(n,k:integer):integer;
begin
 asm
 cld
 mov    cx,     k
 jcxz @done
@1:
  sal n, 1
  loop @1
@done:
 end;
 sal:= n;
end;

function sar(n,k:integer):integer;
begin
 asm
  cld
  mov   cx,     k
 jcxz @done
@1:
  sar   n,      1
  loop  @1
@done:
 end;
 sar:=n;
end;

© Stack Overflow or respective owner

Related posts about assembly