8086 A86 v4.05 COM|内存Dump工具 完整开发文档

本文整合:内存地址显示原理、CALL/RET子程序调用规范、Dump工具迭代全版本源码;全部代码规避A86内置单字母保留符号(a/b/c/d/s/t),可直接编译运行于 DOSBox / 实模式DOS

一、基础:8086实模式内存地址 与 打印实现

1.1 地址基础概念

mov bx, mybuf   ; bx = mybuf 的偏移地址(编译期直接计算)

1.2 通用打印十六进制子程序(基础工具库)

; putchar DL=ASCII字符输出
putchar:
        push ax
        mov ah,02h
        int 21h
        pop ax
        ret

; print_crlf 输出回车换行
print_crlf:
        push dx
        mov dl,0Dh
        call putchar
        mov dl,0Ah
        call putchar
        pop dx
        ret

; print_hex16:AX=16位数值,输出4位大写十六进制
print_hex16:
        push ax
        push bx
        push cx
        push dx
        mov cx,4
ph16_lp:
        rol ax,4
        mov dx,ax
        and dx,0Fh
        mov bx,dx
        mov dl,[hex_table+bx]
        mov ah,02h
        int 21h
        dec cx
        jnz ph16_lp
        pop dx
        pop cx
        pop bx
        pop ax
        ret

; print_hex8:AL=8位字节,输出2位大写十六进制
print_hex8:
        push ax
        push bx
        push cx
        push dx
        mov cx,2
ph8_lp:
        rol al,4
        mov dx,ax
        and dx,0Fh
        mov bx,dx
        mov dl,[hex_table+bx]
        mov ah,02h
        int 21h
        dec cx
        jnz ph8_lp
        pop dx
        pop cx
        pop bx
        pop ax
        ret

hex_table db '0123456789ABCDEF'

1.3 使用示例:打印变量完整逻辑地址

mov ax,cs
call print_hex16    ;打印段地址
mov dl,':'
call putchar
mov ax,va
call print_hex16    ;打印变量偏移地址
call print_crlf

二、8086 CALL / RET 子程序调用原理与工程技巧

2.1 近调用 vs 远调用

2.2 三种标准参数传递方案

传参方式 适用场景 特点
寄存器传参 少参数工具子程序(打印、计算) 速度最快,本项目打印函数均采用
栈传参 多参数,模拟C/BASIC函数 调用者使用 add sp,N 手动平衡栈
全局内存变量 老式BASIC GOSUB 风格 无需操作栈/寄存器,全局共享数据

2.3 子程序现场保护规范

原则:子程序内部修改的通用寄存器,入口全部push保存,ret前恢复;作为返回值的寄存器不保存

my_sub:
        push ax
        push bx
        push cx
        push dx
        push si
        push di
        push bp
        ; 业务逻辑代码写此处
        pop bp
        pop di
        pop si
        pop dx
        pop cx
        pop bx
        pop ax
        ret

2.4 常见致命坑

三、内存Dump工具 迭代版本源码(仿 DEBUG D 命令)

3.1 基础版Dump:固定128字节,仅支持偏移地址

; memdump.asm A86 v4.05 .COM 基础内存Dump
; 输入4位十六进制偏移,dump 128字节(8行×16)内存
org 100h
seg_start:
jmp main

hex_table   db '0123456789ABCDEF'
prompt      db 0Dh,0Ah,'Input 4-digit HEX offset > $'
crlf        db 0Dh,0Ah

in_buf      db 6 dup(0)
dump_buf    db 16 dup(0)

;==== 工具子程序 putchar print_crlf print_hex16 print_hex8 同上 ====
read_str:
        push ax
        mov ah,0Ah
        int 21h
        pop ax
        ret

hex4_to_ax:
        push bx
        push cx
        push dx
        xor ax,ax
        mov cx,4
h4_loop:
        mov bl,[si]
        cmp bl,'9'
        jbe h4_num
        sub bl,7
h4_num:
        sub bl,'0'
        shl ax,4
        or al,bl
        inc si
        dec cx
        jnz h4_loop
        pop dx
        pop cx
        pop bx
        ret

dump_line:
        push ax
        push bx
        push cx
        push dx
        push si
        push di
        mov ax,si
        call print_hex16
        mov dl,':'
        call putchar
        mov dl,' '
        call putchar
        mov di,0
        mov cx,16
