如何从ascii转换为整数?
data
asd:
.int 32
.text
.globl _start
_start:
movl $4, %eax
movl $1, %ebx
movl $1,(asd)
add $48,(asd)
movl $asd, %ecx
movl $4, %edx
int $0x80
# Exit the program
movl $1, %eax
movl $0, %ebx
int $0x80
代码正在写一个ascii值,我认为如果我可以添加48值。我可以写,但我不能打印,两个阶段的数字"例如53或156"。我该如何打印?
答案 0 :(得分:0)
我不确定我完全理解你所问的是什么,但我认为你是否试图在终端上打印一个整数?
如果是这种情况,这是一种方法(无符号)。
#define SYS_EXIT $1
#define SYS_WRITE $4
#define STDOUT $1
#define LEN $11
.global _start
.text
_start:
movl $(printable+9), %edi // Pointer to end of digit string
movl number, %eax
cycledigits:
xorl %edx, %edx
divl decimal
addl $48, %edx // Add 48 to remainder, store digit
movb %dl, (%edi)
decl %edi // Next digit
cmpl $0, %eax
jg cycledigits // No more digits
print:
movl STDOUT, %ebx
movl $printable, %ecx
movl LEN, %edx
movl SYS_WRITE, %eax
int $0x80
exit:
movl SYS_EXIT, %eax
xorl %ebx, %ebx // The exit code.
int $0x80
.data
decimal:
.long 10
number: // The number to print:
.long 43
printable: // Digit string goes here:
.zero 10
.byte '\n
答案 1 :(得分:0)
通常最好首先考虑高级语言版本。如果该号码有n
个数字并存储在数组a
中,那么我们需要:
char *p = a;
unsigned val = 0;
while (n > 0) {
n--;
val = 10 * val + (*p++ - '0');
}
所以,让我们说%esi
p
%eax
以上val
为%ecx
,n
为 movl $n, %ecx
movl $a, %esi
xorl %eax, %eax ; val = 0
eval_while_cond:
orl %ecx, %ecx ; if (n <= 0)
jle done ; goto done
subl $1, %ecx ; n--
movl %eax, %ebx ; tmp1 = val
mull $10, %ebx ; tmp1 = 10 * tmp1
movzbl (%esi), %eal; tmp2 = *p
addl $1, %esi ; p++
subl $'0, %eax ; tmp2 = tmp2 - '0'
addl %eax, %ebx ; val = tmp2 + tmp1
jmp eval_while_cond
done:
; result is now in %eax
。逐行翻译
readline