汇编高手们请帮下忙

来源:百度知道 编辑:UC知道 时间:2024/06/30 13:56:39
一道很简单的题 但我初学 不会做 请帮忙看看

题目很简单 要求用户输入一个decimal 数字 然后程序给出 如果把用户输入的decimal转换成binary 那么那个binary里面会有几个1

假设用户输入的数字都是有效数字(只是数字) 假设用户输入的数字不会超过1byte

如果程序运行应该是这样的(例子):
Enter a number: 5
The number of 1s in the binary representation is: 2
Enter a number: 0
The number of 1s in the binary representation is: 0

用的软件是Microsoft Assembler (MASM32) 10.0
谢谢了

已经在Debug中验证通过。
Begin:
push cs
pop ds
mov dx,Offset Messeg1 ; 0Dh 0Ah 'Enter a number: $'
mov ah,9
int 21h
mov ah,1
int 21h
cmp al,1bh ;ESC 退出
jnz 1st
mov ah,4ch
int 21h
1st: and al,0f
mov bl,al
xor si,si
mov cx,4
2nd: test bl,1
jz 3rd
inc si
3rd: shr bl,1
loop 2nd
mov dx,Offset Messeg2 ; 0Dh 0Ah 'The number of 1s in the binary representation is: $'
mov ah,9
int 21h
mov dx,si
add dx,30h
mov ah,2
int 21h
jmp Begin

不好意思,我学的都忘光了。但思路应该是循环除2取余,然后断余数是否为1,是1的话用一个寄存器做累加,为0,直接进入下一个循环