我正在尝试将字符串转换为十进制,然后将所有数字加至该十进制(例如,如果数字为5,则需要加1 + 2 + 3 + 4 + 5),然后在打印该数字后将其转换回十进制。
我正在尝试以下步骤,代码尚不完整,直到现在我只是将字符串转换为十进制并检查是否已转换,否则会出现错误
title fill in title
;------------------------------------------------------------------------------
stacksg segment para stack 'Stack'
db 32 dup(0)
stacksg ends
;---------------------------------------------------------------------------------------------
datasg segment para 'Data'
paralst label byte
maxlen DB 20
actlen DB ?
indata DB 20 dup ('$')
strout DB "Enter a number between 1 and 20: $"
errors DB "USER ERROR$"
sum DB "The sum is: $"
datasg ends
;-----------------------------------------------------------------------------------
codesg segment para 'Code'
main proc far
assume ss:stacksg, ds:datasg, cs:codesg
mov ax, datasg ;initialize data segment registers
mov ds, ax
mov es, ax
MOV AH, 06H
MOV AL, 0
MOV CX, 0 ; ch-row, cl-columns
MOV DH, 79
MOV DL, 79
MOV BH, 01000000B
int 10H
Mov AH, 02H
MOV BH, 00H
MOV DH, 12 ; or mov dx 0c28h
MOV DL, 25
INT 10H
MOV AH, 09H
LEA DX, STROUT
INT 21H
Mov AH, 0AH
LEA DX, paralst
int 21H
;converting string to decimal
CALL READ_NUM
READ_NUM Proc Near
push BX
push CX
push DX
LEA BX, indata
Mov AX, 0
Mov CX, 0
Mov DL, 10
again:
MOV CL, [BX]
CMP CL, 0DH
JE THEEND
SUB CX, 30H
MUL DL
ADD AX, CX
INC BX
THEEND:
POP DX
POP CX
POP BX ; conversion complete
CMP AX, 20 ; since converted decimal is in AX,
; i am checking if it is over 20 or below 1....
; there is where i get error.
JG ERRS
CMP AX, 1
JL ERRS
mov ax, 4c00h
int 21h
MOV DX, 0
MOV CX, 1
TOPPY:
CMP DX, AX
JG ENDDY
ADD CX, DX
INC DX
JMP TOPPY
ERRS:
Mov AH, 02H
MOV BH, 00H
MOV DH, 15 ; or mov dx 0c28h
MOV DL, 25
INT 10H
MOV AH, 09H
LEA DX, errors
INT 21H
mov ax, 4c00h
int 21h
ENDDY:
Mov AH, 02H
MOV BH, 00H
MOV DH, 16 ; or mov dx 0c28h
MOV DL, 25
INT 10H
MOV AH, 09H
LEA DX, sum
INT 21H
mov ax, 4c00h
int 21h
Ret
READ_NUM endp
main endp ;end of procedure
codesg ends ;end of code segment
end main ;end of program
答案 0 :(得分:0)
LEA BX, indata Mov AX, 0 Mov CX, 0 Mov DL, 10 again: MOV CL, [BX] CMP CL, 0DH JE THEEND SUB CX, 30H MUL DL ADD AX, CX INC BX THEEND:
call
已执行),因此您必须使用ret
指令从中返回。 READ_NUM endp
太低了。 第2点和第3点是否来自您的调试工作?
READ_NUM Proc Near
push bx
push cx
lea bx, indata
mov al, 0 ;Clear result
again:
mov cl, 10
mul cl ;AX = AL * CL
mov cl, [bx]
cmp cl, 13
je THEEND
sub cl, '0'
add al, cl ;Add in new digit
adc ah, 0
inc bx
jmp again <<<< you missed this one!!
THEEND:
pop cx
pop bx ;Conversion complete
ret <<<< you missed this one!
READ_NUM endp
请将整个 READ_NUM 进程移至DOS下面。终止该代码,以免您的代码偶然掉入其中:
mov ax, 4C00h
int 21h
READ_NUM Proc Near
push bx
paralst label byte maxlen DB 20 actlen DB ? indata DB 20 dup ('$')
由于您要求输入1到20之间的数字,因此可以选择将此缓冲区限制为2个字符。这样最多可以输入99个。使用字节大小的乘法,您的算法无论如何只能支持0到255之间的数字。
paralst label byte
maxlen DB 3 ;2 characters and 1 carriage return
actlen DB ?
indata DB 3 dup (0)
CMP AX, 20 ; since converted decimal is in AX, ; i am checking if it is over 20 or below 1.... ; there is where i get error. JG ERRS CMP AX, 1 JL ERRS mov ax, 4c00h int 21h
即使没有必要的jmp again
,该调试附加功能也可以很好地与一位数字输入配合使用。
MOV DX, 0 MOV CX, 1 TOPPY: CMP DX, AX JG ENDDY ADD CX, DX INC DX JMP TOPPY
计算直到输入的数字之和是错误的。数字1被加两次!
看到下面,如果您从上到下开始添加,则简单得多:
;AX=5 (input)
xor cx, cx
more:
add cx, ax ;+5 +4 +3 +2 +1
dec ax
jnz more
;CX=15
要打印此两位数的结果,您可以阅读this。