ASM EXE程序16位:更改内存大小时出错

时间:2017-04-04 18:27:10

标签: assembly tasm 16-bit

我用 SMALL 模型编写 EXE 程序 我想借助我的程序加载其他程序。我读到,首先我必须释放一些记忆。我使用DOS 4Ah INT 21h中断。但是当我使用它时,我在AX中有错误7(控制单元内存被破坏)。我做错了什么?

;-------------------MACRO-----------------
println MACRO info
    push ax
    push dx

    mov ah, 09h
    mov dx, offset info
    int 21h

    ;print new line
    mov dl, 10
    mov ah, 02h
    int 21h

    mov dl, 13
    mov ah, 02h
    int 21h

    pop dx
    pop ax
ENDM
;-----------------end macro----------------

.model small

.stack 100h

.data

initToRunErrorText db "Bad init to run other programs", '$'

myDataEnd db '0'

.code

main:
    mov ax, @data
    mov es, ax
    mov ds, ax

    call initToRun

    mov ah, 4Ch
    int 21h

;   Result
;       ax = 0 => all is good
;       ax != 0 => we have an error
initToRun PROC
    push ax bx

    mov ah, 4Ah
    mov bx, offset myDataEnd + 100h
    shr bx, 4
    add bx, 2
    int 21h

    jnc initToRunAllGood

    add ax, '0'
    mov dl, al
    mov ah, 06h
    int 21h

    mov ax, 1
    println initToRunErrorText

    jmp initToRunEnd

initToRunAllGood:
    mov ax, 0

initToRunEnd:
    pop bx ax
    ret
ENDP

program_length equ $-main

end main

对于编译,我使用TASM 16位与DOSBox 0.74

1 个答案:

答案 0 :(得分:2)

您无需在.exe程序中释放内存。只有.com程序会保留所有内存。确定.exe程序的实际最后一个字节并找到分配块的段是相当复杂的。顺便说一句:在.com程序中,你必须调整堆栈指针!

正如@RossRidge所提到的,一些链接器(例如TLINK)在头部写入一个信息来分配最大内存。这很烦人。

您可以使用MASM套件中包含的Microsoft exemod之类的工具(版本< 6)修改此标题项。套件可下载here。 exemod实用程序可以在DISK4中找到。使用它像:

exemod <exefile> /max 1

另一种选择是调整程序内部分配的内存。首先制作一个由父程序执行的简单程序HELLO.EXE

.MODEL small
.STACK          ; default: 1000h
.DATA
    hello db "Hello world", 13, 10, "$"
.CODE
main PROC
    mov ax, @data
    mov ds, ax
    mov dx, OFFSET hello
    mov ah, 09h
    int 21h
    mov ax, 4C00h
    int 21h
main ENDP
END main

现在是父母:

.MODEL small

.STACK

.DATA
    hello db "HELLO.EXE", 0

    params label word
        dw 0
        dw OFFSET command_line, SEG command_line
        dw 0ffffh,0ffffh ; fcb1
        dw 0ffffh,0ffffh ; fcb2

    command_line db 0,13

.CODE

free_memory PROC
    mov ax, sp              ; SS:SP -> nearly the end of program
    shr ax, 4
    mov bx, ss
    add bx, ax
    add bx, 2               ; BX = a paragraph beyond program
    mov ax, es              ; ES -> first paragraph of the program (containing PSP)
    sub bx, ax              ; BX = program size in paragraphs
    mov ah, 4ah             ; Resize memory block - http://www.ctyme.com/intr/rb-2936.htm
    int 21h                 ; Call MS-DOS

    ret
free_memory ENDP

execute_hello PROC
    push ds                 ; Save DS
    push es                 ; Save ES

    mov cs:[stk_seg],ss     ; Save stack pointer
    mov cs:[stk_ptr],sp

    mov ax, 4B00h
    mov dx, OFFSET hello
    mov bx, SEG params
    mov es, bx
    mov bx, OFFSET params

    mov ax,4b00h            ; Exec - load and/or execute program - http://www.ctyme.com/intr/rb-2939.htm
    int 21h                 ; Call MS-DOS

    cli                     ; Let no interrupt disturb
    mov ss,cs:[stk_seg]     ; Restore stack pointer
    mov sp,cs:[stk_ptr]
    sti                     ; Allow interrupts

    pop es                  ; Restore ES and DS
    pop ds
    ret

    ; Data for this function in the code segment
    stk_ptr dw 0
    stk_seg dw 0
execute_hello ENDP

main PROC
    mov ax, @data           ; Initialize DS
    mov ds, ax

    call free_memory        ; ES should point to PSP (default)
    call execute_hello

    mov ax, 4C00h           ; Terminate with return code - http://www.ctyme.com/intr/rb-2974.htm
    int 21h                 ; Call MS-DOS
main ENDP

END main

程序结束由堆栈指针决定。因此,您应该确保STACK段是程序中的最后一个段。使用tlink命令行选项.MAP生成/s文件。它应该看起来像:

 Start  Stop   Length Name               Class

 00000H 0005BH 0005CH _TEXT              CODE
 00060H 00079H 0001AH _DATA              DATA
 00080H 0047FH 00400H STACK              STACK


Detailed map of segments

 0000:0000 005C C=CODE   S=_TEXT          G=(none)   M=INBBC8~1.ASM ACBP=48
 0006:0000 001A C=DATA   S=_DATA          G=DGROUP  M=INBBC8~1.ASM ACBP=48
 0006:0020 0400 C=STACK  S=STACK          G=DGROUP  M=INBBC8~1.ASM ACBP=74

Program entry point at 0000:004C

正如您所看到的,STACK是此处列出的最后一段。

现在你可以运行父项并阅读孩子(HELLO.EXE)必须告诉的内容: - )