home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / RGASM.RAR / ASMCODE.EXE / CHAPT9 / VOLUME.ASM < prev    next >
Encoding:
Assembly Source File  |  1993-05-10  |  15.3 KB  |  355 lines

  1. ;
  2. ;       Program Volume ( Chapter 9 )
  3. ;
  4.     page    55,132
  5. ;**************************************************************************
  6. ;                                                                         *
  7. ;  VOLUME - Reading/modifying the disk label                              *
  8. ;                                                                         *
  9. ;  Author: A.I.Sopin,  VSU   1992,    Version 1.4                         *
  10. ;                                                   03-03-92 03:32pm      *
  11. ;**************************************************************************
  12. .MODEL SMALL
  13. EXTRN    DIAM00 : FAR, DIAM01 : FAR, COLOR : FAR , INSYM4 : FAR
  14. ;
  15. ;  Save registers used
  16. PUSHR   MACRO   REGLST
  17.     IFB     <REGLST>
  18.     push    ax
  19.     push    bx
  20.     push    cx
  21.     push    dx
  22.     push    si
  23.     push    di
  24.     push    ds
  25.     push    es
  26.     push    bp
  27.     ENDIF
  28.     IRP     REG,<REGLST>
  29.     push    REG
  30.     ENDM
  31.     ENDM
  32. ;  Restore registers used
  33. POPR    MACRO   REGLST
  34.     IFB     <REGLST>
  35.     pop     bp
  36.     pop     es
  37.     pop     ds
  38.     pop     di
  39.     pop     si
  40.     pop     dx
  41.     pop     cx
  42.     pop     bx
  43.     pop     ax
  44.     ENDIF
  45.     IRP     REG,<REGLST>
  46.     pop     REG
  47.     ENDM
  48.     ENDM
  49. ;
  50. DIAM0   MACRO   TEXT$,OP$,M$,N$,U$,V$
  51.     mov     TOFF,offset TEXT$    ;  address of text string
  52.     mov     OP,OP$               ;  working mode
  53.     mov     bx,M$                ;
  54.     mov     M,bx                 ;  first line for output
  55.     mov     bx,N$                ;
  56.     mov     N,bx                 ;  last line for output
  57.     mov     bx,U$                ;  cursor line or buffer address
  58.     mov     U,bx                 ;
  59.     mov     bx,V$                ;
  60.     mov     V,bx                 ;
  61.     mov     L,80                 ;
  62.     lea     bx,PARM              ;
  63.     push    bx                   ;  address of parameter list
  64.     Call    DIAM00               ;
  65.     ENDM
  66. ;  Call subroutine for screen cleaning and color setting
  67. CLR     MACRO  M$,N$,C$
  68.     mov    ch, &M$
  69.     mov    dh, &N$
  70.     mov    ah, &C$
  71.     Call   Color
  72.     ENDM
  73. ;----------------------------------------------------------
  74. .STACK
  75. ;----------------------------------------------------------
  76. .DATA   
  77. MODE0   DB     0                       ;  screen mode
  78. DISK0   DB     0                       ;  screen mode
  79. TEXT0   DB     ' Reading/modifying the disk label             Version 1.4 ',0
  80. TEXT1   DB     '   Drive : A  (Esc - exit) ', 0
  81. TEXT2   DB     ' Disk label:  XXXXXXXXXXX  (PgUp - write modified label)  ',0
  82. TEXT3   DB     ' New label written. Press Enter  ', 0
  83. TEXT4   DB     ' Start sector:    =XXXXX ', 0
  84. ERR1    DB     ' Error reading disk label.   ',0
  85. ERR2    DB     ' Error writing disk label.   ',0
  86. ERR3    DB     ' The disk has no label. Press F2 to create new one',0
  87. SECTOR  DB      512 dup (0)
  88. CX0     DW      0
  89. SI0     DW      0
  90. ;  Parameter list for subroutines   DIAM00, DIAM01
  91.     EVEN
  92. PARM    LABEL  WORD                    ; parameter list (address in stack)
  93. OP      dw     0                       ; operation code
  94. M       dw     0                       ; number of first line
  95. N       dw     0                       ; number of last line
  96. TOFF    dw     0                       ; offset of text string
  97. L       dw     80                      ; length of text string
  98. U       dw     0                       ; cursor line  (1---25)
  99. V       dw     0                       ; cursor column (1---80)
  100. Q       dw     0                       ; accepted cursor line
  101. S       dw     0                       ; accepted cursor location
  102. W       db 4 dup (0)                   ; interrupt key code
  103. IOFF    dw     0                       ; address of screen parameters list
  104. ;----------------------------------------------------------
  105. .CODE
  106. .Startup
  107.     mov     ah,0Fh                  ;  function 0Fh - get video mode
  108.     int     10h                     ;  BIOS video service call
  109.     mov     MODE0,al                ;  save original video mode
  110.     mov     ah,19h                  ;  function 19h - get current drive 
  111.     int     21h                     ;  MS-DOS service call
  112.     mov     DISK0,al                ;  drive number
  113.     CLR     1,25,07h                ;  clear screen
  114.     DIAM0   TEXT0,1,2,2,0,0
  115. ;----------------------------------------------------------
  116. ;  Ask for disk drive (line 3)
  117. MAIN:   CLR     3,25,07h                ;  clear screen
  118.     mov     ax,@DATA                ;  address of data segment
  119.     mov     ds,ax                   ;
  120.     DIAM0   TEXT1,3,3,-3,3,12
  121.     cmp     W+1,1                   ;  Esc key?
  122.     jne     M0
  123.     jmp     Exit                    ;  entry
  124. M0:     mov     al,TEXT1+11             ;
  125.     and     al,0DFh                 ;  force uppercase
  126.     cmp     al,'A'                  ;  drive A ?
  127.     jb      MAIN                    ;  error
  128.     cmp     al,'Z'                  ;  drive  Z ?
  129.     ja      MAIN                    ;  error
  130.     sub     al,41h                  ;  0 -A, 1 -B, 2 -C ...
  131.     mov     CS:Disk,al              ;  save drive number
  132.     mov     ah,0Eh                  ;  function 0Eh - set drive
  133.     int     21h                     ;  DOS service call
  134. ;----------------------------------------------------------
  135. ;  Determining disk parameters  (Disk Info)
  136. DosInfo:mov     ah,30h                  ;
  137.     int     21h                     ;  MS-DOS  Version
  138.     mov     dl,al                   ;  save version
  139.     mov     ah,52h                  ;  DosInfo
  140.     int     21h                     ;  ES:(BX-2)  -pDiskInfo
  141.     LDS     bx,ES:[bx]              ;  DS:BX  -pFirstDrive
  142.     mov     cx,26                   ;  number of performances of cycle
  143. ;  Read disk info block for next disks
  144. M10:    mov     al,[bx]                 ;  drive number (0 -A, 1 -B)
  145.     cmp     al,CS:Disk              ;  disk found ?
  146.     je      M13                     ;
  147.     add     bx,24                   ;  address of next block (or 0FFFFh)
  148.     cmp     dl,4                    ;  DOS version <  4.0 ?
  149.     jl      M11                     ;
  150.     inc     bx                      ;  for DOS 4.0 and higher offset is 25
  151. M11:    LDS     bx,[bx]                 ;  address of next block
  152.     cmp     bx,0FFFFh               ;  end of chain?
  153.     je      M12                     ;
  154.     loop    M10                     ;  to cycle beginning
  155. M12:    jmp     MAIN                    ;  error in path string
  156. ;----------------------------------------------------------
  157. ;  Calculating sector number from cluster number
  158. M13:    mov     al,[bx]+4               ;  sectors per cluster - 1
  159.     inc     al                      ;
  160.     cbw                             ;
  161.     mov     CS:SecClus,ax           ;  sectors per cluster
  162.     mov     cx,4                    ;  elements in sector
  163.     shl     ax,cl                   ;
  164.     mov     CS:Clus32,ax            ;  directory entries per cluster
  165.     mov     ax,[bx]+6               ;  SecRes
  166.     mov     CS:SecRes,ax            ;  reserve sectors
  167.     mov     al,[bx]+8               ;
  168.     mov     CS:Fats,al              ;  number of  FATs
  169.     mov     ax,[bx]+9               ;  MaxDir
  170.     mov     CS:MaxDir,ax            ;  entries in root directory
  171.     mov     ax,[bx]+11              ;  SecData
  172.     mov     CS:SecData,ax           ;  start sector of data area
  173.     mov     al,[bx]+15              ;  SecFat
  174.     cbw                             ;
  175.     mov     CS:SecFat,ax            ;  sectors per FAT
  176.     mov     ax,[bx]+16              ;  SecDir (VER =3.xx)
  177.     cmp     dl,4                    ;  DOS version <  4.0 ?
  178.     jl      M14                     ;
  179.     mov     ax,[bx]+17              ;  SecDir (VER =4.01, 5.00)
  180. M14:    mov     CS:SecDir,ax            ;  start sector of root directory
  181.     mov     CS:SEC,ax               ;  start sector of root directory
  182. ;  Output message about start sector (line =4)
  183.     mov     dx,@DATA                ;
  184.     mov     ds,dx                   ;  DS:BP - output buffer
  185.     lea     bp,TEXT4+19             ;  address of buffer for editing
  186.     xor     dx,dx                   ;  clear high part
  187.     mov     si,5                    ;  number of digits
  188.     Call    INSYM4                  ;  number in DX:AX
  189.     DIAM0   TEXT4,1,4,-4,0,0
  190. ;----------------------------------------------------------
  191. ;  Read sectors of root directory
  192.     mov     ax,CS:MaxDir            ;  entries in root directory
  193.     mov     cl,4                    ;
  194.     shr     ax,cl                   ;  sectors in directory
  195.     mov     CX0,ax                  ;
  196. M20:    Call    READ                    ;  read sector
  197.     lea     si,SECTOR               ;  address of first sector
  198.     mov     cx,16                   ;  entries per sectors
  199. ;  Cycle for processing directory entries in sector read
  200. M21:    mov     SI0,si                  ;  save address of entry
  201.     mov     ax,[si]                 ;
  202.     and     ax,ax                   ;  empty entry ?
  203.     jz      M23                     ;
  204.     cmp     ax,0F600h               ;  empty entry ?  (Pctools)
  205.     je      M23                     ;
  206.     mov     al,[si]+11              ;  file attribute
  207.     and     al,08h                  ;
  208.     cmp     al,08h                  ;  label read ?
  209.     jne     M22                     ;
  210.     jmp     M25                     ;  label found
  211. M22:    add     si,32                   ;  address of next entry
  212.     loop    M21                     ;  to next cycle performance
  213.     inc     CS:SEC                  ;  modify number sector
  214.     dec     CX0                     ;  sectors left
  215.     jg      M20                     ;  read next sector
  216. ;  Output message "label not found"
  217. M23:    DIAM0   ERR3,2,5,-5,0,0
  218.     cmp     W+1,1                   ;  Esc key?
  219.     jne     M24                     ;
  220.     jmp     MAIN                    ;  exit
  221. M24:    cmp     W+1,3Ch                 ;  F2 - create label ?
  222.     jne     M23                     ;
  223. ;  Ask for creating a new label (line 6)
  224.     DIAM0   TEXT2,3,6,-6,6,15
  225.     cmp     W+1,1                   ;  Esc key?
  226.     jne     M27                     ;
  227.     jmp     MAIN                    ;  exit
  228. ;  Output label read  (DS:SI - address of directory entry)
  229. M25:    mov     cx,13                   ;  length of label
  230.     mov     ax,@DATA                ;
  231.     mov     es,ax                   ;  ES points to data segment
  232.     lea     di,TEXT2+14             ;  target address
  233.     cld                             ;  right direction
  234.     mov     al,' '                  ;  space character
  235.     rep     stosb                   ;  clear label area
  236.     mov     cx,11                   ;  length of label
  237.     lea     di,TEXT2+14             ;  target address
  238.     rep     movsb                   ;  send label
  239. ;  Input new label for modification and writing (line 7)
  240. M26:    DIAM0   TEXT2,3,6,-6,6,15
  241.     cmp     W+1,1                   ;  Esc key?
  242.     jne     M27                     ;
  243.     jmp     MAIN                    ;  exit
  244. M27:    cmp     W+1,49h                 ;  PgUp - write label ?
  245.     je      M28                     ;
  246.     jmp     MAIN                    ;
  247. ;  Write sector with the label modified onto disk
  248. M28:    mov     ax,@DATA                 ;
  249.     mov     es,ax                   ;  ES points to data segment
  250.     mov     di,SI0                  ;  ES:DI - target address
  251.     Call    VOLMOV                  ;  send label
  252.     Call    WRITE                   ;  write logical sector
  253. ;  Write label in the  Boot-sector (offset 43)
  254.     mov     CS:SEC,0                ;  Boot sector
  255.     Call    READ                    ;  read sector
  256.     lea     di,SECTOR+43            ;  target address
  257.     Call    VOLMOV                  ;  send label
  258.     Call    WRITE                   ;  write logical sector
  259.     DIAM0   TEXT3,2,7,-7,0,0        ;  output message "label written"
  260.     jmp     MAIN                    ;  perform program from beginning
  261. ;----------------------------------------------------------
  262. ;  Finish of the program's work
  263. Exit:   xor     ah,ah                   ;  function 00h - set video mode
  264.     mov     al,MODE0                ;  original video mode
  265.     int     10h                     ;  BIOS video service call
  266.     mov     dl,DISK0                ;  original disk drive
  267.     mov     ah,0Eh                  ;  function 0Eh - set current drive
  268.     int     21h                     ;  DOS swervice call
  269.     mov     ax,4C00h                ;  function 4Ch - terminate process
  270.     int     21h                     ;  DOS service call
  271. ;----------------------------------------------------------
  272. ;  Data in code segment
  273. MaxDir  DW      0                       ;  Entries in root directory
  274. NumClus DW      0                       ;  Cluster number
  275. SecDir  DW      0                       ;  Start sector of root directory
  276. SecData DW      0                       ;  Start sector of data area
  277. SecRes  DW      0                       ;  Number of reserve sectors
  278. SEC     DW      0                       ;  Logical sector number
  279. SecClus DW      0                       ;  Sectors per cluster
  280. SecFat  DW      0                       ;  sectors per FAT
  281. Dirs    DW      0                       ;  Sectors in root directory
  282. Fats    DB      0                       ;  Number of FAT
  283. TypFat  DB      0                       ;  FAT type (12 or 16 bits)
  284. Disk    DB      0                       ;  Drive number  0 -A, 1 -B, 2 -C...
  285. Clus32  DW      0                       ;  Directory entries in cluster
  286. ;----------------------------------------------------------
  287. ;
  288. ;  Subroutine for sector reading  (INT  26h)
  289. ;
  290. ;  SEC   - number of logical sector
  291. ;
  292. ;  DS:BX - address of sector buffer
  293. ;
  294. ;  In case of an error the Carry Flag is set
  295. ;
  296. ;----------------------------------------------------------
  297. READ    PROC    NEAR
  298.     PUSHR   <bp,si,di>              ;  save registers
  299.     lea     bx,SECTOR               ;  address of buffer for reading sector
  300.     mov     ax,@DATA                ;
  301.     mov     ds,ax                   ;  DS points to data segment
  302.     mov     cx,1                    ;  number of sectors =1
  303.     mov     al,CS:Disk              ;  drive number (0 -A, 1 -B...)
  304.     mov     dx,CS:SEC               ;  sector number
  305.     int     25h                     ;  read logical sector
  306.     add     sp,2                    ;  adjust stack
  307.     POPR    <di,si,bp>              ;  restore registers
  308.     RETN
  309. READ    ENDP
  310. ;----------------------------------------------------------
  311. ;
  312. ;  Subroutine for sector writing  (INT  26h)
  313. ;
  314. ;  SEC   - number of logical sector
  315. ;
  316. ;  DS:BX - address of sector buffer
  317. ;
  318. ;  In case of an error the Carry Flag is set
  319. ;
  320. ;----------------------------------------------------------
  321. WRITE   PROC    NEAR
  322.     lea     bx,SECTOR               ;  address of sector buffer
  323.     mov     ax,@DATA                ;
  324.     mov     ds,ax                   ;  DS points to data segment
  325.     mov     cx,1                    ;  number of sectors =1
  326.     mov     al,CS:Disk              ;  drive number (0 -A, 1 -B...)
  327.     mov     dx,CS:SEC               ;  sector number
  328.     int     26h                     ;  write logical sector
  329.     pop     cx                      ;  adjust stack
  330.     jc      ERROR                   ;  to error message
  331.     RETN                            ;  normal exit
  332. ;  Output error message "label not written"
  333. ERROR:  DIAM0   ERR2,2,7,-7,0,0         ;
  334.     pop     cx                      ;  adjust stack
  335.     jmp     MAIN                    ;
  336. WRITE   ENDP
  337. ;----------------------------------------------------------
  338. ;
  339. ;  Subroutine for sending disk label
  340. ;
  341. ;  ES:DI - address where the label must be sent
  342. ;
  343. ;----------------------------------------------------------
  344. VOLMOV  PROC    NEAR
  345.     mov     cx,11                   ;  length of label
  346.     mov     ax,@DATA                ;
  347.     mov     ds,ax                   ;  DS points to data segment
  348.     lea     si,TEXT2+14             ;  DS:SI - source
  349.     cld                             ;  rigth direction
  350.     rep     movsb                   ;  send label
  351.     mov     byte ptr ES:[di],28h    ;  send attribute
  352.     RETN
  353. VOLMOV  ENDP
  354.     END
  355.