汇编语言从用户输入但显示输出

时间:2015-11-23 02:39:36

标签: linux assembly x86 user-input

我写了这段代码,它从用户读取数据但没有显示输出。它是用汇编语言编写的。我是汇编语言的新手。有人可以帮我解决这个问题。我将非常感激。提前致谢。这是代码:

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
   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

2 个答案:

答案 0 :(得分:3)

在典型环境中,文件描述符0代表标准输入,1代表标准输出,2代表标准错误输出。

从标准错误输出中读取对我来说毫无意义。

尝试更改程序以进行阅读

   ;Read and store the user input
   mov eax, 3
   mov ebx, 2
   mov ecx, num  
   mov edx, 5       ;5 bytes (numeric, 1 for sign) of that information
   int 80h

   ;Read and store the user input
   mov eax, 3
   mov ebx, 0
   mov ecx, num
   mov edx, 5       ;5 bytes (numeric, 1 for sign) of that information
   int 80h

以使系统从标准输入读取一些数据。

答案 1 :(得分:1)

section .data
out1: db 'Enter the number:'
out1l: equ $-out1
out2: db 'The number you entered was:'
out2l: equ $-out2




section  .bss
input: resb 4


section .text
global _start
_start:

;for displaying the message
mov eax,4
mov ebx,1
mov ecx,out1
mov edx,out1l
int 80h



;for taking the input from the user
mov eax,3
mov ebx,0
mov ecx,input
mov edx,4
int 80h

;for displaying the message
mov eax,4
mov ebx,1
mov ecx,out2
mov edx,out2l
int 80h



;for displaying the input

mov eax,4
mov ebx,1
mov ecx,input
mov edx,4
int 80h

mov eax,1
mov ebx,100
int 80h