2015-11-06 68 views
1

我在我的組裝問題code.I希望交換的數字,但是當我添加這些數字加功能沒有工作well.Thanks大會,添加功能

後兩個數字添加用戶輸入的這代碼

.model small 
.stack 100h 
.data 
     msg1 db 'Enter the number1:$' 
     msg2 db 'Enter the number2:$' 
     msg3 db 'After swap numbers are:$' 
     msg4 db 'Sum is:$' 
     num1 db ? 
     num2 db ? 
     sum db ? 
     diff db ? 
.code 
MAIN PROC 
     mov ax,@data 
     mov ds,ax 

     mov ah,09h   ;display first msg 
     mov dx,offset msg1 
     mov ah,01h   ;taking input 
     int 21h 
     mov num1,al 


     mov ah,09h   ;display second msg 
     mov dx,offset msg2 
     int 21h 
     mov ah,01h   ;taking input 
     int 21h 
     mov num2,al 

     mov bl,num1 
     mov cl,num2 
     mov num1,cl 
     mov num2,bl 

     mov ah,09h   ;display third msg 
     mov dx,offset msg3 
     int 21h 
     mov ah,02h 
     mov dl,num1 
     int 21h 
     mov ah,02h 
     mov dl,num2 
     int 21h 

     mov bl,num1 
     add bl,num2 
     mov sum,bl 

     mov ah,09h  ;display fourth msg 
     mov dx,offset msg4 
     int 21h 
     mov ah,02h 
     mov dl,sum 
     int 21h 

     mov ah,4ch 
     int 21h 
MAIN ENDP 

END MAIN 
+0

一些代碼在代碼塊外部結束,多條線連接在一起。刷新頁面,你會看到。我會重新發布,因爲它可能有助於一個答案 –

+1

請定義*「工作不好」*,是從計算機冒出來的煙? – Leeor

+1

你的程序的問題是,當你執行'mov bl,num1'' add bl,num2'和'mov sum,bl'時,你正在添加你輸入的字符的ASCII值。 ASCII'0'是48位十進制,'1'是49位十進制等。在添加這些數字之前,您需要從每個值中減去48位小數,然後添加它們。要打印出總和,您需要將ASCII 48添加到結果中以將其轉換回可打印字符。你也會發現,如果num1和num2的總和大於9,那麼你會遇到另一個問題。總和需要在打印前轉換爲ASCII字符串。 –

回答

1

你的程序輸入兩個1位數字,因此有可能爲總和高達18您的代碼沒有這種可能性的交易,但它可能是這是故意的。

當你輸入你希望接收到的範圍爲48到57的ASCII字符(它們代表數字0到9)。您變量NUM1NUM2你應該減去48

mov ah, 09h   ;display first msg 
mov dx, offset msg1 
mov ah, 01h   ;taking input 
int 21h 
sub al, 48 
mov num1, al 
mov ah, 09h   ;display second msg 
mov dx, offset msg2 
int 21h 
mov ah, 01h   ;taking input 
int 21h 
sub al, 48 
mov num2, al 

這樣你總和已經擺脫了這些值的特徵性質分配這些值之前以後將是真正的總和這兩個數字。

當準備輸出任何結果時,您必須將值轉換爲其文本表示形式。只需添加48.

mov ah, 09h   ;display third msg 
mov dx, offset msg3 
int 21h 
mov ah, 02h 
mov dl, num1 
add dl, 48 
int 21h 
mov ah, 02h 
mov dl, num2 
add dl, 48 
int 21h 

mov ah, 09h   ;display fourth msg 
mov dx, offset msg4 
int 21h 
mov ah, 02h 
mov dl, sum 
add dl, 48 
int 21h 
+0

@hamxarajput如果這個答案讓你感到高興,那麼你可以接受它。 –