問題描述
你好,我有一個問題,我需要從用戶那裡得到輸入,輸入是一個數字和一個數字後的數字,數字可以是雙字 (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)
我正在製作一個程序,我想在其中從用戶那裡逐位獲取一個 10 位數字
(4,294,967,296) 並存儲所有數字並在 EAX 中製作
其中一個數字.
例如:輸入 = 1 2 3 4,EAX = 1234
我嘗試使用 2 位數字開頭,但遇到了一些問題,我不知道如何
從這裡繼續。非常感謝您的幫助,在此先感謝!
.model small
.STACK 100h
.data
num dd ?
ten DB 10
.code
.386
start:
MOV AX, @DATA
MOV DS, AX
MOV AH,1
INT 21H
SUB AL,30H
MOV BH,AL
MOV AH,1
INT 21H
SUB AL,30H
MOV CH,AL
MOV AL,BL
MUL ten
ADD AL,CH
push eax
call printNum
MOV AX, 4c00h
INT 21h
END start
參考解法
方法 1:
MOV AL,BL MUL ten ADD AL,CH
The above would produce the value of your 2‑digit number if you would have used the correct register. You have stored the first digit in BH
, but you're using BL
here!
.386 push eax
You are in emu8086. Forget about using 32‑bit registers. If you want to work with 32‑bit numbers, you'll have to store them in a couple of 16‑bit registers. e.g. DX:AX
where DX
holds the Most Significand Word and AX
holds the Least Significand Word.
To solve your task of building a 10 digit number up to 4GB‑1, next codez will be useful:
Multiplying the dword in DI:SI
by 10
mov ax, 10
mul si ; LSW * 10 ‑> DX:AX
mov si, ax
xchg di, dx
mov ax, 10
mul dx ; MSW * 10 ‑> DX:AX
jc Overflow
add di, ax
jc Overflow
Adding the new digit AL=[0,9]
to the dword in DI:SI
mov ah, ah
add si, ax
adc di, 0
jc Overflow
Looping until the user presses Enter
Input:
mov ah, 01h ; DOS.GetKeyWithEcho
int 21h ; ‑> AL
cmp al, 13 ; Is it <ENTER> ?
je Done
sub al, '0' ; From character to digit
cmp al, 9
ja Invalid
...
jmp Input
Done:
It's important that you decide how you will handle an invalid user input and what you will do for an input that yields a number bigger than 4GB‑1. Many programs have been written that overlook this and then at some point mysteriously fail...
(by daniel evanenko、Sep Roland)