Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cromulent

macrumors 604
Original poster
Oct 2, 2006
6,829
1,132
The Land of Hope and Glory
I'm trying to write a simple assembly program to open and read a file. I have the following code (sorry the forum has mucked up my formatting somewhat):

Code:
SECTION .data
	path  db 'test.txt'          ; open the file "test.txt"
	flags db 0x0000           ; set flags to O_RDONLY as defined in sys/fcntl.h
	mode  db 0x0		 ; set mode to NULL
	read_size db 0xFF	 ; set the size to read in bytes to 255

SECTION .text
	global start
	
start:
	enter 0,0			 ; enter the process
	pusha				 ; not sure about this
	push dword mode		 ; push path onto the stack
	push dword flags	         ; push flags onto the stack
	push dword path		 ; push mode onto the stack
	mov eax, 0x5		 ; call open() as defined in sys/syscall.h
	push dword eax		 ; push eax onto the stack
	int 0x80			 ; make the kernel call
	mov edx, eax		 ; store the return value from open in edx
	add esp, byte 16	         ; align the stack
	mov eax, 0x3		 ; call read() as defined in sys/syscall.h
	push dword eax		 ; push eax onto the stack
	int 0x80			 ; make the kernel call
	add esp, byte 16           ; align the stack again
	push dword read_size    ; push read_size onto the stack
	push dword edx            ; push edx onto the stack
	mov eax, 0x1		 ; call exit procedure
	int 0x80			 ; make the kernel call

After the mov eax, 0x3 and push dword eax calls I am missing the place to store the input that is read from the file and the file descriptor. The file descriptor is stored in edx currently but from what I have read I should use edx to store the things read from the file. Where should I store the file descriptor in this case? Can I just use any of the general purpose registers? My code assembles and executes without core dumping which is generally a good sign but as it does not do anything at the moment I can't really test it.

Any help is appreciated.

Edit: Aha, just read about esi and edi. They seem to be what I am looking for.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.