当前位置: 代码迷 >> 汇编语言 >> 汇编程序结束的时候ret不就行了,干嘛还要mov ah,4ch,int 21h?该如何解决
  详细解决方案

汇编程序结束的时候ret不就行了,干嘛还要mov ah,4ch,int 21h?该如何解决

热度:7200   发布时间:2013-02-26 00:00:00.0
汇编程序结束的时候ret不就行了,干嘛还要mov ah,4ch,int 21h?
我在主程序结尾ret就能返回dos

干嘛还要
mov ah,4ch
int 21h
这样麻烦的调用来返回dos

这两种方式到底有什么不同么?

------解决方案--------------------------------------------------------
ret在子函数里不能用来返回DOS。
另外,如果你要在其他程序里检查你的程序的errorlevel,那么你需要用int32设置退出码
------解决方案--------------------------------------------------------
用什么方法结束程序,这个全是有系统对此操作上的约定而来。对 dos 来说,调用 int21h 的 4ch 功能是比较高级、被推荐的方法,因为它没有特别的限制,还可用设置程序结束状态供可能的后续程序使用,即上面朋友提及的 errorlevel 。
此外,int20h 、int21h 的 00 功能 也是可用结束程序的,不过,要求当前的 cs 是指向程序的 psp 的。如果一定想用 retf 来进行的话,就是要 retf 到 psp:0000 处,那里是个 itn20h 指令,所以实际也就是间接地对 int20h 的调用。
------解决方案--------------------------------------------------------
最重要的地方是堆栈切换,这对任何一个内核来说都是难点和讲究技巧的地方。从用户程序返回操作系统时,通常需要切换堆栈,用ret肯定是有问题的。也许int 20h和int 21h能够安全地切换堆栈。但具体操作系统是怎么做的,还有待研究。不过,MS-DOS太老旧了,似乎没必要纠结它了。
------解决方案--------------------------------------------------------
Inp.:
AH = 4Dh
Return: AH = termination type
00h normal (INT 20,INT 21/AH=00h, or INT 21/AH=4Ch)
01h control-C abort
02h critical error abort
03h terminate and stay resident (INT 21/AH=31h or INT 27)
AL = return code
CF clear
Notes: the word in which DOS stores the return code is cleared after being
read by this function, so the return code can only be retrieved once

COMMAND.COM stores the return code of the last external command it
executed as ERRORLEVEL
this call should not be used if the child was started with AX=4B04h;
use AH=8Ah instead