DATA SEGMENT
x dd 0fffffffh
xl equ word ptr x
xh equ word ptr x+2
count dw 0
DATA ENDS
STACK SEGMENT STACK
DW 100H DUP(?)
STACK ENDS
CODE SEGMENT
assume cs:code ds:data ss:stack
START: mov ax,data
mov ds,ax ;data到ax
mov dx,xl
mov bx,8000h
mov cx,16
XLL: call COUNT0 ;调用COUNT@
loop XLL
mov dx,xh
mov bx,8000h
mov cx,16
XHH: call COUNT0
loop XHH
mov ax,count
mov bl,10
div bl
add al,'0' ;商在al
add ah,'0' ;余数ah
push ax
mov dl,al
mov ah,2
int 21h
pop ax
mov dl,ah
mov ah,2
int 21h
mov ax,4c00h ;返回dos
int 21h
COUNT0 PROC NEAR ;统计1的个数
mov ax,dx
or ax,bx ;将其中一位,置1
cmp ax,dx ;和原来比较
jz done
k: shr bx,1 ;右移一位
RET
done: inc count ;相等,则count+1
jmp k
COUNT0 ENDP
CODE ENDS
END START