title: MASM Assembly library linkage
body: <p>I am trying to implement a visualization of Lee's algorithm, having a "main" code file that uses a macro library and a procedure library. I could link the macro library just fine but I can't really understand function calling from a different file.</p>
  
  <p>The way I am doing this right now, it worked fine up until the point where I add the include directive for the procedure library. How am I supposed to handle procedure calling in this situation? Right now, the TASM assembles the code just fine, but upon calling the executable nothing happens.</p>
  
  <p>I also do not really understand the way the segment registers overlap.
  Am I even supposed to use an include directive here or am I supposed to create multiple object files? And if so, why is that? Why can't I just used the include directive and assemble a single file?</p>
  
  <p>Main file:   </p>
  
  <pre><code>;*** 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
  </code></pre>
  
  <p>Macro library:</p>
  
  <pre><code>;-- 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
  </code></pre>
  
  <p>Procedure library:</p>
  
  <pre><code>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
  </code></pre>