NASM 汇编语言似乎无法将输出保存到文件
NASM assembly language cant seem to save output to a file
好的,所以我正在学习教程,我一直在为此绞尽脑汁..我已经尝试寻找资源,但似乎没有任何效果或点击。我试图做的就是从一个文件中逐个字符地读取输入,然后继续将其保存到另一个文件中。 (如果你愿意,可以复制)
现在我 运行 陷入两个让我抓狂的主要问题。
- 由于某种原因,文件的输出被打印到终端屏幕上。那太好了,但那不是我写这篇文章时想到的。它应该只是写入文件并退出。
- 在 运行 脚本之后,"output file" 被创建但它是空的
我关注的教程是在 https://www.tutorialspoint.com/assembly_programming/assembly_file_management.htm
上找到的
section .data
f_in: db "file_input.txt",0
f_out: db "file_output.txt",0
section .bss
Buff resb 1 ;hold the value of one char
fd_in resb 1
fd_out resb 1
section .data
global _start
_start:
nop ;keeps gdb debugger happy
;Creates the output file that will be used to store
Create:
mov eax, 8 ;sys call, create
mov ebx, f_out ;file name is ebx register
mov ecx, 666q ;file permisions for the outfile in octal
int 80h ;kernel call
mov [fd_out], eax ;mov file desc into fd_out
;Open the input file to be read in read only mode
;(0) = read only
;(1) = write only
;(2) = read write only
Open:
mov eax, 5 ;sys call, open
mov ebx, f_in ;file name in ebx
mov ecx, 0 ;file access mode (0) == read only
int 80h ;kernel call
mov [fd_in], eax ;mov file desc into fd_in
;Read the opened file data into Buff var declared above
;Buff only holds 1 byte
Read:
mov eax, 3 ;sys call, read
mov ebx, [fd_in] ;mov file desc in ebx
mov ecx, Buff ;mov pointer Buff into ecx
mov edx, 1 ;mov Buff size into edx
int 80h
cmp eax, 0 ;check val in eax with 0
je Exit ;if eax is (0 means EOF) so exit
;write all the data encoded into the file
Write:
mov eax, 4 ;sys call, write
mov ebx, [fd_out] ;file descriptor
mov ecx, Buff ;message to write
mov edx, 1 ;length of message
int 80h
jmp Read ;go back to begining and read again
Exit:
;close the files
Close_files:
mov eax, 6 ;sys call, close
mov ebx, [fd_in] ;put file descriptor in ebx
int 80h
mov ebx, [fd_out]
int 80h
;now exit after files are closed
mov eax, 1 ;sys call, exit
int 80h
对于 fd_in resb 1
和 fd_out resb 1
,您只为文件句柄保留了一个字节。但是随后您从这些位置读取和写入整个 ebx
。 ebx
是 4 个字节长。那不会很好地工作。
为两个文件句柄尝试 resb 4
。
对具体情况的解释:打开输出文件,并将句柄存储在 fd_out
中。文件句柄大概是5之类的,所以5写在fd_out
,接下来的3个字节清零。然后,您打开输入文件,并将句柄存储在 fd_in
中。这个文件句柄是6,所以6写在fd_in
里,后面的3个字节清空,也就是说fd_out
清空,因为它就在fd_in
后面在记忆中。然后,下次将 fd_out
加载到 ebx
时,您将读取四个零字节,即 0。文件句柄 0 显然对应于标准输出,这就是为什么所有内容都被转储到屏幕上的原因。
好的,所以我正在学习教程,我一直在为此绞尽脑汁..我已经尝试寻找资源,但似乎没有任何效果或点击。我试图做的就是从一个文件中逐个字符地读取输入,然后继续将其保存到另一个文件中。 (如果你愿意,可以复制) 现在我 运行 陷入两个让我抓狂的主要问题。
- 由于某种原因,文件的输出被打印到终端屏幕上。那太好了,但那不是我写这篇文章时想到的。它应该只是写入文件并退出。
- 在 运行 脚本之后,"output file" 被创建但它是空的
我关注的教程是在 https://www.tutorialspoint.com/assembly_programming/assembly_file_management.htm
上找到的section .data
f_in: db "file_input.txt",0
f_out: db "file_output.txt",0
section .bss
Buff resb 1 ;hold the value of one char
fd_in resb 1
fd_out resb 1
section .data
global _start
_start:
nop ;keeps gdb debugger happy
;Creates the output file that will be used to store
Create:
mov eax, 8 ;sys call, create
mov ebx, f_out ;file name is ebx register
mov ecx, 666q ;file permisions for the outfile in octal
int 80h ;kernel call
mov [fd_out], eax ;mov file desc into fd_out
;Open the input file to be read in read only mode
;(0) = read only
;(1) = write only
;(2) = read write only
Open:
mov eax, 5 ;sys call, open
mov ebx, f_in ;file name in ebx
mov ecx, 0 ;file access mode (0) == read only
int 80h ;kernel call
mov [fd_in], eax ;mov file desc into fd_in
;Read the opened file data into Buff var declared above
;Buff only holds 1 byte
Read:
mov eax, 3 ;sys call, read
mov ebx, [fd_in] ;mov file desc in ebx
mov ecx, Buff ;mov pointer Buff into ecx
mov edx, 1 ;mov Buff size into edx
int 80h
cmp eax, 0 ;check val in eax with 0
je Exit ;if eax is (0 means EOF) so exit
;write all the data encoded into the file
Write:
mov eax, 4 ;sys call, write
mov ebx, [fd_out] ;file descriptor
mov ecx, Buff ;message to write
mov edx, 1 ;length of message
int 80h
jmp Read ;go back to begining and read again
Exit:
;close the files
Close_files:
mov eax, 6 ;sys call, close
mov ebx, [fd_in] ;put file descriptor in ebx
int 80h
mov ebx, [fd_out]
int 80h
;now exit after files are closed
mov eax, 1 ;sys call, exit
int 80h
对于 fd_in resb 1
和 fd_out resb 1
,您只为文件句柄保留了一个字节。但是随后您从这些位置读取和写入整个 ebx
。 ebx
是 4 个字节长。那不会很好地工作。
为两个文件句柄尝试 resb 4
。
对具体情况的解释:打开输出文件,并将句柄存储在 fd_out
中。文件句柄大概是5之类的,所以5写在fd_out
,接下来的3个字节清零。然后,您打开输入文件,并将句柄存储在 fd_in
中。这个文件句柄是6,所以6写在fd_in
里,后面的3个字节清空,也就是说fd_out
清空,因为它就在fd_in
后面在记忆中。然后,下次将 fd_out
加载到 ebx
时,您将读取四个零字节,即 0。文件句柄 0 显然对应于标准输出,这就是为什么所有内容都被转储到屏幕上的原因。