Johnrad
Johnrad

Reputation: 2655

x86 Assembly Language - Terminate Program

I am sure this is very trivial for most, but I am not very familiar with x86 assembly language. I am just trying to teach myself.

I am in windows. And everywhere I read, I was told to use INT 21 to return to the operating system. Which this exits the program, but I get an error saying Unhandled exception at 0x003d1313 in Assignment1.exe: 0xC0000005: Access violation reading location 0xffffffff.

Thanks!

Upvotes: 3

Views: 18134

Answers (3)

Jens Björnhager
Jens Björnhager

Reputation: 5648

If your stack is balanced, the easiest way to exit your program is

retn

Upvotes: 3

JosephH
JosephH

Reputation: 8825

On Windows, if you are using a formal assembler (e.g. MASM), you can simply call the following:

.386 
.model flat, stdcall 
option casemap:none 
include \masm32\include\windows.inc 
include \masm32\include\kernel32.inc 
includelib \masm32\lib\kernel32.lib 
.data 
.code 
start: 
        invoke ExitProcess,0 
end start

If you are not using any assembler and want to simply execute a chunk of binary code, execute the following:

push xxx
push -1
push 0
mov eax, yyy
mov edx, 7FFE0300
call dword ptr ds:[edx]

where xxx is the exit code of the process and yyy is the system call number for NtTerminateProcess ( use http://www.pediy.com/document/Windows_System_Call_Table/Windows_System_Call_Table.htm to determine the call number for appropriate OS. it's 0x172 for Windows 7)

Upvotes: 7

paulsm4
paulsm4

Reputation: 121881

The answer depends entirely on what operating system you're using :)

Here's an example using "int 0x80" on Linux:

movl $1, %eax
movl $0, %ebx
int $0x80

This Wikipedia link gives you more options:

http://en.wikipedia.org/wiki/Exit_%28operating_system%29

Upvotes: 4

Related Questions