home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 07 / masm.asc < prev    next >
Text File  |  1991-06-11  |  4KB  |  185 lines

  1. _MASM'S CHANGING FACE_
  2. by Mike Schmit
  3.  
  4.  
  5.  
  6. [LISTING ONE]
  7.  
  8.   .MODEL small
  9.   .STACK 100                ; reserves 100 bytes for the stack
  10.   .CODE                     ; start of code segment
  11.   main PROC
  12.    .STARTUP                 ; generates startup code
  13.    mov  bx, 1               ; stdout
  14.    mov  cx, msg_len
  15.    mov  dx, offset DGROUP:msg
  16.    mov  ah, 40h             ; write to handle
  17.    int  21h                 ; call DOS to write msg
  18.    .EXIT                    ; generates exit code
  19.   main ENDP
  20.   .DATA                     ; start of data segment
  21.   msg BYTE 'Hello world.'
  22.   msg_len equ $ - msg
  23.   END main                  ; end, specify starting address
  24.  
  25.  
  26.  
  27.  
  28.  
  29. [LISTING TWO]
  30.  
  31.  
  32.  EXTRN GetDC     : far
  33.  EXTRN MoveTo    : far
  34.  EXTRN LineTo    : far
  35.  EXTRN ReleaseDC : far
  36.  
  37.  point_list struc
  38.   x1 dw ?
  39.   y1 dw ?
  40.   x2 dw ?
  41.   y2 dw ?
  42.  point_list ends
  43.         .
  44.         .  (assume bx = hWnd)
  45.         .
  46.         push    bx
  47.         call    GetDC       ; returns hDC
  48.         mov     di, ax
  49.  
  50.         push    di
  51.         push    [si].x1
  52.         push    [si].y1
  53.         call    MoveTo
  54.  
  55.         push    di
  56.         push    [si].x2
  57.         push    [si].y2
  58.         call    LineTo
  59.  
  60.         push    bx
  61.         push    di
  62.         call    ReleaseDC
  63.         .
  64.         .
  65.         .
  66.  
  67.  
  68.  
  69. [LISTING THREE]
  70.  
  71.  GetDC     PROTO FAR PASCAL hWnd:WORD
  72.  MoveTo    PROTO FAR PASCAL hDC:WORD, nX:WORD, nY:WORD
  73.  LineTo    PROTO FAR PASCAL hDC:WORD, nX:WORD, nY:WORD
  74.  ReleaseDC PROTO FAR PASCAL hWnd:WORD, hDC:WORD
  75.  
  76.  option oldstructs
  77.  point_list struct
  78.   x1 word ?
  79.   y1 word ?
  80.   x2 word ?
  81.   y2 word ?
  82.  point_list ends
  83.         .
  84.         .    (assume bx = hWnd)
  85.         .
  86.         invoke  GetDC, bx              ; returns hDC
  87.         mov     di, ax
  88.         invoke  MoveTo, di, [si].x1, [si].y1
  89.         invoke  LineTo, di, [si].x2, [si].y2
  90.         invoke  ReleaseDC, bx, di
  91.         .
  92.         .
  93.         .
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. [LISTING FOUR]
  101.  
  102.  factorial MACRO num
  103.   LOCAL result, factor
  104.   IF num LE 0
  105.    %error factorial parameter out of bounds
  106.   ENDIF
  107.   result = 1
  108.   factor = num
  109.   WHILE factor GT 0
  110.     result = result * factor
  111.     factor = factor - 1
  112.   ENDM
  113.   EXITM %result
  114.  ENDM
  115.  i = 1
  116.  REPEAT 20             ; repeat block macro
  117.    DWORD factorial(i)  ; to generate a table of
  118.    i = i + 1           ; the first 20 factorials
  119.  ENDM
  120.  DWORD factorial(-33)  ; error
  121.  
  122.  
  123.  
  124.  
  125. Example 1. Macro parameters can either be required as designated by the REQ keyword or specify a default value
  126.  
  127.  set_cursor_pos MACRO row:REQ, col:REQ, page:=<0>
  128.   mov  dh, row
  129.   mov  dl, col
  130.   mov  bh, page
  131.   int  10h
  132.  ENDM
  133.  ...
  134.  set_cursor_pos 5, 10, 1    ; all parameters supplied
  135.  ...
  136.  set_cursor_pos 7, 15       ; page parameter takes default value
  137.  ...
  138.  set_cursor_pos             ; ERROR: required parameters missing
  139.  
  140.  
  141.  
  142.  
  143. Figure 1: MASM 6.0 contains decision and loop directives (in this case, an .IF/.ELSE loop) that are translated to their corresponding instructions at assembly time.
  144.  
  145.  .IF  ax <  mem_word1
  146.       mov   mem_word2, 2
  147.  .ELSE
  148.       mov   mem_word2, 3
  149.  .ENDIF
  150.  
  151. The above code is translated to the following:
  152.  
  153.         cmp    ax, mem_word1
  154.         jnb    @C0001
  155.         mov    mem_word2, 2
  156.         jmp    @C0003
  157.  @C0001:
  158.         mov    mem_word2, 3
  159.  @C0003:
  160.  
  161.  
  162.  
  163.  
  164. Figure 2. MASM 6.0 automatically generates a jump fixup when there is a jump out of range. Notice this example that the generated code is five bytes long instead of two. 
  165.  
  166.  
  167.       cmp ax, error_code
  168.       je  exit_error
  169.       db 128 dup(90h)  ; (128 bytes of code, NOP's here)
  170.  exit_error:
  171.  
  172.  
  173. MASM 6.0 translates this to the following:
  174.  
  175.  
  176.       cmp ax, error_code
  177.       jne $+3          ; Note: $+3 is a relative
  178.                        ; jump 3 bytes ahead
  179.       jmp exit_error
  180.       db 128 dup(90h)
  181.  exit_error:
  182.  
  183.  
  184.  
  185.