Exiting a Program via INT 80h

There is a second int 8 0h instruction in eatsyscall.asm, and it has a humble but crucial job: shutting down the program and returning control to Linux. This sounds simpler than it is, and once you understand Linux internals a little more, you'll begin to appreciate the work that must be done both to launch a process and to shut one down.

From your own program's standpoint, it's fairly simple: You place the number of the sys_exit service in EAX, place a return code in EBX, and then execute int 8 0h:

mov eax,1 mov ebx,0 int 80H

Specify Exit syscall Return a code of zero

Make the syscall to terminate the program

The return code is a numeric value that you can define however you want. Technically, there are no restrictions on what it is (aside from having to fit in a 32-bit register), but by convention a return value of 0 means ''everything worked OK; shutting down normally.'' Return values other than 0 typically indicate an error of some sort. Keep in mind that in larger programs, you have to watch out for things that don't work as expected: a disk file cannot be found, a disk drive is full, and so on. If a program can't do its job and must terminate prematurely, it should have some way of telling you (or, in some cases, another program) what went wrong. The return code is a good way to do this.

...and then jumps to the instruction at that address, which is the one immediately after the INT 80h.

The IRET Instruction pops the return address off the stack...

Vector Table

...and then jumps to the instruction at that address, which is the one immediately after the INT 80h.

The IRET Instruction pops the return address off the stack...

Vector Table

Vector 80h

Figure 8-6: Returning home from an interrupt

Exiting this way is not just a nicety. Every program you write must exit by making a call to sys_exit through the kernel services dispatcher. If a program just ''runs off the edge'' it will in fact end, but Linux will hand up a segmentation fault and you'll be none the wiser as to what happened.

Was this article helpful?

0 0

Post a comment