Author Topic: sys_open 64 bit  (Read 16363 times)

Offline TightCoderEx

  • Full Member
  • **
  • Posts: 103
sys_open 64 bit
« on: May 10, 2012, 03:15:25 PM »
Here is a brief snippet that opens a file and it works

Code: [Select]
.OpenF: xor rsi, rsi ; flags = O_RDONLY
mov rdx, rsi ;  mode = NULL
mov rax, rdx
mov al, SYS_OPEN ; = 2
syscall ; Open file
or rax, rax
jle .Exit

Excerpt man 2 open on my system Ubuntu 12.04

RETURN_VALUE:
        open () and creat () return the new file descriptor, or -1 if an error occurred ( in which case, errno is set appropriately )

1: When a valid descriptor is returned it is in RAX
2: When an error occures, ECX = -1 and errno in RAX as a negative value

Questions:

1. Is what I've gleaned from experimentation correct in so much as values being returned by 64 bit and syscall
2. Is there something already available that will return a point to text denoting the error.


For my own purposes and maybe as a benefit to others I want to hammer out an algo where registers are setup in the same manner as a syscall invocation, but my procedure would leave all registers unchanged except RAX if successful, or ZR=1 and RAX = errorno if call failed and RSI a pointer to ASCII string indicating what the error is.

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: sys_open 64 bit
« Reply #1 on: May 10, 2012, 03:49:55 PM »
I highly discourage you from returning anything in RSI.  Your code will cause you problems later on if you attempt to interface with other modules.  Let me also discourage you from trying to save every register.  There is a reason why registers RCX/RDX/R8/R9 are considered volatlie ( ie: trashed by the calling procedure ).  Although they are used as parameters to the calling procedure, compilers that call your procedure will have arranged the code to save what needs saved anyways.

You may wish to read up on calling conventions before trying to change the world.  ;)

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: sys_open 64 bit
« Reply #2 on: May 10, 2012, 04:53:54 PM »
64-bit calling conventions have already changed the world! :)

Seriously, while I don't disagree with anything Rob said, I have set up a similar routine for 32-bit system calls. I started by converting the comments in errno.h to strings, set up an array of pointers to 'em, and use the (negated) error number as an index into 'em. I invoke it via a macro that prints an "identifier", the errno.h text, and then "goes someplace"...
Code: [Select]
...
int 80h
checkerror "opening input file", exit
...
No great shakes, I'm just trying to encourage myself to check for possible errors. Every. Single. Time.

If you're interfacing with C anyway, I think "perror()" already does this(?). I don't think it's a bad idea...

Best,
Frank



Offline TightCoderEx

  • Full Member
  • **
  • Posts: 103
Re: sys_open 64 bit
« Reply #3 on: May 10, 2012, 07:04:23 PM »
You may wish to read up on calling conventions before trying to change the world.  ;)

Points to be definitely considered Rob, but for the most part, I am in my own little world and in the $MS environment I've even done this that could be considered Pascal etal stdcall compliant, if there could even be such a thing.  I do appreciate the input as the end of the day, I would like to design my apps to be upward compatible, and personal idiosyncrasies could be problematic.

Peter

Offline TightCoderEx

  • Full Member
  • **
  • Posts: 103
Re: sys_open 64 bit
« Reply #4 on: May 10, 2012, 07:10:04 PM »
I started by converting the comments in errno.h to strings, set up an array of pointers to 'em, and use the (negated) error number as an index into 'em.

Fundamentally we are thinking the same thing.  I will probably embellish upon the comments in errno.h and even toying with putting them into a text file as to facilitate other languages. Not a real priority though.  I'll post my effort in this thread and maybe you'd consider sharing how comparable it is to yours.

Peter