• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Delphi+Asm-TBits类的学习

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

技术交流,DH讲解.

在D2010的classes中有个TBits类,这个类主要是位操作的.

  TBits = class
  private
    FSize: Integer;
    FBits: Pointer;
    procedure Error;
    procedure SetSize(Value: Integer);
    procedure SetBit(Index: Integer; Value: Boolean);
    function GetBit(Index: Integer): Boolean;
  public
    destructor Destroy; override;
    function OpenBit: Integer;
    property Bits[Index: Integer]: Boolean read GetBit write SetBit; default;
    property Size: Integer read FSize write SetSize;
  end;

这个类没有什么方法,我们看到了property Bits[Index: Integer]: Boolean read GetBit write SetBit; default;这个属性,就是读取和设置某一位的.
那我们看看它是怎么实现的?

//在类中Eax就是Self指针
procedure TBits.SetBit(Index: Integer; Value: Boolean); assembler;
asm
        CMP     Index,[EAX].FSize  //如果Indx>=Size then 扩容
        JAE     @@Size

@@1:    MOV     EAX,[EAX].FBits
        OR      Value,Value
        JZ      @@2
        BTS     [EAX],Index  //将Eax中第Index位赋值给CF,然后Eax第index位=1;
        RET

@@2:    BTR     [EAX],Index //将Eax中第Index位赋值给CF,然后Eax第index位=0;
        RET

@@Size: CMP     Index,0   //if index <0 then Error
        JL      TBits.Error
        PUSH    Self      //push [eax]
        PUSH    Index
        PUSH    ECX {Value}
        INC     Index
        CALL    TBits.SetSize
        POP     ECX {Value}
        POP     Index
        POP     Self
        JMP     @@1
end;

function TBits.GetBit(Index: Integer): Boolean; assembler;
asm
        CMP     Index,[EAX].FSize
        JAE     TBits.Error
        MOV     EAX,[EAX].FBits
        BT      [EAX],Index   //将eax的第Index位赋值给CF
        SBB     EAX,EAX //eax - (eax + CF)
        AND     EAX,1  //清除Eax中的其他位
end;

这里我们发现BTR,BTS,BT,SBB等位操作符.我们之前不是算过99999中有多少个一么?
当时我们用的方法是移位然后与,那么我们现在换个方法呢?

Function _1Bits(ANum: Integer): Integer;
asm
  xor edx,edx // 位数
  xor ecx,ecx // 1的个数
@@nLoop:
  cmp edx,32  // 循环32次
  je @@nExit
  bt eax,edx
  jnc @@nNoInc // if CF = 0 then
  inc ecx
@@nNoInc:
  inc edx
  jmp @@nLoop
@@nExit:
  mov eax,ecx
end;

全部都是直接对位操作的.
BTR和BTS的作用如上面我注释中写的那样,但是我现在没有其他例子给大家看.TBits类很纯洁,所以可以用到其他版本中去.

我是DH.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap