简体   繁体   中英

Understanding input and output in ASM

can Anyone please help me understand what exactly is happening here? I am new to assembly language and have written a simple code as follows: (I am developing on LINUX)

What i want to do is accept an integer from the user and just display what the user has entered.

 .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

but I am not getting the expected result. instead i am getting ASCII codes for any character that i am inputting.

for ex:

input - 2
output - 50

input - 1
output - 49 

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

what is wrong? what changes should i make to have the desired result? what is the basic concept that i missed understanding.

Input is done in the system's native codepage. If you want to convert the numeral's ASCII code into its corresponding number then you need to do two things first:

  1. Bounds check it. The value must be between '0' and '9' inclusive, otherwise it wasn't a numeral.

  2. Subtract '0'. This way '0' becomes 0, '5' becomes 5, etc.

Write it in C then use your compiler to generate the assembly.

Then fix up the assembly to make it look like you wrote it.

well I have figured out an answer for my own question. It may be useful for others who have just started to learn ASM and come up with same interpreting problem that i had.

first of all lets look at what is happening in the code:

.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

Till here what is happening is : program waits for the user to enter a number. But , what actually is being read is the ASCII code for the "bytes".

Thus when a user enters '2' what actually is read is 0x32 or (110010 in binary or 
its decimal equivalent is 50)

The keyboard driver arranges to put these bits in a special memory location that is written when something is read from the input. What is true for the input is same for output. When a memory location contains a binary sequence that is reported on the screen , similar ASCII conversion is applied.

(considering only ASCII standard for the moment) thus, INPUT------> ASCII CONVERSION--------> converted data (bytes) STORED BYTES-------->ASCII CONVERSION--------->Output Data

so , now the question arises why the output (actually the contents of %ebx ) of this program are not as expected...?

In our program, we are not displaying anything to screen. All echo $? is doing is simply reading whatever there is in ebx -- that is 110010 (50). If you want echo $? to display 2, then you have got to put 2 (ie 00010) in ebx.

The echo $? program is "interpreting" the contents of the memory (ebx) as an integer, not a character.

This is what that makes the difference!

And other thing that has to do is with the "little endian" intel architecture. when you enter 4523 , why do we get output as "52" and not 53, 50 etc?.... In little endian architecture , when you enter the number 4523, '4' is read and is stored in least significant byte in the address 'number'

If you compile/run program on big endian you may have different results (vice-versa) And the most important thing is : when you "echo $?" the least significant byte of %ebx is interpreted.

That is why we get output 52 i.e '4'.

This is a tricky and difficult concept to understand and explain. You have to spend some time thinking over this. Once you get it, you feel great.

thankyou.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM