Linux中的文件操作 [英] File operations in Linux
问题描述
大家好:
我正在编写一个小程序集程序,它将打开一个文件,处理它b $ b,然后再关闭它。我正在努力构建一个简单的框架。为了它
目前。我已经打开,关闭和退出系统调用了,
但我觉得有问题。
我的代码是下面。它似乎工作正常,它只是静默运行,如下面给出
。但是如果我尝试使用不存在的文件(例如将/ dev / zero更改为
/ dev / zer),则不会产生错误消息。
感谢您的任何建议!
section .text
path db" / dev / zero",0x00
global _start
_start:
mov eax,5;打开
mov ebx,路径
xor ecx,ecx; O_RDONLY
mov edx,0x100; S_IRUSR
int 0x80
xchg eax,ebx;将fd放入ebx
mov eax,6;关闭
int 0x80
mov eax,1;退出
int 0x80
Hello all:
I''m writing a small assembly program that will open a file, process
it, then close it again. I''m trying to build a simple "framework" for it
at the moment. I''ve got the open, close, and exit system calls in place,
but I think there''s something wrong.
My code is below. It seems to work OK, it just runs silently as given
below. But if I try it with a non-existent file (e.g. change /dev/zero to
/dev/zer) then no error message is produced.
Thanks for any advice!
section .text
path db "/dev/zero",0x00
global _start
_start:
mov eax,5 ; open
mov ebx,path
xor ecx,ecx ; O_RDONLY
mov edx,0x100 ; S_IRUSR
int 0x80
xchg eax,ebx ; put fd into ebx
mov eax,6 ; close
int 0x80
mov eax,1 ; exit
int 0x80
推荐答案
2008年5月26日10:11,kid joe写道:
On 26 May 2008 at 10:11, kid joe wrote:
我的代码如下。它似乎工作正常,它只是静默运行,如下面给出
。但是,如果我尝试使用不存在的文件(例如,将/ dev / zero
更改为/ dev / zer),则不会生成错误消息。
My code is below. It seems to work OK, it just runs silently as given
below. But if I try it with a non-existent file (e.g. change /dev/zero
to /dev/zer) then no error message is produced.
这并不奇怪 - 你没有告诉它产生错误
消息!如果你使用一个不存在的文件,那么open()系统调用
*会*通过
返回一个负面的nunber,通知你的程序有问题在eax。你只需选择忽略这个...
其他几件事:因为你没有创建文件,所以打开模式
参数()被忽略,所以预先在edx中放置任何特殊的东西是没有意义的。
你无需使用xchg指令,无论如何你立即使用了。而且你最后没有给shell返回一个明智的退出
状态。
这里是你修改这些东西的代码:
节.data
path db" / dev / zero",0x00
errmsg db"打开文件时出错! ,0x0A
errlen equ
That''s hardly surprising - you didn''t tell it to produce an error
message! If you use a non-existent file, then the open() system call
*will* thoughtfully inform your program that there was a problem, by
returning a negative nunber in eax. You just choose to ignore this...
A couple of other things: as you''re not creating a file, the mode
argument to open() is ignored, so there''s no point putting anything
special in edx beforehand. There''s no need for the xchg instruction when
you immediately clobber eax anyway. And you don''t return a sensible exit
status to the shell at the end.
Here''s your code with these things fixed up:
section .data
path db "/dev/zero",0x00
errmsg db "Error opening file!",0x0A
errlen equ
-errmsg
section .text
global _start
_start:
mov eax,5;打开
mov ebx,路径
xor ecx,ecx; O_RDONLY
int 0x80
或eax,eax
jns cont
;错误打开文件...
mov eax,4;写
mov ebx,2; stderr
mov ecx,errmsg
mov edx,errlen
int 0x80
mov ebx,1;返回失败状态
jmp完成
续:
mov ebx,eax;将fd放入ebx
mov eax,6;关闭
int 0x80
xor ebx,ebx;返回成功状态
完成:
mov eax,1;退出
int 0x80
-errmsg
section .text
global _start
_start:
mov eax,5 ; open
mov ebx,path
xor ecx,ecx ; O_RDONLY
int 0x80
or eax,eax
jns cont
; error opening file...
mov eax,4 ; write
mov ebx,2 ; stderr
mov ecx,errmsg
mov edx,errlen
int 0x80
mov ebx,1 ; return failure status
jmp finish
cont:
mov ebx,eax ; put fd into ebx
mov eax,6 ; close
int 0x80
xor ebx,ebx ; return success status
finish:
mov eax,1 ; exit
int 0x80
" Antoninus Twink" < no **** @ nospam.invalidwrote in message
"Antoninus Twink" <no****@nospam.invalidwrote in message
2008年5月26日10:11,kid joe写道:
global _start
_start:
mov eax,5;打开
mov ebx,路径
xor ecx,ecx; O_RDONLY
int 0x80
On 26 May 2008 at 10:11, kid joe wrote:
global _start
_start:
mov eax,5 ; open
mov ebx,path
xor ecx,ecx ; O_RDONLY
int 0x80
这是无可争议的偏离主题。请不要在这里回复
来保护新闻组。
-
免费游戏和编程好东西。
< a rel =nofollowhref =http://www.personal.leeds.ac.uk/~bgy1mmtarget =_ blank> http://www.personal.leeds.ac.uk/~bgy1mm
这篇关于Linux中的文件操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!