NASM汇编将input转换为整数?

好的,所以我对组装很新,事实上,我对assembly很陌生。 我写了一段代码,只是简单地把用户的数字input,乘以10,并通过程序退出状态(通过在terminal键入echo $?)将结果expression给用户。问题是,它是没有给出正确的数字,4×10显示为144.那么我认为input可能会作为一个字符,而不是一个整数。 我的问题在于,如何将字符input转换为整数,以便在算术计算中使用?

如果有人能够回答记住我是初学者,这将是非常好的:)另外,如何将所述整数转换回字符?

section .data section .bss input resb 4 section .text global _start _start: mov eax, 3 mov ebx, 0 mov ecx, input mov edx, 4 int 0x80 mov ebx, 10 imul ebx, ecx mov eax, 1 int 0x80 

这里有几个将string转换为整数的函数,反之亦然:

 ; Input: ; ESI = pointer to the string to convert ; ECX = number of digits in the string (must be > 0) ; Output: ; EAX = integer value string_to_int: xor ebx,ebx ; clear ebx .next_digit: movzx eax,byte[esi] inc esi sub al,'0' ; convert from ASCII to number imul ebx,10 add ebx,eax ; ebx = ebx*10 + eax loop .next_digit ; while (--ecx) mov eax,ebx ret ; Input: ; EAX = integer value to convert ; ESI = pointer to buffer to store the string in (must have room for at least 10 bytes) ; Output: ; EAX = pointer to the first character of the generated string int_to_string: add esi,9 mov byte [esi],STRING_TERMINATOR mov ebx,10 .next_digit: xor edx,edx ; Clear edx prior to dividing edx:eax by ebx div ebx ; eax /= 10 add dl,'0' ; Convert the remainder to ASCII dec esi ; store characters in reverse order mov [esi],dl test eax,eax jnz .next_digit ; Repeat until eax==0 mov eax,esi ret 

这就是你将如何使用它们:

 STRING_TERMINATOR equ 0 lea esi,[thestring] mov ecx,4 call string_to_int ; EAX now contains 1234 ; Convert it back to a string lea esi,[buffer] call int_to_string ; You now have a string pointer in EAX, which ; you can use with the sys_write system call thestring: db "1234",0 buffer: resb 10 

请注意,在这些例程中我没有做太多的错误检查(比如检查是否有字符在'0' - '9'范围之外)。 例程也不处理签名的数字。 所以,如果你需要这些东西,你必须自己添加它们。