.Model tiny
.Code
Org 100h
Start:
mov ax,5300h
xor bx,bx
int 15h ; Check APM install
jnb @@1
jmp ExitSys
@@1:
cmp bx,504Dh ; sign
jz @@2
jmp ExitSys
@@2:
cmp ax,101h ; ver 1.1
jnb @@3
jmp ExitSys
@@3:
push cx
mov Version,ax
mov ax,5400h
xor bx,bx
int 2Fh ; power.exe install check?
xor ax,ax
cmp bx,504Dh
jnz @@4
push ax ; power.exe version
mov ax,5401h
mov bx,100h
int 2Fh ; set management enable
pop ax
jb @@4
inc ax
@@4:
pop cx
push ax
push bx
push cx
mov ax,5301h
xor bx,bx
int 15h ; real-mode interface
mov ax,530Eh
xor bx,bx
mov cx,101h
int 15h ; driver version
mov ax,530Fh
mov bx,1
mov cx,bx
int 15h ; engage power management
mov ax,5308h
mov bx,1
mov cx,bx
int 15h ; enable power mamagement
mov ax,5307h
mov bx,1
mov cx,3
int 15h ; power off
pop cx ; support device
test cl,8 ; BIOS power management disable?
jz @@5
push cx
mov ax,5308h
mov bx,1
xor cx,cx
int 15h ; system stand-by
pop cx
@@5:
test cl,10h ; BIOS power disengaged
jz @@6
mov ax,530Fh
mov bx,1
xor cx,cx
int 15h ; disengage power management
@@6:
mov ax,5304h
xor bx,bx
int 15h ; disconnect interface
pop bx
pop ax
test ax,ax
jz ExitSys
mov ax,5401h
mov bl,bh
mov bh,1
int 2Fh ; set APM firmware power manager enable
ExitSys:
mov ax,4c00h
int 21h
Version DW ?
End Start
.Code
Org 100h
Start:
mov ax,5300h
xor bx,bx
int 15h ; Check APM install
jnb @@1
jmp ExitSys
@@1:
cmp bx,504Dh ; sign
jz @@2
jmp ExitSys
@@2:
cmp ax,101h ; ver 1.1
jnb @@3
jmp ExitSys
@@3:
push cx
mov Version,ax
mov ax,5400h
xor bx,bx
int 2Fh ; power.exe install check?
xor ax,ax
cmp bx,504Dh
jnz @@4
push ax ; power.exe version
mov ax,5401h
mov bx,100h
int 2Fh ; set management enable
pop ax
jb @@4
inc ax
@@4:
pop cx
push ax
push bx
push cx
mov ax,5301h
xor bx,bx
int 15h ; real-mode interface
mov ax,530Eh
xor bx,bx
mov cx,101h
int 15h ; driver version
mov ax,530Fh
mov bx,1
mov cx,bx
int 15h ; engage power management
mov ax,5308h
mov bx,1
mov cx,bx
int 15h ; enable power mamagement
mov ax,5307h
mov bx,1
mov cx,3
int 15h ; power off
pop cx ; support device
test cl,8 ; BIOS power management disable?
jz @@5
push cx
mov ax,5308h
mov bx,1
xor cx,cx
int 15h ; system stand-by
pop cx
@@5:
test cl,10h ; BIOS power disengaged
jz @@6
mov ax,530Fh
mov bx,1
xor cx,cx
int 15h ; disengage power management
@@6:
mov ax,5304h
xor bx,bx
int 15h ; disconnect interface
pop bx
pop ax
test ax,ax
jz ExitSys
mov ax,5401h
mov bl,bh
mov bh,1
int 2Fh ; set APM firmware power manager enable
ExitSys:
mov ax,4c00h
int 21h
Version DW ?
End Start
Gemini 解答
技术解读:DOS 下的 APM 关机实现
该汇编代码的核心逻辑是利用 BIOS 提供的 APM (Advanced Power Management,高级电源管理) 接口来实现系统关机。在 2003 年左右的 DOS 环境下,这是实现软关机(Soft-off)的标准方式。
1. 核心原理:APM BIOS 接口
代码通过调用
INT 15h中断,并配合AX寄存器中的特定功能号来与主板 BIOS 的 APM 模块通信。
AX=5300h:检测 APM 是否安装。AX=5301h:建立实模式接口连接。AX=5307h:设置电源状态。其中BX=0001h(系统设备 ID)和CX=0003h(关机状态)是触发关机的关键参数。2. 代码逻辑分析与局限性
- 兼容性依赖:该程序高度依赖主板 BIOS 对 APM 1.1 或更高版本的支持。在 2000 年代初,虽然大多数 ATX 主板支持 APM,但部分老旧的 AT 主板或特定的 BIOS 设置(如 BIOS 中关闭了 APM 功能)会导致
INT 15h调用失败。INT 2Fh的误用与冗余:代码中混用了INT 2Fh(通常用于与POWER.EXE等 DOS 设备驱动程序通信)。在现代 DOS 环境(如纯 DOS 7.1 或 Windows 9x 的 DOS 模式)下,直接通过INT 15h即可完成关机,INT 2Fh的相关调用在多数情况下是多余的,甚至可能因驱动未加载而导致程序逻辑分支错误。- 状态机处理:代码尝试处理
530Eh(驱动版本)、530Fh(启用电源管理)等步骤,这在规范的 APM 实现中是严谨的,但在实际执行中,如果 BIOS 已经处于激活状态,部分调用可能会返回错误码,代码中通过jnb等跳转指令处理了部分异常,但并未对所有可能的错误返回码进行健壮性检查。3. 历史背景与技术误解
- “关机程序”的本质:在 DOS 时代,DOS 本身并不具备电源管理能力。所谓的“关机程序”本质上是向 BIOS 发送一个“请切断电源”的指令。如果硬件不支持 APM(例如早期的 AT 架构电源),无论程序如何编写,都无法实现软关机。
- APM 与 ACPI 的更迭:该程序仅适用于 APM 标准。随着 2000 年后 ACPI (Advanced Configuration and Power Interface) 标准的普及,APM 逐渐被淘汰。在较新的硬件上,APM 接口可能被 BIOS 模拟或完全缺失,导致该程序在后期硬件上可能失效。
4. 结论与建议
该程序在 2003 年的典型 ATX 兼容机环境下是有效的。其逻辑严谨地遵循了 APM 1.1 规范的握手流程。
注意事项:
- 环境限制:若在 Windows XP/7 等 NT 内核系统的“命令提示符”下运行,该程序会因权限不足或系统接管了电源管理而无法生效。
- 现代替代:在现代模拟器(如 DOSBox)中,该程序通常无效,因为 DOSBox 并不模拟完整的 APM BIOS 接口,而是通过特定的端口或宿主机交互来处理关机。
- 安全性:该程序直接操作硬件接口,若在运行过程中系统处于不稳定状态,强制执行
5307h可能导致磁盘缓存未及时回写,从而引发数据丢失。在实际应用中,应确保所有文件句柄已关闭。
ko20010214
=================================
大功告成,打个Kiss!
ko20010214@MSN.com
神州优雅Q300C
Intel CeleronM 370处理器 | 256MbDDR内存
40G硬盘 | USB2.0 | IEEE 1394
13.3 ' WXGA 宽屏(16:10) | COMBO光驱
10/100M网卡 | 四合一读卡器
=================================
大功告成,打个Kiss!
ko20010214@MSN.com
神州优雅Q300C
Intel CeleronM 370处理器 | 256MbDDR内存
40G硬盘 | USB2.0 | IEEE 1394
13.3 ' WXGA 宽屏(16:10) | COMBO光驱
10/100M网卡 | 四合一读卡器


