Compilation Basics: Program return to the operating system

Source: Internet
Author: User
Tags execution

The last one talked about the pseudo instructions of the next assembly, I also learn to compile not how long time, so unavoidably some wrong place, if you found the error in the reading process, but also hope to do not teach Ah, this article we look at the assembly language program returned to the operating system this part is how to achieve.

We all know that Assembly language source program to go through the assembly and connection two steps to generate a can be directly executed in DOS EXE files, in DOS execution of this file, DOS like calling subroutine, the control to it, and this EXE file after the completion of the program as a subroutine to return to the caller as DOS,

First, with the 4CH system function call implementation return

The 4CH system call function is to end the program execution return DOS, so you can use it to complete the program return to the operating system by using the following command at the end of the program:

MOV AH, 4CH

INT 21H

But not any program can use this method to return to DOS, only the main program to return DOS to use this way, and the calling subroutine should return to the caller rather than the system, so can not be returned with 4CH implementation. Here is a way to return to the calling program.

Second, with the program prefix implementation of the return

When executing EXE in DOS, DOS creates a 256-byte program prefix (PSP) after command.com the staging part and gives control to it after loading the EXE program. PSP content consists of 3 parts information: The information used by the loading program and the DOS connection, the parameters for the loading program and the information for use by DOS itself.

In the DOS transfer control, the code snippet (CS) is pointed to the code snippet of the EXE program, the SS points to the stack segment, and the DS and ES do not point to the data segment and the additional segment of the user program, but point to the PSP.

In the PSP first address (first to second byte) there is a soft interrupt instruction "INT 20H" (I will talk about the interrupt section later), and the function of this instruction is to end the user program returned to the operating system, so you can use the program segment prefix to return to DOS, the following steps:

A, weave the program into a process, and set the Type property of the procedure to far

B, the PSP's starting logical address into the stack, the "INT 20H" instructions to the address of the stack. Since the EXE file is loaded into memory, both DS and Es point to the PSP, so the command "PUSH DS" can be implemented to push the PSP's starting logical address onto the stack.

C, at the end of the program is programmed to use the return instruction RET, so that the system will be saved in the stack of the PSP starting logical address pop-up to the CS and IP, and then go to the PSP at the beginning of the address of the instructions int 20H, so as to achieve the purpose of return to DOS.

This article is from the "Late Evening" blog, please be sure to keep this source http://yiluohuanghun.blog.51cto.com/3407300/939717

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.