dl_hex:
        mov al,[si]
        mov [dump_buf+di],al
        call print_hex8
        mov dl,' '
        call putchar
        inc si
        inc di
        dec cx
        jnz dl_hex
        mov dl,'|'
        call putchar
        mov dl,' '
        call putchar
        mov si,dump_buf
        mov cx,16
dl_ascii:
        mov al,[si]
        cmp al,20h
        jb dl_dot
        cmp al,7Eh
        ja dl_dot
        mov dl,al
        jmp dl_put
dl_dot:
        mov dl,'.'
dl_put:
        call putchar
        inc si
        dec cx
        jnz dl_ascii
        call print_crlf
        pop di
        pop si
        pop dx
        pop cx
        pop bx
        pop ax
        ret

main:
        mov dx,prompt
        mov ah,09h
        int 21h
        mov dx,in_buf
        mov byte [in_buf],5
        call read_str
        call print_crlf
        lea si,[in_buf+2]
        call hex4_to_ax
        mov si,ax
        mov bx,8
dump_all:
        call dump_line
        dec bx
        jnz dump_all
        ret
编译命令:a86 memdump.asm -o memdump.com

3.2 增强版1:支持段:偏移地址输入 + 自定义Dump字节数

; memdump_ex.asm A86 v4.05 COM
; 支持 xxxx / xxxx:xxxx 地址;自定义dump字节总数
org 100h
seg_start:
jmp main

hex_table   db '0123456789ABCDEF'
prompt_addr db 0Dh,0Ah,'Addr(xxxx:xxxx or xxxx) > $'
prompt_len  db 'Dump byte count > $'
crlf        db 0Dh,0Ah

in_buf      db 9 dup(0)
dump_buf    db 16 dup(0)
seg_val     dw 0
off_val     dw 0
dump_total  dw 0

;==== putchar print_crlf print_hex16 print_hex8 read_str hex4_to_ax 同上 ====
dec3_to_ax:
        push bx
        push cx
        xor ax,ax
        mov cx,3
d3_loop:
        mov bl,[si]
        cmp bl,0Dh
        je d3_end
        sub bl,'0'
        mov bx,bx
        mov dx,ax
        shl ax,1
        shl ax,1
        add ax,dx
        shl ax,1
        add ax,bx
        inc si
        dec cx
        jnz d3_loop
d3_end:
        pop cx
        pop bx
        ret

dump_line:
        push ax
        push bx
        push cx
        push dx
        push si
        push di
        mov ax,es
        call print_hex16
        mov dl,':'
        call putchar
        mov ax,si
        call print_hex16
        mov dl,' '
        call putchar
        mov di,0
dl_hex:
        mov al,[es:si]
        mov [dump_buf+di],al
        call print_hex8
        mov dl,' '
        call putchar
        inc si
        inc di
        dec cx
        jnz dl_hex
        mov ax,di
fill_space:
        cmp ax,16
        jae fill_end
        mov dl,' '
        call putchar
        mov dl,' '
        call putchar
        mov dl,' '
        call putchar
        inc ax
        jmp fill_space
fill_end:
        mov dl,'|'
        call putchar
        mov dl,' '
        call putchar
        mov si,dump_buf
        mov cx,di
dl_ascii:
        mov al,[si]
        cmp al,20h
        jb dl_dot
        cmp al,7Eh
        ja dl_dot
        mov dl,al
        jmp dl_put
dl_dot:
        mov dl,'.'
dl_put:
        call putchar
        inc si
        dec cx
        jnz dl_ascii
        call print_crlf
        pop di
        pop si
        pop dx
        pop cx
        pop bx
        pop ax
        ret

main:
        mov dx,prompt_addr
        mov ah,09h
        int 21h
        mov dx,in_buf
        mov byte [in_buf],8
        call read_str
        call print_crlf
        lea si,[in_buf+2]
        mov ax,cs
        mov [seg_val],ax
        xor cx,cx
find_colon:
        mov al,[si+cx]
        cmp al,0Dh
        je no_colon
        cmp al,':'
        je has_colon
        inc cx
        jmp find_colon
