0

I am playing with my first program in assembly, and I've got it to return status codes that can be displayed with echo $?. Out of curiosity, I tried to see what would happen if I returned a negative integer as my status code, like so:

.section .data
.section .text
.globl _start
_start:

mov $1, %eax # set syscall to exit

mov $0, %ebx # set status code to 0
subl $1, %ebx # subtract 1 from it!


int $0x80 

When I assemble and run my program I get the following output:

hc06@HC06:~$ echo $?
255

I'm a complete infant in assembly and I'm sure this is obvious behavior to the rest of you, but I'm very curious why this happens. Thanks!

Alex V
  • 3,416
  • 2
  • 33
  • 52
  • because Linux exit status is an 8-bit unsigned integer. – Peter Cordes Nov 24 '18 at 02:01
  • @PeterCordes Okay, so when it gets the first 8 bits of -1 and reads that as an unsigned integer, the result is 255? – Alex V Nov 24 '18 at 02:05
  • @AlejandroAlvarado - correct, the return value is only 8 bits, even though in C, the return value in main() is specified as an int. The return value is also only 8 bits for MSDOS and Windows, it is a legacy thing going back to some mini-computer system (PDP-11?). – rcgldr Nov 24 '18 at 02:39
  • Yup, 0 - 1 wraps around to the maximum unsigned value, with all the bits set. Was that the part you were wondering about? Plain old base 2 math. – Peter Cordes Nov 24 '18 at 03:06
  • note that [it's better to use xor to set a register to zero](https://stackoverflow.com/q/33666617/995714) – phuclv Nov 24 '18 at 03:49

0 Answers0