NASM - The Netwide Assembler
NASM Forum => Programming with NASM => Topic started by: ecstatic on June 12, 2011, 06:12:18 AM
-
While opening a file through int 80h, what should be the syntax of the file name we have to give in ebx? No matter how i try it gives me syntax error. My code is:
MOV EAX,5
MOV EBX,receipt.txt
MOV ECX,2
MOV EDX,700
INT 80h
I need to open receipt.txt. I have tried writing only 'receipt' or the full path but it does not work any way. What am i doing wrong?
-
sys_open wants the address (offset) of a zero-terminated string containing the filename in ebx.
; nasm -f elf32 myfile.asm
; ld -o myfile myfile.o
global _start ; tell ld about our entrypoint
section .data
filename db "receipt.txt", 0
section .bss
file_descriptor resd 1
section .text
_start:
mov eax, 5 ;__NR_open
mov ebx, filename
mov ecx, 2 ; open mode (is 2 right for what you want?)
mov edx, 700 ; file permissions (if we're creating it) - usually octal!
int 80h
cmp eax, -4096
ja error
mov [file_descriptor], eax
;... read, write, whatever
; close file
mov eax, 6 ; __NR_close
mov ebx, [file_descriptor]
int 80h
error:
mov ebx, eax
neg ebx ; make it positive - can read this with "echo $?"
; don't forget to exit cleanly!
exit:
mov eax, 1 ; __NR_exit
int 80h
Something like that... This assumes that the file is in the current directory - sys_open doesn't know about the "PATH" environment variable, so you may need to specify the full path in the "filename" string, if it's elsewhere. That's untested code, but I think it's fairly near right (famous last words!). Try something like that, and if it doesn't work, do "echo $?" to find out the error number. Then look it up, and see what went wrong. If you can't fix it, get back to us.
(Incidentally - put "code" in square brackets - like a Nasm memory reference - at the beginning of your code, and "/code" in square brackets at the end of your code, lest the forum software get confused)
Best,
Frank
-
sys_open wants the address (offset) of a zero-terminated string containing the filename in ebx.
; nasm -f elf32 myfile.asm
; ld -o myfile myfile.o
global _start ; tell ld about our entrypoint
section .data
filename db "receipt.txt", 0
section .bss
file_descriptor resd 1
section .text
_start:
mov eax, 5 ;__NR_open
mov ebx, filename
mov ecx, 2 ; open mode (is 2 right for what you want?)
mov edx, 700 ; file permissions (if we're creating it) - usually octal!
int 80h
cmp eax, -4096
ja error
mov [file_descriptor], eax
;... read, write, whatever
; close file
mov eax, 6 ; __NR_close
mov ebx, [file_descriptor]
int 80h
error:
mov ebx, eax
neg ebx ; make it positive - can read this with "echo $?"
; don't forget to exit cleanly!
exit:
mov eax, 1 ; __NR_exit
int 80h
Something like that... This assumes that the file is in the current directory - sys_open doesn't know about the "PATH" environment variable, so you may need to specify the full path in the "filename" string, if it's elsewhere. That's untested code, but I think it's fairly near right (famous last words!). Try something like that, and if it doesn't work, do "echo $?" to find out the error number. Then look it up, and see what went wrong. If you can't fix it, get back to us.
(Incidentally - put "code" in square brackets - like a Nasm memory reference - at the beginning of your code, and "/code" in square brackets at the end of your code, lest the forum software get confused)
Best,
Frank
I tried this and it executed fine but when control reaches mov ebx, filename a dialogue box opens saying the NTVDM CPU has encountered an illegal instruction. CS:0000 IP:0075 OP:f0 00 f0 37 05
What could be the possible problem now?
-
I am using nasm on windows 7. Would there be any difference in instructions?
-
Oh. I thought you'd switched to Linux. Different instructions entirely for Windows - well, not different instructions, but you need to do different things with 'em. int 80h is just not on!
For 'doze... you'd want "OpenFile"... "_OpenFile@12", actually. The address of a zero-terminated string with the filename would be one of the parameters - probably the last one pushed. Dunno about openmode... "when in doubt, push zero" used to work for me...
extern _Openfile@12
push 0
push 0
push filename
call _Openfile@12
...
That probably isn't even close! You need a Windows help file!!! Consult MicroSoft.
Best,
Frank
-
I stand corrected. OpenFile is obsolete, you want CreateFile!
http://msdn.microsoft.com/en-us/library/aa363858(v=vs.85).aspx
Best,
Frank