Assembly 使用INT 21h(DOS)读取数字&;8086大概
我需要提示用户一条消息,告诉他写一个数字,然后我存储这个数字并对其执行一些操作 在INT 21h中搜索后,我发现:Assembly 使用INT 21h(DOS)读取数字&;8086大概,assembly,dos,x86-16,Assembly,Dos,X86 16,我需要提示用户一条消息,告诉他写一个数字,然后我存储这个数字并对其执行一些操作 在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:
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
这不是我的目标。
有什么想法吗?一旦得到字符串,就必须将其转换为数字。问题是,您必须编写自己的过程才能做到这一点。这是我通常使用的(尽管是用C编写的): 这是解释。首先,
*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
,将its指针放入ESI(ESI=字符串地址)
如果您需要读取三个字符,则必须将“读取一个字符”调用放入循环中,直到获得所需的三个字符。@PeteWilson我需要将整数作为一个“整体”来读取,这样我就可以对其执行例如加法操作……如何在汇编中执行此操作:)?@xsari3x:只需将代码翻译为汇编;)最困难的部分是pow()函数,您必须自己编写它。如果你想要十六进制数,你可以通过简单的左移来实现。这种算法不容易转换成asm
res=res*10+(数字-'0')
效率更高且易于实现。请参阅以了解执行此操作的x86 asm。使用imul ebx、eax、10
,而不是那种笨拙的push
/mul
东西。另外,如果您需要,可以使用movsx-eax[esi]
,或者最好使用零扩展,如中所示
('7' - '0' = 7) * 10 ^ 0 = 7 +
('5' - '0' = 5) * 10 ^ 1 = 50 +
('3' - '0' = 3) * 10 ^ 2 = 300 =
---------------------------------
357
.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