Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux上的程序集:来自程序集的意外行为_Linux_Assembly_Linux Kernel_X86 - Fatal编程技术网

Linux上的程序集:来自程序集的意外行为

Linux上的程序集:来自程序集的意外行为,linux,assembly,linux-kernel,x86,Linux,Assembly,Linux Kernel,X86,运行下面的代码将生成一个文件,其中包含欢迎使用jj Shashwat作为内容。我不明白的是,为什么它在文件的末尾写Shashwat,Shashwat在一个完全不同的变量中。你知道为什么会这样吗 section .text global _start ;must be declared for using gcc _start: ;create the file mov eax, 8 mov ebx, file_name mov ecx,

运行下面的代码将生成一个文件,其中包含欢迎使用jj Shashwat作为内容。我不明白的是,为什么它在文件的末尾写Shashwat,Shashwat在一个完全不同的变量中。你知道为什么会这样吗

section .text
   global _start         ;must be declared for using gcc

_start:  

   ;create the file
   mov  eax, 8
   mov  ebx, file_name
   mov  ecx, 0777        ;read, write and execute by all
   int  0x80             ;call kernel

   mov [fd_out], eax

   ; close the file
   mov eax, 6
   mov ebx, [fd_out]

   ;open the file for reading
   mov eax, 5
   mov ebx, file_name
   mov ecx, 2             ;for read/write access
   mov edx, 0777          ;read, write and execute by all
   int  0x80

   mov  [fd_out], eax

   ; write into the file
   mov  edx,len          ;number of bytes
   mov  ecx, msg         ;message to write
   mov  ebx, [fd_out]    ;file descriptor 
   mov  eax,4            ;system call number (sys_write)
   int  0x80             ;call kernel

   ; close the file
   mov eax, 6
   mov ebx, [fd_out]

   mov  eax,1             ;system call number (sys_exit)
   int  0x80              ;call kernel

section .data
        file_name db 'myfile.txt', 0
        msg db 'Welcome to jj', 0
        mgafter db ' Shashwat', 0
        lntwo equ $-mgafter
        len equ  $-msg

section .bss
        fd_out resb 1
        fd_in  resb 1
        info resb  26
这是因为您在定义了msg和msgafter之后说了len eq$-msg,所以len被设置为msg和msgafter的长度,使您的写调用同时写入这两个字符串。这是因为len eq$-msg表示“将len设置为当前位置$和msg位置之间的差值。”


要解决此问题,请将len eq$-msg行移到msg定义的后面

不知道为什么这会让你感到惊讶。您要求它将len字节的数据写入文件,并且您已将len定义为msg定义和msgafter定义结束之间的所有字节。这两个问题都在编写中,因为这包含了所有的细节。一个好的问题应该包含源代码、观察到的行为、预期的行为、对行为意外原因的解释以及对代码的良好注释。如果更多的问题写得那么好,回答问题将是一个更愉快的经历。汇编中没有变量,只有字节。。。当您写入24个字节而不是14个字节时,文件中就有24个字节。另外,您可能不想将零字节存储到文本文件中?这是非常罕见的,我宁愿添加换行符,也就是说,我会将源数据定义更改为:…to jj',10,以便在最终文本文件中有换行符。顺便说一句,$在这些equ中的作用有一些相当好的重复,因此如果fuz的答案不完全清楚,请搜索有关nasm、equ和$的一些问题。虽然我认为答案很清楚:@fuz:这个已经有一个精确的副本了。但是是的,写得好的问题很好,并且很容易看出问题所在。@沙什瓦特:作为复制品结束并不意味着这是一个糟糕的问题;如果您不知道问题发生的原因,则无法搜索正确的术语以找到重复项。fd_out:resb 1是一个问题。您正在加载/存储4个字节,但只保留了1个。mov[fd_out],eax覆盖fd_in之后的resb,以及info之后的resb的前2个字节。正如Ped7g所说,程序集没有变量:标签只是让您引用位置的标记,让数据布局+加载/存储指令变得有意义取决于您。