has_colon:
        lea si,[in_buf+2]
        call hex4_to_ax
        mov [seg_val],ax
        lea si,[in_buf+2+cx+1]
        call hex4_to_ax
        mov [off_val],ax
        jmp addr_ok
no_colon:
        lea si,[in_buf+2]
        call hex4_to_ax
        mov [off_val],ax
addr_ok:
        mov dx,prompt_len
        mov ah,09h
        int 21h
        mov dx,in_buf
        mov byte [in_buf],4
        call read_str
        call print_crlf
        lea si,[in_buf+2]
        call dec3_to_ax
        mov [dump_total],ax
        mov ax,[seg_val]
        mov es,ax
        mov si,[off_val]
        mov cx,[dump_total]
dump_loop:
        cmp cx,0
        je dump_finish
        mov bx,16
        cmp cx,bx
        jb less16
        mov bx,16
less16:
        push cx
        mov cx,bx
        call dump_line
        pop cx
        sub cx,bx
        add si,bx
        jmp dump_loop
dump_finish:
        ret

3.3 最终完整版:段:偏移 + 自定义长度 + 任意按键中止Dump(推荐使用)

; memdump_ex2.asm A86 v4.05 COM Dump 增强:任意按键中止输出
; 输入格式:地址 xxxx 或 xxxx:xxxx,再输入十进制总字节数
org 100h
seg_start:
jmp main

;===== 常量字符串 =====
hex_table   db '0123456789ABCDEF'
prompt_addr db 0Dh,0Ah,'Addr(xxxx:xxxx or xxxx) > $'
prompt_len  db 'Dump byte count > $'
space       db ' '
crlf        db 0Dh,0Ah

;===== 全局缓冲区 =====
in_buf      db 9 dup(0)   ;最大输入 8字符 xxxx:xxxx
dump_buf    db 16 dup(0) ;每行16字节缓存
seg_val     dw 0
off_val     dw 0
dump_total  dw 0

;==================== 工具子程序 ====================
; putchar DL=ASCII
putchar:
        push ax
        mov ah,02h
        int 21h
        pop ax
        ret

print_crlf:
        push dx
        mov dl,0Dh
        call putchar
        mov dl,0Ah
        call putchar
        pop dx
        ret

; print_hex16 AX=16bit → 4位大写HEX
print_hex16:
        push ax
        push bx
        push cx
        push dx
        mov cx,4
ph16_lp:
        rol ax,4
        mov dx,ax
        and dx,0Fh
        mov bx,dx
        mov dl,[hex_table+bx]
        mov ah,02h
        int 21h
        dec cx
        jnz ph16_lp
        pop dx
        pop cx
        pop bx
        pop ax
        ret

; print_hex8 AL=8bit → 2位大写HEX
print_hex8:
        push ax
        push bx
        push cx
        push dx
        mov cx,2
ph8_lp:
        rol al,4
        mov dx,ax
        and dx,0Fh
        mov bx,dx
        mov dl,[hex_table+bx]
        mov ah,02h
        int 21h
        dec cx
        jnz ph8_lp
        pop dx
        pop cx
        pop bx
        pop ax
        ret

; DOS 0Ah 字符串读取 DX=缓冲区首址
read_str:
        push ax
        mov ah,0Ah
        int 21h
        pop ax
        ret

; hex4_to_ax: SI指向4位HEX字符串 → AX数值
hex4_to_ax:
        push bx
        push cx
        push dx
        xor ax,ax
        mov cx,4
h4_loop:
        mov bl,[si]
        cmp bl,'9'
        jbe h4_num
        sub bl,7
h4_num:
        sub bl,'0'
        shl ax,4
        or al,bl
        inc si
        dec cx
        jnz h4_loop
        pop dx
        pop cx
        pop bx
        ret

; 十进制字符串转AX(最多3位 0~255)
dec3_to_ax:
        push bx
        push cx
        xor ax,ax
        mov cx,3
d3_loop:
        mov bl,[si]
        cmp bl,0Dh
        je d3_end
        sub bl,'0'
        mov bx,bx
        mov dx,ax
        shl ax,1
        shl ax,1
        add ax,dx
        shl ax,1
        add ax,bx
        inc si
        dec cx
        jnz d3_loop
