我嘗試使用系統調用在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