Hi,

I'm writing a small application which reads and writes to some files. MY problem is that when I write to the file in my program, I get an error, %eax is set to -14. strerror(-14) returns unknown error, and I was told on irc that posix write() doesn't return -14. As far as I can tell everything is fine with the syscall, I've triple checked everything, but it keeps failing, nothing is written to the file.

.equ SYS_WRITE, 4
.equ ST_BR_WOUT_DESC, -8
.equ LINUX_SYSCALL, 0x80

...

#write 7 to file
    movl $SYS_WRITE, %eax
    movl ST_BR_WOUT_DESC(%ebp), %ebx
    movl $7, %ecx
    #convert to ascii
    addl $48, %ecx
    movl $1, %edx
    int $LINUX_SYSCALL

Thanks in advance for any help

Can you add more then that snippet. And add comments! That 80x86 assembler you're using is camouflaging the assembly code making it harder to read due to its abstraction methodology.

Been doing some research. You're using a Linux assembler that uses the AT&T syntax?

It uses a reverse encoding and different operands.

You're passing the character and not the address of the character to the file I/O.

.equ SYS_WRITE, 4
.equ ST_BR_WOUT_DESC, -8
.equ LINUX_SYSCALL, 0x80 ... 

tmp  .long 0


#write 7 to file    
	movl $SYS_WRITE, %eax                # Linux SYSTEM - WRITE
    movl ST_BR_WOUT_DESC(%ebp), %ebx     # Set ebx file Handle on stack?
    movl $7, %edx                        # binary digit 7
        
    
#convert to ascii
    addl $48, %edx                       # char = '7' = '0'+7
    movl $tmp, %ecx                # get memory address containing character
    mov %ecx,(%edx)              # copy your character into it!


    movl $1, %edx                        # write a single byte
    
# Sys_Write( uint ebx, const char *ecx, size_t edx )
	 ecx is suppose to be the pointer to the data, not the data itself!

    int $LINUX_SYSCALL

I've used a multitude of assemblers before but never LINUX.
This is untested so hope it works.

yeah I'm using GAS, GNU assembler which uses AT&T syntax.

You're right, I'm passing the number instead of a pointer, man I've missed how useless the error messages are in assembly :) thanks a lot that fixed the problem.

Below is the updated code for clarity. I have a buffer, of size 1 byte, which I used earlier in the program to read in a number.

#write 7(max) to file
    movl $SYS_WRITE, %eax                           #put 4 in %eax, syscall code for write()
    movl ST_BR_WOUT_DESC(%ebp), %ebx     #file descriptor for our file, saved to the stack earlier
    movl $55, BUFFER                                    #put the ascii code for 7 into our buffer
    movl $BUFFER, %ecx                                 #put a pointer to our buffer in ecx 
    movl $BUFFER_SIZE, %edx                        #put the length of our Buffer in edx
    int $LINUX_SYSCALL
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.