home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1988 / 09 / moon.lis < prev    next >
File List  |  1988-08-22  |  9KB  |  292 lines

  1. _ARGUMENTS AND AUTOMATIC VARIABLES IN ASSEMBLY LANGUAGE_
  2. by 
  3. Raymond Moon
  4.  
  5. LISTING ONE
  6.  
  7.  1    page 60,132
  8.  2    title FILE_LENGTH - Determine File Length In Bytes
  9.  3    name FILE_LEN
  10.  4
  11.  5  comment @
  12.  6    FILE_LENGTH()                                 V 1.00
  13.  7  -------------------------------------------------------
  14.  8  NAME
  15.  9    file_length()         Determine file length in bytes
  16.  10
  17.  11  SYNOPSIS
  18.  12    length = file_length(filename, attribute);
  19.  13    long          length;         length of file in bytes
  20.  14    char          *filename;      path\filename.ext
  21.  15    unsigned int  attribute;      file attribute used
  22.  16                                   in search
  23.  17
  24.  18  DESCRIPTION
  25.  19    This function uses DOS function 4eh, Find First
  26.  20    File, to return with the filelength.  So that this
  27.  21    function does not corrupt the current Data Transfer
  28.  22    Area, DTA, this function moves the DTA to its own
  29.  23    automatic variable area.  If the file is not found,
  30.  24    -1L is returned.
  31.  25
  32.  26    This function allows any kind of file to be found by
  33.  27    specifying the file attribute.  The following #define
  34.  28    statements can be used to define the various file
  35.  29    attributes:
  36.  30
  37.  31    #define NORM          0x00    /* Normal file    */
  38.  32    #define RO            0x01    /* Read-only file */
  39.  33    #define HID           0x02    /* Hidden file    */
  40.  34    #define SYS           0x04    /* System file    */
  41.  35    #define VOL_ID        0x08    /* Volume ID      */
  42.  36    #define SUBDIR        0x10    /* Subdirectory   */
  43.  37    #define ARCH          0x20    /* Archive file   */
  44.  38
  45.  39    The bit or operator, |, can be used to combine
  46.  40    several attributes.  For example:
  47.  41
  48.  42    length = file_length("filename.ext", RO|HID|SYS);
  49.  43
  50.  44    All normal, read-only, hidden, and system files will
  51.  45    be searched for a match to "filename.ext."
  52.  46
  53.  47    This procedure was assembled using MICROSOFT MASM
  54.  48    V5.0 with the switches, /v /ml /z, set.
  55.  49
  56.  50  CAUTION
  57.  51    None.
  58.  52
  59.  53  RETURNS
  60.  54    File length as a long.  -1L signifies an error.
  61.  55
  62.  56  AUTHOR
  63.  57    Raymond Moon - 18 Nov 87
  64.  58
  65.  59    Copyright (c) 1987, MoonWare
  66.  60    ALL RIGHTS RESERVED
  67.  61
  68.  62  HISTORY
  69.  63    Version - Date        Remarks
  70.  64    1.00  - 18 Nov 87     - Orginal
  71.  65
  72.  66    @
  73.  67
  74.  68  ;----------------------------
  75.  69  ;  Define the segments for the memory model so that
  76.  70  ;  they can link with Microsoft C.
  77.  71
  78.  72  _TEXT   segment byte public 'CODE'
  79.  73  _TEXT   ends
  80.  74
  81.  75  _DATA   segment word    public  'DATA'
  82.  76  _DATA   ends
  83.  77
  84.  78  CONST   segment word    public  'CONST'
  85.  79  CONST   ends
  86.  80
  87.  81  _BSS    segment word    public  'BSS'
  88.  82  _BSS    ends
  89.  83
  90.  84  DGROUP  group   _DATA, CONST, _BSS
  91.  85
  92.  86  ;----------------------------
  93.  87  ;  Define the assume directive so MASM knows to what
  94.  88  ;  the segment registers point
  95.  89
  96.  90  assume  cs:_TEXT, ds:DGROUP, ss:DGROUP, es:DGROUP
  97.  91
  98.  92  ;----------------------------
  99.  93  ;  Define the structures for passed parameters and
  100.  94  ;  automatic variables.
  101.  95
  102.  96  PARMS    struc
  103.  97           dw     2 dup (?)
  104.  98  FILENAME dw     ?
  105.  99  ATTR     dw     ?
  106. 100  PARMS    ends
  107. 101
  108. 102  ;----------------------------
  109. 103  ;  _AUTO structure contains the following variables:
  110. 104  ;  OLD_DTA      Saved address of old DTA
  111. 105  ;  NEW_DTA      Start of NEW_DTA, this first field is
  112. 106  ;                reserved for subseqent Find Next File
  113. 107  ;  F_ATTR       Attribute found
  114. 108  ;  F_TIME       Time file was last written
  115. 109  ;  F_DATE       Date file was last written
  116. 110  ;  F_LENGTH     Double word file length
  117. 111  ;  F_NAME       Found filename.ext
  118. 112
  119. 113  _AUTO    struc
  120. 114  OLD_DTA  dd     ?
  121. 115  NEW_DTA  db     21 dup (?)
  122. 116  F_ATTR   db     ?
  123. 117  F_TIME   dw     ?
  124. 118  F_DATE   dw     ?
  125. 119  F_LENGTH dd     ?
  126. 120  F_NAME   db     13 dup (?)
  127. 121  _AUTO    ends
  128. 122
  129. 123  ;----------------------------
  130. 124  ;  Define structures for addressing trhe offset and
  131. 125  ;  segment portions of doubleword pointers and the
  132. 126  ;  least and most significant words of long intergers.
  133. 127
  134. 128  DOUBLEWORD_PTR  struc
  135. 129  OFF     dw      ?
  136. 130  SEGMT   dw      ?
  137. 131  DOUBLEWORD_PTR  ends
  138. 132
  139. 133  LONG_INT        struc
  140. 134  LSW     dw      ?
  141. 135  MSW     dw      ?
  142. 136  LONG_INT        ends
  143. 137
  144. 138  ;----------------------------
  145. 139  ;  Start the Code Segment.
  146. 140
  147. 141  _TEXT   segment
  148. 142
  149. 143  _FILE_LENGTH    proc    near
  150. 144
  151. 145          public  _FILE_LENGTH
  152. 146
  153. 147  ;----------------------------
  154. 148  ;  Standard entry logic.  Save calling BP.  Set up new
  155. 149  ;  BP.  Define automatic variable addressing base,
  156. 150  ;  AUTO.  Save segment registers are saved as DS and
  157. 151  ;  ES are used.
  158. 152
  159. 153          push    bp
  160. 154          mov     bp,sp
  161. 155          sub     sp, size _AUTO
  162. 156          AUTO    equ [bp - size _AUTO]
  163. 157          push    ds
  164. 158          push    es
  165. 159
  166. 160  ;----------------------------
  167. 161  ;  Get and save current DTA address in OLD_DTA.  Use
  168. 162  ;  DOS service 2fh.  The address is returned in ES:BX.
  169. 163
  170. 164          mov     ah,2fh
  171. 165          int     21h
  172. 166          mov     AUTO.OLD_DTA.OFF,bx
  173. 167          mov     AUTO.OLD_DTA.SEGMT,es
  174. 168
  175. 169  ;----------------------------
  176. 170  ;  Set up NEW_DTA as the current DTA.  Use DOS service
  177. 171  ;  1ah.  DS = SS assumed.
  178. 172
  179. 173          mov     ah,1ah
  180. 174          lea     dx,AUTO.NEW_DTA
  181. 175          int     21h
  182. 176
  183. 177  ;----------------------------
  184. 178  ;  Set up for File First Find, Service 4eh.
  185. 179  ;  CX = Search attribute
  186. 180  ;  DS:DX => ASCIIZ string, d:path\filename.ext
  187. 181
  188. 182          mov     ah,4eh
  189. 183          mov     cx,[bp].ATTR
  190. 184          mov     dx,[bp].FILENAME
  191. 185          int     21h
  192. 186
  193. 187  ;----------------------------
  194. 188  ;  Check for Carry Flag set as that indicates that the
  195. 189  ;  file was not found.  If so, put -1L in F_LENGH so
  196. 190  ;  -1L is returned as file length.
  197. 191
  198. 192          jnc     FL1
  199. 193          mov     ax,-1
  200. 194          mov     AUTO.F_LENGTH.LSW,ax
  201. 195          mov     AUTO.F_LENGTH.MSW,ax
  202. 196
  203. 197  ;----------------------------
  204. 198  ;  Restore old DTA, service 1ah.
  205. 199  ;  DS:DX => old DTA.
  206. 200
  207. 201  FL1:    mov     ah,1ah
  208. 202          mov     dx,AUTO.OLD_DTA.OFF
  209. 203          mov     ds,AUTO.OLD_DTA.SEGMT
  210. 204          int     21h
  211. 205
  212. 206  ;----------------------------
  213. 207  ;  Set up AX:DX to return file length.
  214. 208
  215. 209          mov     ax,AUTO.F_LENGTH.LSW
  216. 210          mov     dx,AUTO.F_LENGTH.MSW
  217. 211
  218. 212  ;----------------------------
  219. 213  ;  Exit logic.  Restore segment registers.  Remove any
  220. 214  ;  automatic variables.  Restore calling BP.
  221. 215
  222. 216          pop     es
  223. 217          pop     ds
  224. 218          mov     sp,bp
  225. 219          pop     bp
  226. 220          ret
  227. 221
  228. 222  _FILE_LENGTH    endp
  229. 223
  230. 224  _TEXT   ends
  231. 225
  232. 226          end
  233.  
  234.  
  235.  
  236.  
  237. Example 1: Definition of strncpy()
  238.  
  239.     char *strncpy(string1, string2, n);
  240.     char *string1;   pointer to the first string
  241.     char *string2;   pointer to the second string
  242.     unsigned int n;  number of characters to be
  243.                      copied
  244.  
  245.  
  246. Example 2: needs caption
  247.  
  248.  
  249.  PARMS struc      ; Passed arg addressing struc
  250.        dw   2 dup(?) ; Saved IP and bp registers
  251.     string1 dw    ?  ; Pointer to 1st string
  252.     string2 dw    ?  ; Pointer to 2nd string
  253.     N       dw    ?  ; # char to copy
  254.     PARMS ends
  255.  
  256.  
  257.  
  258. Example 3:  Automatic variable structure
  259.  
  260.  
  261.     _AUTO    struc    ; Auto var addressing struc
  262.     INT_1    dw   ?   ; 1st automatic variable
  263.     INT_2    dw   ?   ; 2nd automatic variable
  264.     INT_3    dw   ?   ; 3rd automatic variable
  265.     _AUTO ends
  266.  
  267.  
  268. Example 4: Standard entry code 
  269.  
  270.     push bp        ; Save the called bp
  271.     mov  bp,sp     ; Initialize new frame pointer
  272.     sub  sp,size _AUTO
  273.                    ; Make room for auto vars
  274.     AUTO equ [bp - size _AUTO]
  275.                    ; Auto var addressing base
  276.  
  277. Example 5: #defines for file attributes to be used with file_length()
  278.  
  279.  
  280.     #define NORM     0x00     /* Normal file    */
  281.     #define RO       0x01     /* Read-only file */
  282.     #define HID      0x02     /* Hidden file    */
  283.     #define SYS      0x04     /* System file    */
  284.     #define VOL_ID   0x08     /* Volume ID      */
  285.     #define SUBDIR   0x10     /* Subdirectory   */
  286.     #define ARCH     0x20     /* Archive file   */
  287.  
  288.  
  289.  
  290.  
  291. -30-
  292.