Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

"Upon return from a system call, eax will contain the return value. If an error occurs, eax will contain a negative value, with the absolute value indicating the error."

I've always wondered about this. Why do errors always return as negative?



If I recall, DEC's VMS operating system had an error return convention where the high bit was negative on error, and the other bits meant different things: system the error occurred in, specific error, etc.

I believe it was a tradition to have a high bit indicate error at the time Unix was originated.


OpenVMS (née VMS) function calls and signals use the low bit set to indicate success, and the low bit clear to indicate failure; the condition value format.

The high bits of the condition value format can also tell you more about what component returned the error, and there were a few control flags up in the highest bits, including bit 31 (the sign bit) which inhibited message display.

Somewhat unusually, the low three bits of the condition status value are used to indicate varying levels of success or failure; the success (0b001), informational (0b011), warning (0b000), error (0b010) and severe/fatal (0b100) errors.

VAX had the BLBS and BLBS instructions for testing and branching on the low bit, too.

These success- and failure-testing instructions were separate from the BEQL/BEQU, BNEQ/BNEQU, BGTR/BGRTU, BGEQ/BGEQU, BLSS/BLSSU and BLEQ/BLEQU that looked at whether the signed or unsigned value was positive, zero or negative, or zero or greater, or zero or less. A condition value that had been displayed or otherwise inhibited was switched to a negative value.

Yes, VAX had a lot of instructions.

This usage of the low bit also led to the semi-inevitable "Success on VMS being odd" in-joke.


Because positive values contain the actual return value, e.g. the number of bytes read, etc.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: