我尝试使用系统调用在Ubuntu 11.04的NASM中实现cat>filename
命令。我的程序已成功编译并运行成功(看起来如此)。但每当我试图启动cat filename
命令它显示“没有这样的文件或目录”,但我看到该文件驻留在目录中。如果我尝试通过双击打开文件,则会显示“您没有打开文件所需的权限”。你能帮我找到我的代码中的错误吗?在NASM中实现cat> fileName命令
的代码如下:
section .data
msg: dd "%d",10,0
msg1: db "cat>",0
length: equ $-msg1
section .bss
a resb 100
len1 equ $-a
b resd 1
c resb 100
len2 equ $-c
section .txt
global main
main:
mov eax,4 ;;it will print cat>
mov ebx,1
mov ecx,msg1
mov edx,length
int 80h
start:
mov eax,3 ;;it will take the file name as input
mov ebx,0
mov ecx,a
mov edx,len1
int 80h
mov eax,5 ;;it will create the file by giving owner read/write/exec permission
mov ebx,a
mov ecx,0100
mov edx,1c0h
int 80h
cmp eax,0
jge inputAndWrite
jmp errorSegment
inputAndWrite:
mov [b],eax
mov eax,3 ;;take the input lines
mov ebx,0
mov ecx,c
mov edx,len2
int 80h
mov edx,eax ;;write the input lines in the file
mov eax,4
mov ebx,[b]
mov ecx,c
int 80h
jmp done
errorSegment:
jmp done
done:
mov eax, 1
xor ebx, ebx
int 80h
P.S.上述代码通过采用RageD的建议进行重新编辑。然而,我创建的文件没有包含从“inputAndWrite”段输入的任何行。我正在寻找你的建议。
您是否试图重新实现'cat'(即连接文件并打印到STDOUT),或者只是创建一个程序来复制像'cp'这样的文件?无论哪种情况,在用'cat filename'测试你的程序之前,你有没有检查过'哪个猫'?在Debian Wheezy中'哪个猫'输出'/ bin/cat'。 – nrz 2013-04-09 22:55:40
@nrz:我试图实现:cat> file1。在启动这个命令之后,file1将包含从命令行给出的输入行。但是在运行我的程序之后,file1被创建,但是当我尝试打开文件时,它显示“您没有打开文件所需的权限”。 – sabu 2013-04-09 23:03:30