0
下面的代碼應該添加兩個一維矩陣並顯示總和。程序獲取第二個矩陣的輸入時出現問題:rd_next循環永遠不會結束。然而,它需要第一個矩陣的輸入很好。爲什麼在爲第二個矩陣進行用戶輸入時,rd_next代碼永遠不會結束?
data_seg segment
mat1 dw 3 dup(?)
mat2 dw 3 dup(?)
n db 3
ten dw 10
counter db ?
string db 10 dup(?)
msg1 db 10,13,"Enter first matrix: ","$"
msg2 db 10,13,"Enter second matrix: ","$"
msg3 db 10,13,"Enter a number: ","$"
data_seg ends
code_seg segment
assume cs:code_seg,ds:data_seg
print_string proc
pop si
pop dx
mov ah,9
int 21h
push si
ret
print_string endp
read_char proc
pop di
mov ah,1
int 21h
mov ah,0
push ax
push di
ret
read_char endp
read_number proc
pop si
mov bx,0
mov dx,0
next_digit:
call read_char
pop ax
cmp al,0Dh
je done
sub al,30h
mov cl,al
mov ch,0
mov ax,bx
mul ten
add ax,cx
mov bx,ax
jmp next_digit
done: push bx
push si
ret
read_number endp
print_number proc
pop si
pop ax
mov bx,0
mov dx,0
repeat1:
mov cx,0
mov dx,0
div ten
push dx
inc counter
cmp ax,0
jne repeat1
print_digit:
pop dx
add dl,30h
mov ah,2
int 21h
dec counter
jnz print_digit
push si
ret
print_number endp
start:
mov ax,data_seg
mov ds,ax
mov al,n
mov counter,al ; initialize counter variable
mov bp,offset mat1 ; initialize pointer to first matrix
push offset msg1 ; prompt user to enter first matrix
call print_string
rd_next:
push offset msg3 ; prompt user for next number in matrix
call print_string
call read_number ; call the read_number procedure.
pop dx
mov [bp],dx
add bp,2
dec counter
jnz rd_next ; loop back to read the next number.
mov counter,al ; reset counter variable
mov bp,offset mat2 ; initialize pointer to second matrix
push offset msg2 ; prompt user to enter second matrix
call print_string
jmp rd_next
mov al,n
mov si,offset mat1
mov di,offset mat2
matrixsum:
mov bx,[si]
mov cx,[di]
add bx,cx
push bx
call print_number
inc si
inc di
dec al
jnz matrixsum
code_seg ends
end start
我沒有看到任何代碼錯誤。我試圖創建一個單獨的rd_next2循環來獲取第二個矩陣的輸入,但它不起作用。