linux - 了解 ASM 中的输入和输出

标签 linux assembly

谁能帮我理解这里到底发生了什么? 我是汇编语言的新手,写了一段简单的代码如下: (我在LINUX上开发)

我想做的是从用户那里接受一个整数,然后只显示用户输入的内容。

 .section .data

number:
    .long 0

.section .text

.globl _start

_start:

movl $3, %eax           #Read system call
movl $0, %ebx                   #file descriptor (STDIN)
movl $number, %ecx              #the address to which data is to be read into.  
movl $4, %edx                   #number of bytes to be read 
int $0x80

#the entered number is stored in %ebx. it can be viewed using "echo $? "
movl number , %ebx              
movl $1, %eax
int $0x80

但我没有得到预期的结果。相反,我正在获取我输入的任何字符的 ASCII 代码。

例如:

input - 2
output - 50

input - 1
output - 49 

input - a
output - 97 ....  and so on?

怎么了?我应该进行哪些更改才能获得预期的结果?我错过了理解的基本概念是什么。

最佳答案

输入是在系统的 native 代码页中完成的。如果要将数字的ASCII码转换成对应的数字,首先需要做两件事:

  1. 边界检查它。该值必须介于“0”和“9”之间(含),否则它不是数字。

  2. 减去“0”。这样“0”就变成了 0,“5”就变成了 5,等等。

关于linux - 了解 ASM 中的输入和输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18604425/

相关文章:

c++ - LInux/c++,如何同时保护两个数据结构?

linux - D可以编译成平台独立代码吗?

assembly - 为什么编译器在做 64 位操作时会改变 move to move?

c++ - 类成员函数的函数指针与任意函数的指针之间的区别

string - 字符串中 2 个字符之间的汇编切换

java - 如何在编译器中包含汇编器

linux - 网络上所有机器的主机名发现

linux - 如何将以下日期更改为 2 个特定表格? (Linux 终端/shell )

linux - 需要将 azure VM OS 磁盘大小从 32 GB 调整为 16 GB

assembly - 如何在编译时检测 NASM 中的体系结构以获得 x64 和 x86 的一个源代码?