好吧,我沿着一个教程,我一直在抓着我的大脑了这个..我试过寻找资源,但似乎没有任何工作或点击。所有我试图做的是从文件读取输入,逐个字符地继续保存到另一个文件。 (如果你愿意的话) 现在我遇到了两个让我疯狂的主要问题。NASM汇编语言似乎不能将输出保存到文件
- 出于某种原因,文件的输出被打印到终端屏幕上。这真是太好了,除了这些,我写这些时并不是我想到的。它应该只写入一个文件并退出。
- 脚本运行之后,“输出文件”被创建,但它是空的
,即时通讯以下是在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
你在使用什么API?什么平台? – wallyk
我使用虚拟盒来运行一个32位的Ubuntu。不知道什么API即时使用 –