Assembly(ASM)中读取系统调用的行为

时间:2017-03-17 12:35:47

标签: linux assembly x86 system-calls

我目前正在学习ASM,我对以下代码(编译)有疑问

此代码来自此tutorial

问题是:为什么当fd在指定的行上为0,1或2(对应于stdin,stdout和stderr)时,以及当fd为3或更大时,我具有相同的行为,它什么都不做(它会跳过scanf)。

section .data                           ;Data segment
   userMsg db 'Please enter a number: ' ;Ask the user to enter a number
   lenUserMsg equ $-userMsg             ;The length of the message
   dispMsg db 'You have entered: '
   lenDispMsg equ $-dispMsg                 

section .bss           ;Uninitialized data
   num resb 5

section .text          ;Code Segment
   global _start

_start:                ;User prompt
   mov eax, 4
   mov ebx, 1
   mov ecx, userMsg
   mov edx, lenUserMsg
   int 80h

   ;Read and store the user input
   mov eax, 3
   mov ebx, 2 ; /!\ QUESTION IS ABOUT THIS LINE /!\
   mov ecx, num  
   mov edx, 5          ;5 bytes (numeric, 1 for sign) of that information
   int 80h

   ;Output the message 'The entered number is: '
   mov eax, 4
   mov ebx, 1
   mov ecx, dispMsg
   mov edx, lenDispMsg
   int 80h  

   ;Output the number entered
   mov eax, 4
   mov ebx, 1
   mov ecx, num
   mov edx, 5
   int 80h  

   ; Exit code
   mov eax, 1
   mov ebx, 0
   int 80h

我们可以使用以下命令编译和执行此代码:

$> nasm -f elf64 test.S
$> ld test.o
$> ./a.out

谢谢,

0 个答案:

没有答案