我刚刚开始学习汇编(出于爱好(,我制作了一个小程序,可以从用户那里获得一个数字并打印出来:
section .data:
message: db "please enter a number: " ;define message to "please enter a number"
message_length equ $-message ;define message_length to $-message the lenght of the message
message_done: db "you have entered the number " ;define message_done to "you have entered the number "
message_done_length equ $-message ;define message_done_length to the $-message_done the length of message_done
section .bss:
num resb 5 ;num will store the input the user will give
section .text:
global _start
_start:
mov eax, 4 ;set the next syscall to write
mov ebx, 1 ;set the fd to stdout
mov ecx, message ;set the output to message
mov edx, message_length ;set edx to the length of the message
int 0x80 ;syscall
mov eax,3 ;set the next syscall to read
mov ebx, 2 ;set the fd to stdout
mov ecx, num ;set the output to be num
mov edx, 5 ;set edx to the length of the num
int 0x80 ;syscall
mov eax, 4 ;set the syscall to write
mov ebx, 1 ;set the fd to stout
mov ecx, message_done ;set the output to the message
mov edx, message_done_length ;set edx to the message_done_length
int 0x80 ;syscall
mov eax, 4 ;set the syscall to write
mov ebx ,1 ;set the fd to stdout
mov ecx, num ;set the output to num
mov edx, 5 ;the length of the message
int 0x80 ;syscall
mov eax, 1 ;set the syscall to exit
mov ebx, 0 ;retrun 0 for sucsess
int 0x80 ; syscall
当我运行这个并输入一个数字,然后输入输入时,我得到了这个:
please enter a number: you have entered the number ����
我做错了什么?
三个错误:
section .data:
这组装成一个称为.data:
的部分,该部分不同于.data
。放下冒号。section指令不是标签。在所有其他section
指令中都会出现相同的问题。
message_done: db "you have entered the number "
message_done_length equ $-message
应为$-message_done
。就目前情况来看,你写的字节太多了。这可能是您在邮件后看到垃圾的原因。
mov eax,3 ;set the next syscall to read
mov ebx, 2 ;set the fd to stdout
mov ecx, num ;set the output to be num
mov edx, 5 ;set edx to the length of the num
int 0x80 ;syscall
您希望fd是stdin(0(,您的注释是stdout,而文件描述符2实际上是stderr。使其成为mov ebx, 0
。当您从终端运行程序时,它可能会按原样工作,因为文件描述符0、1和2在终端上都是打开的,并且都是读写的,但如果您使用输入重定向,它会行为不端。