Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a file in assembler and modify it?

I'm starting to learn Assembler and I'm working in Unix. I want to open a file and write 'Hello world' on it.

section .data

textoutput db 'Hello world!', 10
lentext equ $ - textoutput
filetoopen db 'hi.txt'

section .text
global _start

_start:

mov eax, 5            ;open
mov ebx, filetoopen
mov ecx, 2            ;read and write mode
int 80h

mov eax, 4
mov ebx, filetoopen   ;I'm not sure what do i have to put here, what is the "file descriptor"?
mov ecx, textoutput
mov edx, lentext

mov eax, 1
mov ebx, 0
int 80h              ; finish without errors

But when I compile it, it doesn't do anything. What am I doing wrong? When I open a file where does the file descriptor value return to?

like image 756
Rama Avatar asked Nov 29 '11 14:11

Rama


People also ask

How do I open an assembly file?

Open an assemblyIn the main menu, choose File | Open and then select an assembly file. Press Ctrl+O and then select an assembly file. on the Assembly Explorer toolbar and then select an assembly file. Drag an assembly file (or a selection of files) from Windows Explorer to the Assembly Explorer window.

How do I read an assembler file?

Reading from a FilePut the system call sys_read() number 3, in the EAX register. Put the file descriptor in the EBX register. Put the pointer to the input buffer in the ECX register. Put the buffer size, i.e., the number of bytes to read, in the EDX register.

What is a file handle in assembly?

A file handle is an integer value which is used to address an open file.

What is the file extension for an assembly file?

The file extension of the assembly program is ". asm" for the Code Warrior assembler, but is often ". s" for other assemblers. The assembly code is called source code and the assembly program file is usually called a source file.


1 Answers

This is x86 Linux (x86 is not the only assembly language, and Linux is not the only Unix!)...

section .data

textoutput db 'Hello world!', 10
lentext equ $ - textoutput
filetoopen db 'hi.txt'

The filename string requires a 0-byte terminator: filetoopen db 'hi.txt', 0

section .text
global _start

_start:

mov eax, 5            ;open
mov ebx, filetoopen
mov ecx, 2            ;read and write mode

2 is the O_RDWR flag for the open syscall. If you want the file to be created if it doesn't already exist, you will need the O_CREAT flag as well; and if you specify O_CREAT, you need a third argument which is the permissions mode for the file. If you poke around in the C headers, you'll find that O_CREAT is defined as 0100 - beware of the leading zero: this is an octal constant! You can write octal constants in nasm using the o suffix.

So you need something like mov ecx, 0102o to get the right flags and mov edx, 0666o to set the permssions.

int 80h

The return code from a syscall is passed in eax. Here, this will be the file descriptor (if the open succeeded) or a small negative number, which is a negative errno code (e.g. -1 for EPERM). Note that the convention for returning error codes from a raw syscall is not quite the same as the C syscall wrappers (which generally return -1 and set errno in the case of an error)...

mov eax, 4
mov ebx, filetoopen   ;I'm not sure what do i have to put here, what is the "file descriptor"?

...so here you need to mov ebx, eax first (to save the open result before eax is overwritten) then mov eax, 4. (You might want to think about checking that the result was positive first, and handling the failure to open in some way if it isn't.)

mov ecx, textoutput
mov edx, lentext

Missing int 80h here.

mov eax, 1
mov ebx, 0
int 80h              ; finish without errors
like image 140
Matthew Slattery Avatar answered Oct 11 '22 14:10

Matthew Slattery