d3_end:
        pop cx
        pop bx
        ret

; check_key: 检测是否有按键按下
; AH=01h INT16h:ZF=0 代表有按键;有按键则读取清除缓冲区
check_key:
        push ax
        mov ah,01h
        int 16h
        jz ck_exit
        ;有按键,读取掉扫描码,清空键盘缓冲区
        mov ah,00h
        int 16h
ck_exit:
        pop ax
        ret

;==================== Dump一行 最多16字节 ====================
; ES:SI = 起始内存地址,CX=本行实际打印字节数
dump_line:
        push ax
        push bx
        push cx
        push dx
        push si
        push di

        ;打印 段:偏移
        mov ax,es
        call print_hex16
        mov dl,':'
        call putchar
        mov ax,si
        call print_hex16
        mov dl,' '
        call putchar

        ;读取字节存入dump_buf,打印HEX
        mov di,0
dl_hex:
        mov al,[es:si]
        mov [dump_buf+di],al
        call print_hex8
        mov dl,' '
        call putchar
        inc si
        inc di
        dec cx
        jnz dl_hex

        ;补齐本行不足16字节的空格,对齐
        mov ax,di
fill_space:
        cmp ax,16
        jae fill_end
        mov dl,' '
        call putchar
        mov dl,' '
        call putchar
        mov dl,' '
        call putchar
        inc ax
        jmp fill_space
fill_end:

        ;打印分隔 |
        mov dl,'|'
        call putchar
        mov dl,' '
        call putchar

        ;打印ASCII,不可见替换 .
        mov si,offset dump_buf
        mov cx,di
dl_ascii:
        mov al,[si]
        cmp al,20h
        jb dl_dot
        cmp al,7Eh
        ja dl_dot
        mov dl,al
        jmp dl_put
dl_dot:
        mov dl,'.'
dl_put:
        call putchar
        inc si
        dec cx
        jnz dl_ascii

        call print_crlf
        pop di
        pop si
        pop dx
        pop cx
        pop bx
        pop ax
        ret

;==================== 主程序 ====================
main:
        ;==== 1. 输入地址 xxxx:xxxx / xxxx ====
        mov dx,offset prompt_addr
        mov ah,09h
        int 21h
        mov dx,offset in_buf
        mov byte [in_buf],8   ;最大8字符输入
        call read_str
        call print_crlf

        lea si,[in_buf+2]
        mov ax,cs
        mov [seg_val],ax  ;默认段=CS

        ;查找 ':'
        xor cx,cx
find_colon:
mov bx,si
add bx,cx
mov al,[bx]

        cmp al,0Dh
        je no_colon
        cmp al,':'
        je has_colon
        inc cx
        jmp find_colon

has_colon:
        ;前4字符 = 段
        lea si,[in_buf+2]
        call hex4_to_ax
        mov [seg_val],ax
        ;偏移从 :+1 开始
mov bx, cx
lea si, [in_buf + 3]
add si, bx
        call hex4_to_ax
        mov [off_val],ax
        jmp addr_ok

no_colon:
        ;无冒号,全部作为偏移,段保持CS
        lea si,[in_buf+2]
        call hex4_to_ax
        mov [off_val],ax
addr_ok:

        ;==== 2. 输入Dump字节长度(十进制) ====
        mov dx,offset prompt_len
        mov ah,09h
        int 21h
        mov dx,offset in_buf
        mov byte [in_buf],4
        call read_str
        call print_crlf
        lea si,[in_buf+2]
        call dec3_to_ax
        mov [dump_total],ax

        ;==== 3. 设置ES=段基址 ====
        mov ax,[seg_val]
        mov es,ax
        mov si,[off_val]
        mov cx,[dump_total]

dump_loop:
        cmp cx,0
        je dump_finish

        ;【新增】每次循环先检测按键,有按键直接退出
        call check_key
        jnz dump_finish

        ;每行最多16字节
        mov bx,16
        cmp cx,bx
        jb less16
        mov bx,16
less16:
        push cx
        mov cx,bx
        call dump_line
        pop cx
        sub cx,bx
        add si,bx
        jmp dump_loop

dump_finish:
        ret
编译命令:a86 memdump_ex2.asm -o memdump_ex2.com

使用说明

四、可选扩展开发方向