程序集编程bug

时间:2014-10-05 13:43:36

标签: loops assembly type-conversion

我是编程组装的新手。现在我正在尝试编写一个将数字从十进制转换为二进制的程序。但是在尝试输入时我遇到了一个程序。输出msg2并进入循环后,程序不会关闭。我可以输入很多数字,程序也不会关闭。我想问题是转换数: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 个答案:

答案 0 :(得分:1)

我发现您的代码至少存在两个问题:

首先,当您到达convertHowMany时,您认为AL仍然包含用户输入的字符。但情况不是这样,因为INT 21h/AH=02h和{{ 3}}修改AL。您必须以某种方式保存和恢复AL的值(例如,通过推送和弹出AX)。

第二个问题是如何在循环之前初始化SI。您将AX的值移至SI,这意味着 ALAH。此时AH不为零,因为您刚刚使用了INT 21h/AH=09h 您可以将序列xor si,si / mov si,ax更改为mov si,ax / and si,0FFh