我需要提示用户一个告诉他写一个号码的msg,然后我存储这个号码并对其进行一些操作 在INT 21h搜索后,我发现了这个:
INT 21h / AH=1 - read character from standard input, with echo, result is stored in AL.
if there is no character in the keyboard buffer, the function waits until any key is pressed.
example:
mov ah, 1
int 21h
主要问题是它只读取一个字符并将其表示为ASCII 所以如果我需要写数字“357” 我将其视为3,5,7
这不是我的目标。 任何想法?
答案 0 :(得分:4)
When you managed to get the user input,将其指针放在ESI(ESI =地址到字符串)
中.DATA
myNumber BYTE "12345",0 ;for test purpose I declare a string '12345'
Main Proc
xor ebx,ebx ;EBX = 0
mov esi,offset myNumber ;ESI points to '12345'
loopme:
lodsb ;load the first byte pointed by ESI in al
cmp al,'0' ;check if it's an ascii number [0-9]
jb noascii ;not ascii, exit
cmp al,'9' ;check the if it's an ascii number [0-9]
ja noascii ;not ascii, exit
sub al,30h ;ascii '0' = 30h, ascii '1' = 31h ...etc.
cbw ;byte to word
cwd ;word to dword
push eax
mov eax,ebx ;EBX will contain '12345' in hexadecimal
mov ecx,10
mul ecx ;AX=AX*10
mov ebx,eax
pop eax
add ebx,eax
jmp loopme ;continue until ESI points to a non-ascii [0-9] character
noascii:
ret ;EBX = 0x00003039 = 12345
Main EndP
答案 1 :(得分:2)
获得字符串后,必须将其转换为数字。问题是,你必须编写自己的程序来做到这一点。这是我经常使用的(虽然用C语写):
int strToNum(char *s) {
int len = strlen(s), res = 0, mul = 0;
char *ptr = s + len;
while(ptr >= s)
res += (*ptr-- - '0') * (int)pow(10.0, mul++);
return res;
}
这是解释。首先,*ptr-- - '0'
获取数字的整数表示(以便'9' - '0' = 9
,然后它递减ptr
,以便它指向前一个字符。一旦我们知道该数字,我们就有了将其提高到10的幂。例如,假设输入为'357',代码的作用是:
('7' - '0' = 7) * 10 ^ 0 = 7 +
('5' - '0' = 5) * 10 ^ 1 = 50 +
('3' - '0' = 3) * 10 ^ 2 = 300 =
---------------------------------
357