943,704 Members | Top Members by Rank

Ad:
  • Assembly Discussion Thread
  • Unsolved
  • Views: 807
  • Assembly RSS
Jul 2nd, 2009
0

write() error code

Expand Post »
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.

asm Syntax (Toggle Plain Text)
  1. .equ SYS_WRITE, 4
  2. .equ ST_BR_WOUT_DESC, -8
  3. .equ LINUX_SYSCALL, 0x80
  4.  
  5. ...
  6.  
  7. #write 7 to file
  8. movl $SYS_WRITE, %eax
  9. movl ST_BR_WOUT_DESC(%ebp), %ebx
  10. movl $7, %ecx
  11. #convert to ascii
  12. addl $48, %ecx
  13. movl $1, %edx
  14. int $LINUX_SYSCALL

Thanks in advance for any help
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
calef13 is offline Offline
2 posts
since Jul 2009
Jul 2nd, 2009
0

Re: write() error code

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.
Last edited by wildgoose; Jul 2nd, 2009 at 5:30 pm. Reason: typo
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 2nd, 2009
0

Re: write() error code

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.

Assembly Syntax (Toggle Plain Text)
  1. .equ SYS_WRITE, 4
  2. .equ ST_BR_WOUT_DESC, -8
  3. .equ LINUX_SYSCALL, 0x80 ...
  4.  
  5. tmp .long 0
  6.  
  7.  
  8. #write 7 to file
  9. movl $SYS_WRITE, %eax # Linux SYSTEM - WRITE
  10. movl ST_BR_WOUT_DESC(%ebp), %ebx # Set ebx file Handle on stack?
  11. movl $7, %edx # binary digit 7
  12.  
  13.  
  14. #convert to ascii
  15. addl $48, %edx # char = '7' = '0'+7
  16. movl $tmp, %ecx # get memory address containing character
  17. mov %ecx,(%edx) # copy your character into it!
  18.  
  19.  
  20. movl $1, %edx # write a single byte
  21.  
  22. # Sys_Write( uint ebx, const char *ecx, size_t edx )
  23. ecx is suppose to be the pointer to the data, not the data itself!
  24.  
  25. int $LINUX_SYSCALL

I've used a multitude of assemblers before but never LINUX.
This is untested so hope it works.
Last edited by wildgoose; Jul 2nd, 2009 at 5:57 pm. Reason: twiddle
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 2nd, 2009
0

Re: write() error code

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.

asm Syntax (Toggle Plain Text)
  1. #write 7(max) to file
  2. movl $SYS_WRITE, %eax #put 4 in %eax, syscall code for write()
  3. movl ST_BR_WOUT_DESC(%ebp), %ebx #file descriptor for our file, saved to the stack earlier
  4. movl $55, BUFFER #put the ascii code for 7 into our buffer
  5. movl $BUFFER, %ecx #put a pointer to our buffer in ecx
  6. movl $BUFFER_SIZE, %edx #put the length of our Buffer in edx
  7. int $LINUX_SYSCALL
Last edited by calef13; Jul 2nd, 2009 at 6:13 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
calef13 is offline Offline
2 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Assembly Forum Timeline: protected mode switch
Next Thread in Assembly Forum Timeline: Problem assembling with Nasm-IDE





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC