Search code examples
linuxassemblysystem-callsexitexitstatus

Exit syscall not returning contents of %ebx?


On Ubuntu Linux, 32 bit, x86 processor, compiling with GAS

I've run into a very strange issue, wherein the contents of the %ebx register isn't being returned as the status code of my program when I make the exit syscall. Here is the relevant code. Here is a dump of the registers just before the syscall:

eax            0x1  1
ecx            0x804a00c  134520844
edx            0xff  255
ebx            0x159  345
esp            0xbffff3bc  0xbffff3bc
ebp            0xbffff3c0  0xbffff3c0
esi            0x0  0
edi            0x0  0
eip            0x80480c6  0x80480c6 <num_loop_end+5>
eflags         0x246  [ PF ZF IF ]
cs             0x73  115
ss             0x7b  123
ds             0x7b  123
es             0x7b  123
fs             0x0  0
gs             0x0  0

And the relevant assembly code:

   0x080480c1 <+0>:  mov    $0x1,%eax
=> 0x080480c6 <+5>:  int    $0x80

The register dump occurred at the => above. However, instead of returning 345 as the status code, my program is exiting with code 89, or 0131 in octal. What might be causing this. Let me know if I should post more code, although I don't know how that could affect this issue.


Solution

  • This is perfectly normal behaviour, read the man page for exit:

    The exit() function causes normal process termination and the value of status & 0377 is returned to the parent (see wait(2)).

    0377 is an octal value, and it's equivalent decimal value is 255. Therefore any value greater than 255 is going to end up less than that from the bitwise AND operation:

    345 AND 255 = 89
    

    That's where the value 89 comes from.