What is the difference between using int 0x20 and int 0x21 / ah = 0x4C to exit a 16-bit build program?

At different times I used both

int 0x20 

and

 mov ah, 0x4c int 0x21 

as ways to complete a 16-bit build program.

But what is the difference between the two?


EDIT : Thanks for your comments. Following Alexey’s link to the PSP (program segment prefix), this nugget is from Microsoft MASM support .

The article seems to have more differences than just return codes.

We are pleased to award the accepted answer to everyone who can tie them together more specifically.

+4
source share
3 answers

Firstly, some background. DOS uses 21h interrupt for its system calls. AH is used to demultiplex various INT 21h functions. When a program is executed, DOS sets 256 bytes in front of it, known as PSP (program segment prefix), which contain information about the process.

The original exit function in DOS INT 21/AH=00 . Now, apparently, the DOS developers decided that returning from the program should be a way to exit the program (did this come from CP / M?). RET (nearby) pops a word from the stack and jumps to it. So, when a program is created, its stack begins with the word 0000 . This is the beginning of the PSP. So, at the beginning of the PSP there is code to end the program. To keep this code small, INT 20h acts as an alias for MOV AH,00h ; INT 21h MOV AH,00h ; INT 21h .

[Edit: this can be seen in the screenshot below.]

DOS 2.0 took many things from Unix, including return codes. So, there is a new function INT 21h, INT 21h/AH=4ch , which takes a return code to return it to the OS. This function is also designed to work with EXE files (AFAIR is also new in DOS 2.0), which can have several segments. The previous exit functions (INT 20h and INT 21h / 00) assume that CS is the same as when starting the program in the COM program, that is, it indicates 256 bytes before the program in PSP.

[Edit: Illustration of DOS program structure, using <code> debug </code>

Historical Note: There were 3 ways to exit the program on CP / M:

  • BDOS 0 function call (equivalent to INT 21 AH = 00h, DOS 0 function)
  • Go to WBOOTF location at 00:00 (PSP offset 000h equivalent)
  • returning

The WBOOTF location consisted of 3 bytes: 1 byte for the transition, 2 bytes for the purpose of the transition (WBOOT function in BDOS).

In early versions of CP / M, calling BDOS 0 or switching to WBOOT led to a reboot of the CP / M parts from the disk (warm boot) and some initialization of the OS, which should subsequently be launched; while RETurning returns directly to CCP (Console Command Processor, the equivalent of COMMAND.COM), which then requests the next command line. AFAIU, CP / M 3 usually loaded into the ROM and returned to the WBOOT location, causing a reboot of the OS parts from the ROM.

+11
source

Later, int 0x21, you can specify a return code.

The return code is placed in the AL register.

http://spike.scu.edu.au/~barry/interrupts.html#ah4c

+6
source

This is what I know.

 MOV AH, 0x4C INT 0x21 

Finishes executable exe file, exe file must be completed this way because codeegment register CS. Correct me if I am wrong, but if you finish the COM files this way, you will get unexpected results (crashes, freezes, reboots, etc.). therefore

 INT 0x20 

Completes a COM file.

CS is the same as when starting the program in the COM program, that is, it points to the PSP 256 bytes before the program. (CodeSegment InstructionPointer CS: IP, CS contains codes). Yes, we are talking about registers, variables are like a cabinet, I work the way you can put something in a box correctly. AX = 0000 BX = 0000 CX = 0000 (CX consists of CL and CH), etc.

I thought that COM files are usually limited to 64K ALWAYS. The second reason I thought was because COM files do not have DATA SEGMENT, they have data, but they are in the same segment as the code. I thought they had no segments except CODE. All data in the COM file is stored within 64K. EXE files have a segment, some EXE files can have more segments (CS: IP) when using the correct memory model (see Intel memory model).

  • Tiny * CS=DS=SS
  • Small DS=SS
  • Medium DS=SS , multiple code segments
  • Compact single code segment, multiple data segments
  • Large segment of multiple codes and data
  • Huge segments of code and data; single array can be> 64 kb

EXE files are limited to 64 KB using the SMALL memory model. When using larger memory models and distant 32-bit pointers, you can use more than 64 KB (still limited). I thought it was a "trick".

Now everyone is complaining about me why you should use EXE files. The above are the reasons. The memory model comes from Wikipedia. For those who don't care, I found out from professionals. Peter Norton and John Socha. From Norton Utilities (the man behind the Norton Commander for DOS team). He had a book about assembly, for example, "Assembly for IBM-PC." You have to read it, he explains it best. He was a good teacher to me. Microsoft CodeView has clarified a lot for me. Programming in DOS C? Turbo C 2.0 is the best you can get. Oh, and I'm not programming anymore.

+1
source

Source: https://habr.com/ru/post/1436242/


All Articles