MASM汇编库链接

时间:2018-05-05 17:41:05

标签: assembly procedure masm

我正在尝试实现Lee的算法的可视化,其中包含" main"使用宏库和过程库的代码文件。我可以很好地链接宏库,但我不能真正理解从不同文件调用函数。

我现在这样做的方式,它工作正常,直到我为过程库添加include指令。在这种情况下我该如何处理程序调用?现在,TASM组装代码很好,但在调用可执行文件时没有任何反应。

我也不太了解段寄存器重叠的方式。 我甚至应该在这里使用include指令,还是我应该创建多个目标文件?如果是这样,那为什么呢?为什么我不能使用include指令并组装一个文件?

主档案:

;*** MAIN FILE ***;
;-- INCLUSIONS --;

include projMacr.mac
;-- MACROS USED --;
; putPoint (xCoord, yCoord, color)
;   colors character at (x, y) with color
; clrScreen
;   clears screen by printing blanks
; makeBorder
;   creates a x, y border
;-----------------;

;-- DATA SEGMENT --;
date segment para public 'data'
    ;* Vars used by macros
    macAux1 DB 0
    macAux2 DB 0

    ;* x and y coord storage vars
    xPut DB 0
    yPut DB 0
    xStart DB -1
    yStart DB -1
    xFin DB -1
    yFin DB -1

    ;* Color palette
    colorObstacle DB 0A0h
    colorStart DB 030h
    colorFin DB 040h

    ;* Array used as queue for Lee's algorithm + iterators
    queueX DW 300 DUP (?)
    queueY DW 300 DUP (?)
    qBeg DW 0
    qFin DW 0
date ends

;-- STACK SEGMENT --;
stk segment para stack 'stack'   
    db 64 dup ('my_stack') 
stk ends

include projLib.asm

;-- CODE SEGMENT --;
cod segment para public 'code' 
main proc far
assume cs: cod, ds: date, ss: stk, es: date
    ;-- INIT BEGINS --;
    push ds      
    xor ax, ax 
    push ax
    mov ax, date
    mov ds, ax
    ;-- INIT ENDS --;

    ;-- PROGRAM FLOW BEGINS --;
    clrScreen
    makeBorder 30, 10, colorObstacle

    KeyLoop:
    mov ah, 00h
    int 16h

    cmp al, 115 ;Compare with 's'
    je DownKey
    cmp al, 119 ;Compare with 'w'
    je UpKey
    cmp al, 97 ;Compare with 'a'
    je LeftKey
    cmp al, 100 ;Compare with 'd'
    je RightKey
    cmp al, 32 ;Compare with 'Space'
    je SpaceBar
    cmp al, 114 ;Compare with 'r'
    je ResetKey
    cmp al, 113 ;Compare with 'q'
    je ExitKey
    cmp al, 122;Compare with 'z'
    je SetStart
    cmp al, 120;Compare with 'x'
    je SetFinish
    cmp al, 99;Compare with 'c'
    je RunAlgorithm

    jmp KeyLoop

    DownKey: ;Add to Y coord
        inc yPut
    jmp KeyLoop

    UpKey: ;Subtract from Y coord
        dec yPut
    jmp KeyLoop

    LeftKey: ;Subtract from X coord
        dec xPut
    jmp KeyLoop

    RightKey: ;Add to X coord
        inc xPut
    jmp KeyLoop

    SpaceBar: ;Paint point
        putPoint xPut, yPut, colorObstacle
    jmp KeyLoop

    SetStart: ;Set algorithm begin point
        mov bl, xPut
        mov xStart, bl
        mov bl, yPut
        mov yStart, bl
    jmp KeyLoop

    SetFinish: ;Set algorithm end point
        mov bl, xPut
        mov xFin, bl
        mov bl, yPut
        mov yFin, bl
    jmp KeyLoop

    RunAlgorithm: ;Start Lee's algorithm if conditions are met

        push offset xStart
        push offset yStart
        call enqueue
        add sp, 4

        call dequeue

    jmp KeyLoop

    ResetKey: ;Do reset logic
    jmp KeyLoop

    ExitKey: ;End execution

    clrScreen
    ;-- PROGRAM FLOW ENDS --;

    ret
