NASM - The Netwide Assembler
NASM Forum => Programming with NASM => Topic started by: saugata bose on April 10, 2013, 08:02:16 AM
-
I try to implement `cat>filename` command in NASM in Ubuntu 11.04 using system calls. My program is compiled successfully and run successfully (seems so). But whenever I tried to fire `cat filename` command it shows "No such file or directory" yet I see the file residing in the directory. And if I try to open the file by double clicking it shows me "You do not have the permissions necessary to open the file." Can you please help me to find the errors in my code?
The code is following:
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 ,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,
mov ecx,c
int 80h
jmp done
errorSegment:
jmp done
done:
mov eax, 1
xor ebx, ebx
int 80h
-
I try to implement `cat>filename` command in NASM in Ubuntu 11.04 using system calls. My program is compiled successfully and run successfully (seems so). But whenever I tried to fire `cat filename` command it shows "No such file or directory" yet I see the file residing in the directory. And if I try to open the file by double clicking it shows me "You do not have the permissions necessary to open the file." Can you please help me to find the errors in my code?
The code is following:
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
-
Hi Saugata - I changed your "quote" and "/quote" to "code" and "/code". I think it looks a litle better - and makes it easier to copy and paste it to try. :)
But I wanted to ask you how you link this? I ASSume you're using "-f elf" or "-f elf32" (they're exactly the same) to assemble it. This "file or directory not found" (when it's right there!) is familiar to me. It can be caused by ld looking for a "dynamic linker" or "interpreter" that's not there. If you're using gcc to link it (which "global main" suggests) then gcc should find the right interpreter, but if you're using ld directly, you may need to tell it "-I/lib/ld-linux.so.2" or whatever's right for your system. You may need to tell gcc "-32".
Let me try this and see what I can learn...
Best,
Frank
-
Dear Frank,
thank you for your kind reply.
After running this code (cat>file1), file1 is created successfully. But there is nothing inside it. I don't get why my write function is not working here.
eagerly waiting for your rely.
And many many thanx for editing. Actually I don't know what is your code writing format in this forum.
Saugata
-
Well... I learned that I don't remember much about the "O_xxxx" flags to sys_open... or the permissions. I fiddled a little with your flags - surely "0100" is supposed to be "0100q" (octal)... but I "or"ed it with 2 for O_RDWR. I think I just butchered your permissions. :)
But the main thing that I think made a difference was that I zero-terminated your filename. sys_read does not return a zero-terminated string, it's likely to be terminated with a linefeed. I think I was creating a file named "fred\n" (fred was the filename I was using... or trying to use). After I zero-terminated it...
; get filename with sys_read
mov byte [ecx + eax - 1], 0
... things seemed to go better. I can see the "hello fred" I typed in the file "fred". The "permissions" seemed screwed up, but It "kinda works" at this point... mmm... I put the permissions back the way you had 'em... But "O_" flags as 102q... I think it was the zero-termination that fixed it.
I was getting weird results, before. The sys_open seemed to succeed, but when it came time to write the text to the file, the sys_write failed with 0xFFFFFFF7 or -9 - bad file number... ?
The code-writing syntax here is just the word "code" in square brackets - like a Nasm memory reference, not angle brackets like at SO - and "/code" in square brackets at the end. No need for blank lines and leading spaces like at SO. I like this style better... probably because I'm used to it...
Best,
Frank
P.S. Maybe I shouldn't mention it, but I don't think this is how "cat" behaves, exactly...
-
Dear Frank,
I am trying to solve the problem from last night. As I am a newbie in NASM, some concepts are lacking within me. I asked my classmates about it. But they also seem baffled. Thank you, very much.
Yet, I don't understand how to deal with "mov byte[ecx+eax -1],0". For your convenience I am inserting my MODIFIED version of the code. Can you please review the code?
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
mov ebx,1
mov ecx,msg1
mov edx,length
int 80h
start:
mov eax,3
mov ebx,0
mov ecx,a
mov edx,len1
int 80h
mov byte[ecx+eax -1],0 ;;I put your code here after taking the file name as input
mov eax,5
mov ebx,a
mov ecx,0100q
mov edx,1c0h
int 80h
cmp eax,0
jge inputAndWrite
jmp errorSegment
inputAndWrite:
mov [b],eax
mov eax,3
mov ebx,0
mov ecx,c
mov edx,len2
int 80h
mov edx,eax
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
this also shows the same problem. File is created but the input lines are nor shown inside the file. I think ,I misplaced the "mov byte[ecx+eax -1],0".
-
Your zero-termination is in the right place. I've got to refresh my memory on the open flags, but I think you may be opening it for read-only. Try 0101q or 0102q in ecx for the sys_open.
Best,
Frank
-
Frank,
it works finally. :) :D
It works for 0101q
Thank you for all of your support. Actually I try to implement all cat operations individually. After learning from your lesson I try to implement "cat file1"
And in this case if I try to get now "cat file1" operations, here an interesting event occurs. file1 has not opened up in this case. The code is following:
ection .data
msg: dd "%d",10,0
msg1: db "cat ",0
length: equ $-msg1
section .bss
a resb 100
len1: equ $-a
b: resb 100
len2: equ $-b
section .text
global main
extern printf
main:
push ebp
mov ebp,esp
mov eax,4
mov ebx,1
mov ecx,msg1
mov edx,length
int 80h
start:
mov eax,3
mov ebx,0
mov ecx,a
mov edx,len1
int 80h
mov byte[ecx+eax -1],0
open:
mov eax,5
mov ebx,a
mov ecx,0000q
mov edx,1c0h
int 80h
cmp eax,0
jge read_write
pusha ;;here for debugging purpose I tried to see the value of eax if file is not opened. And everytime eax remains negative,; means no file is opened up.
push msg1 ;;
call printf ;;
add esp,4 ;;
popa ;;
jmp exit
read_write:
pusha ;;it is used to print the positive value of eax for succesfull opening of file
push eax
push msg
call printf
add esp,8
popa
exit:
mov ebp,esp
pop ebp
ret
Thank you again for your guidance, Frank.
Saugata