在彙編語言中得到錯誤的結果 (Getting wrong result in Assembly language)


問題描述

在彙編語言中得到錯誤的結果 (Getting wrong result in Assembly language)

   org 100h 
.model small
.data 
 var db ?
 msg db 10,13,'$' 

.code      
; add your code here
main proc
     mov ax,@data
     mov ds,ax

     mov ah,1 ;input 1st number
     int 21h
     sub al,48    
     mov var,al

     mov ah,1   ;input 2nd number
     int 21h
     sub al,48

     MUL var    ; multiplying two numbers

     sub al,48 ; going to ASCII value

     mov dl,al
     mov ah,2    ; printing result
     int 21h

     mov ah,4ch   
     int 21h
main endp
     end main
     ret

參考解法

方法 1:

You incorrectly mix two program models together. For MZ executable DOS program omit the first org 100h and the last ret. Or use the much simpler COM executable, which doesn't use segment‑switching directives .data, .code, and you don;t have to bother with segment registers. Its skeleton looks like

     .model TINY
org 100h
main proc
; Program code must start here at offset 100h
; The first machine instruction.
; Put your code here.

 ret      ; A simple ret terminates the COM program.

var db ? ; Data variables follow the code section.
msg db 10,13,'$'
end main
</code></pre>

When you multiplicate both digits with mul var, the product is in register AX and it may be in the range 0..65535. Only in special case, such as multiplaying 2 by 3 you will get result AX=6, which can be converted to a single‑digit result by adding 48 to it (not by subtracting).

For methods how to convert unsigned 16bit integer to decimal digits search this site, there are lots of examples here.

(by AmirHassanvitsoft)

參考文件

  1. Getting wrong result in Assembly language (CC BY‑SA 2.5/3.0/4.0)

#emu8086 #assembly #x86-16






相關問題

使用 include 'emu8086.inc' 反轉字符串的大小寫和順序 (Reverse case and order of a String using include 'emu8086.inc')

emu8086 : ARR 不包含任何值 (emu8086 : ARR do not contain any value)

為什麼這個彙編代碼不刪除擴展名為“.lnk”的文件? (Why doesn't this assembly code delete the file with ".lnk" extension?)

將 C 代碼轉換為 8086 程序集 (Converting C code into 8086 assembly)

循環無法打印字母 (Loop does not work to print alphabets)

如何使用 call 和 ret 更改堆棧內容? (how to change stack content with call and ret?)

彙編語言中的putch函數通過堆棧 (putch function in assembly language through stack)

如何在不使用 HLT 的情況下對程序進行 HLT (How to HLT the program without using HLT)

使用 8086 彙編計算 10 的階乘 (Computing the factorial of 10 using 8086 assembly)

你好,我有一個問題,我需要從用戶那裡得到輸入,輸入是一個數字和一個數字後的數字,數字可以是雙字 (hello , i got a question that i need to get input from user, the input is a number and digit after digit ,the number can be a doubleWord)

在彙編語言中得到錯誤的結果 (Getting wrong result in Assembly language)

我的氣泡代碼是彙編語言嗎? (is my code for bubble right at assembly language?)







留言討論