main endp
cod ends
end main

宏库:

;-- putPoint --;
; 3 parameters: 
; xCoord
; yCoord
; color - Of format 0WZh, W - background color, Z - text color
;
; alters no registers
;--------------;
putPoint macro xCoord, yCoord, color
    ;* Save registers
    push ax
    push bx
    push cx
    push dx

    ;* Set cursor position
    mov ah, 2 ;Service to set cursor position
    mov bh, 0 ;Video page
    mov dl, xCoord
    mov dh, yCoord
    int 10h ;Bios screen services

    ;* Color point on screen
    mov ah, 09h ;Service to display character with color
    mov al, 00h ;Character to display (empty space)
    mov bh, 0 ;Video page
    mov bl, color
    mov cx, 1 ;Print 1 character (only 1 point)
    int 10h ;Bios screen services

    ;* Restore registers
    pop dx
    pop cx
    pop bx
    pop ax
endm

;-- clrScreen --;
; 0 parameters
;
; alters no registers
;--------------;
clrScreen macro
    ;* Save registers
    push ax
    push bx
    push cx
    push dx

    ;* Reset cursor position
    mov ah, 2 ;Service to set cursor position
    mov bh, 0 ;Video page
    mov dx, 0 ;DH - X coord, DL - Y coord, both to zero
    int 10h ;Bios screen services

    ;* Color screen to black (clear screen)
    mov ah, 09h ;Service to display character with color
    mov al, 00h ;Character to display (empty space)
    mov bh, 0 ;Video page
    mov bl, 0 ;Black
    mov cx, 2000 ;Print 2000 character (to cover entire screen)
    int 10h ;Bios screen services

    ;* Reset cursor position
    mov ah, 2 ;Service to set cursor position
    mov bh, 0 ;Video page
    mov dx, 0 ;DH - X coord, DL - Y coord, both to zero
    int 10h ;Bios screen services

    ;* Restore registers
    pop dx
    pop cx
    pop bx
    pop ax
endm

;-- makeBorder --;
; 3 parameters: 
; xSize
; ySize
; color - Of format 0WZh, W - background color, Z - text color
;
; alters cx
;--------------;
makeBorder macro xSize, ySize, color
    local Loop_1, Loop_2

    ;* Use auxiliary vars for putPoint macro call
    mov macAux1, 0
    mov macAux2, 0

    ;* First loop - border xCoord sides
    mov cx, xSize
    Loop_1:
        putPoint macAux1, 0, color
        putPoint macAux1, ySize, color

        inc macAux1
    loop Loop_1

    ;* Second loop - border yCoord sides
    mov cx, ySize
    Loop_2:
        putPoint 0, macAux2, color
        putPoint xSize, macAux2, color

        inc macAux2
    loop Loop_2

    putPoint xSize, ySize, color
endm

程序库:

cod1 segment para 'code'

;-- enqueue --;
; 2 parameters:
; bp + 4: address of xVar
; bp + 2: address of yVar
;
;does not alter registers
;-------------;
public enqueue
enqueue proc near
assume cs:cod1, ds:date
    ;* Function prologue
    push bp
    mov bp, sp
    push ax
    push bx

    mov ax, [bp + 4] ;xVar
    mov bx, [bp + 2] ;yVar
    mov di, qFin
    mov queueX[di], ax
    mov queueY[di], bx
    inc qFin

    ;* Function epilogue
    pop bx
    pop ax
    pop bp
    ret

enqueue endp

;-- dequeue --;
;
;does not alter registers
;-------------;
public dequeue
dequeue proc near
assume cs:cod1
    inc qBeg

    ret
dequeue endp

cod1 ends
end

0 个答案:

没有答案