汇编程序:统计一个字的二进制表示中1的个数

 

                                                 统计一个十六位字中1的个数的汇编小程序

思路:利用逻辑左移指令shl的功能: 

                 如 shl ax, 1  将ax中的最高位放在psw中CF中,然后,将其余的位依次左移一位,最后一位用0填充。

                 该字为16位,逻辑左移16次,就可以把该字中的所有位都依次放入了CF,  统计这十六次移位中CF位为1的个数,

                 也就是该字中二进制表示中1的个数, 同时,16次逻辑左移后,该字就被填充为0000h , 可以通过测试其是否为0,作为退出条件。

编码如下:

Code:
  1. ;例 5.2 在addr单元中存放着数y(十六位的字)的地址,试编制一程序把Y中1的的个数存入到单元count中   
  2.   
  3. data segment   
  4.     org 10   
  5.     number_y dw 8877h   
  6.     addr_y dw   ?   
  7.     count db ?   
  8.            
  9. data ends   
  10.   
  11. code segment   
  12.     assume cs:code, ds:data   
  13.        
  14. start:    
  15.     mov ax, data   
  16.     mov ds, ax   
  17.        
  18.     mov bx, offset number_y   
  19.     mov addr_y, bx   
  20.     mov bx, addr_y   
  21.     mov ax, word ptr [bx]   
  22.     mov cl, 0   
  23. next:   
  24.     test ax, 0ffffh   
  25.     jz  exit   
  26.        
  27.     shl ax,1   
  28.     jnc next   
  29.                
  30.     inc cl   
  31.     jmp next   
  32.        
  33. exit:   
  34.     mov count, cl   
  35.        
  36.        
  37.     mov dl,cl   
  38.     add dl, 30h    
  39.     mov ah,02h   
  40.     int 21h   
  41.   
  42.     mov ah, 4ch   
  43.     int 21h    
  44. code ends   
  45.     end start   
  46.       

 

Code:
  1. ;例 5.2 在addr单元中存放着数y(十六位的字)的地址,试编制一程序把Y中1的的个数存入到单元count中   
  2.   
  3. data segment   
  4.     org 10   
  5.     number_y dw 8877h   
  6.     addr_y dw   ?   
  7.     count db ?   
  8.            
  9. data ends   
  10.   
  11. code segment   
  12.     assume cs:code, ds:data   
  13.        
  14. start:    
  15.     mov ax, data   
  16.     mov ds, ax   
  17.        
  18.     mov bx, offset number_y ; move the relatvie address of number_y to bx    
  19.     mov addr_y, bx          ; move  the address in bx to addr_y memory unit   
  20.     mov bx, addr_y          ; move the content of addr_y memory unit to bx   
  21.     mov ax, word ptr [bx]   ; mov the number_y to ax indrectly!   
  22.        
  23.     mov cl, 0               ; init cl 0, for count the 1 in the ax (number_y)   
  24. next:   
  25.     test ax, 0ffffh  ; test ax is zero?   
  26.     jz  exit         ;   if 0 , all bits has been tested, exit it      
  27.        
  28.     shl ax,1         ; logical shift left, the highest bit is putted into CF   
  29.     jnc next         ;      if CF=0, the highest bit is 0 ; continue  
  30.                
  31.     inc cl           ;          if cf=1, the hightest bit is 1, so increase cl   
  32.     jmp next         ;  contine   
  33.        
  34. exit:   
  35.     mov count, cl    ; save the number of 1 in ax to the memory unit count   
  36.        
  37.     ; display the number in the memory unit count , assume  its value little to 10   
  38.     mov dl,cl   
  39.     add dl, 30h    
  40.     mov ah,02h   
  41.     int 21h   
  42.   
  43. ; the exit system call   
  44.     mov ah, 4ch   
  45.     int 21h    
  46. code ends   
  47.     end start   
  48.       

 

  • 7
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值