2014-10-05 41 views
1

我是编程装配新手。现在我试图编写一个将数字从十进制转换为二进制的程序。但是我在尝试输入时遇到了一个程序。在输出msg2并进入循环之后,程序不会关闭。我可以输入很多数字,程序不会关闭。我想问题在于convertnumber:cmp si,cx(si是我需要输入多少个数字,cx - 我已经写了多少个数字),但我不确定。我在哪里犯了一个错误,我怎么能纠正它?装配编程错误

.MODEL small 
.Stack 100h 

.DATA 
msg0 db 'how many numbers will include your input number(example. 123 is 3 numbers)? $' 
msg1 db 'Now input number from 0 to 65535: $' 
number db 255, ?, 256 dup ('$') 
numberinAscii db 255, ?, 256 dup ('$') 
enterbutton db 13,10,'$' 


.CODE 
start: 
mov ax, @data 
mov ds,ax 
mov ah,09h 
mov dx, offset msg0 ; first message output 
int 21h 
xor ah,ah ; function 00h of 

int 16h ; int 16h gets a character (ASCII translation in AL) 
int 3 
mov bl,al 
mov dl,al 
mov ah,02h ; function 02h - display character 
int 21h ; call DOS service 

mov ah,09h 
mov dx, offset enterbutton 
int 21h 

mov ah, 09h 
mov dx, offset msg1 ; output second message 
int 21h 
jmp covertHowMany ; converting number that we entered 

next: 
xor si,si 
mov si, ax ; number that we entered now is in si 
xor cx,cx 
mov cx,0 ;cx=0 

enterfirstnumber: ;entering first number (example 123, first number is 1) 
xor ah,ah 
int 16h ; int 16h gets a one character 
int 3 
mov bl,al 
mov dl,al 
mov ah,02h ; function 02h - display character 
int 21h ; 
jmp convertnumber ; converting this number 

input: ;converting number from ascii char to ascii integer 
mov ax,bx 
mov dx,10 
mul dx ; ax:=ax*10 
mov bx,ax ; number that I try to convert is in bx now 
xor ah,ah 
int 16h ; int 16h gets a character (ASCII translation in AL) 
int 3 
mov bl,al 
mov dl,al 
mov ah,02h ; function 02h - display character 
int 21h 
jmp convertnumber 

convertHowMany: 
sub al,30h ; convert from ascii character to ascii number 
jmp next 

convertnumber: 
sub al,30h 
add bx,ax 
inc cx 
cmp cx, si 
jne input 
jmp ending 

ending: 
mov ax,04C00h 
int 21h 

end start 
+1

强烈建议您将学习如何使用调试器,让你可以自己发现问题。 – 2014-10-05 15:15:59

回答

1

我看到至少有两个问题与您的代码:

首先,当你达到convertHowMany你认为AL仍然包含用户键入的字符不会的情况下,因为INT 21h/AH=02hINT 21h/AH=09h修改为AL。您必须以某种方式保存和恢复AL的值(例如通过推送和弹出AX)。

第二个问题是如何在循环之前初始化SI。您正在将AX的值转换为SI,这意味着均为ALAHAH在这一点上不是零,因为您刚刚使用了INT 21h/AH=09h
你可以改变序列xor si,si/mov si,ax成类似mov si,ax/and si,0FFh