ITEEDU

第1章 Iczelion的Win32汇编教程

第2章 Iczelion的ODBC教程

第3章 Iczelion的VxD教程

第4章 Iczelion的PE教程

第5章 罗云彬的Win32汇编教程

第6章 Win32ASM经验点滴

第7章 X86汇编语言编程

第8章 加密解密

第9章 病毒的分析和防治

Windows 95 长文件名的使用

概述:

.386
        CODE  SEGMENT   USE16
              ASSUME    CS:CODE,DS:CODE
              ORG       100H
      START:
              JMP       INSTALL

      HANDLE  DW        ?
        _MIN  DW        ?
        _SEC  DW        ?
       _SEC1  DW        ?
          _A  DD        ?
          _B  DD        ?
   _FS_POINT  DW        0
   _GS_POINT  DW        0
        _DIV  DD        1
        FLAG  DB        ?           ;=1 +
     DIGITAL  DD        5000        ;how many points want to calculate
       POINT  DW        ?           ;total point /5
   NUM_POINT  DW        ?           ;total point /5 * 4
      _COUNT  DD        ?
    TMP_NUM0  DD        ?
     TMP_NUM  DD        10 DUP (?)
    KEY_BUFF  DB        6,0,6 DUP (0)

      D_SAVE  DB        'Saving result to file %c ...',0
              DW        80H
   D_SAVE_OK  DB        8,8,8,8,', OK !',0DH,0AH,0
  D_SAVE_ERR  DB        8,8,8,8,', error !',0DH,0AH,0

      D_TIME  DB        0DH,0AH,'Total %ld points, time used %d:%02d.%03d, '
              DB        'calculate %ld times.',0DH,0AH,0
              DW        DIGITAL,_MIN,_SEC,_SEC1,_DIV
      D_SCAN  DB        '<> Dec 18, 1996',0DH,0AH
              DB        'Copyright(C) by Luo Yun Bin, phone 0576-4114689',0DH,0AH,0AH
              DB        'How many points (10-80000): ',0
     D_ABORT  DB        'User pressed Esc, calculate aborted !%20r ',0DH,0AH,0
       D_CAL  DB        'Calculating, please waiting ... (Esc to cancel)',0DH,0
      D_CAL1  DB        '%d %% calculated, please waiting ... (Esc to cancel)',0DH,0
              DW        PERCENT
     PERCENT  DW        ?
      D_STR1  DB        ' PI = %1ld.%c',0DH,0AH,0
              DW        TMP_NUM0,D_SUB_STR
      D_STR2  DB        '%5ld : %c'
              DB        0DH,0AH,0
              DW        _COUNT,D_SUB_STR
   D_SUB_STR  DB        '%05ld %05ld %05ld %05ld %05ld '
              DB        '%05ld %05ld %05ld %05ld %05ld',0
              DW        TMP_NUM,TMP_NUM+4,TMP_NUM+8,TMP_NUM+12,TMP_NUM+16
              DW        TMP_NUM+20,TMP_NUM+24,TMP_NUM+28,TMP_NUM+32,TMP_NUM+36

    INSTALL:
              MOV       SI,OFFSET D_SCAN
              CALL      PRINTF
              MOV       AH,0AH
              MOV       DX,OFFSET KEY_BUFF
              INT       21H
              MOV       SI,OFFSET KEY_BUFF+2
              CALL      SCANF
              MOV       EAX,DWORD PTR SCAN_NUM
              MOV       DIGITAL,EAX
              MOV       SI,OFFSET D_CAL
              CALL      PRINTF

              XOR       AX,AX
              MOV       DS,AX
              MOV       AX,DS:[046CH]
              PUSH      CS
              POP       DS
              MOV       _SEC,AX

              MOV       AX,CS
              ADD       AX,1000H    ;result of 4*4*5/25^n
              MOV       FS,AX
              ADD       AX,1000H    ;result of 4*239/57121^n
              MOV       GS,AX
              ADD       AX,1000H    ;total result
              MOV       BP,AX

              MOV       AX,FS
              CALL      INIT_NUM
              MOV       DWORD PTR FS:[4],4*239*100000

              MOV       AX,GS
              CALL      INIT_NUM
              MOV       DWORD PTR GS:[4],4*4*5*100000

              MOV       AX,BP
              CALL      INIT_NUM

              CALL      PRE
              CALL      CALC

              XOR       AX,AX
              MOV       DS,AX
              MOV       AX,DS:[046CH]
              PUSH      CS
              POP       DS
              MOV       _SEC1,AX

              PUSH      POINT
              CALL      NUM_OUT
              POP       POINT

              MOV       AX,_SEC1
              SUB       AX,_SEC
              MOV       CX,55
              MUL       CX
              MOV       CX,1000
              DIV       CX
              MOV       _SEC1,DX
              MOV       CX,60
              XOR       DX,DX
              DIV       CX
              MOV       _MIN,AX
              MOV       _SEC,DX
              MOV       SI,OFFSET D_TIME
              CALL      PRINTF

              MOV       SI,81H
              MOV       DI,80H
    CMD_LOP:
              LODSB
              CMP       AL,0DH
              JZ        CMD_END
              CMP       AL,20H
              JBE       CMD_LOP
              CMP       AL,'a'
              JB        CMD_STORE
              CMP       AL,'z'
              JA        CMD_STORE
              SUB       AL,20H
  CMD_STORE:
              STOSB
              JMP       SHORT CMD_LOP
    CMD_END:
              XOR       AL,AL
              STOSB

              MOV       SI,80H
              CMP       BYTE PTR DS:[SI],0
              JZ        QUIT

              MOV       SI,OFFSET D_SAVE
              CALL      PRINTF

              MOV       AH,3CH
              XOR       CX,CX
              MOV       DX,80H
              INT       21H
              JB        FILE_ERR

              MOV       HANDLE,AX
              MOV       STD_OUT,OFFSET WRITE_FILE
              CALL      NUM_OUT

              MOV       STD_OUT,OFFSET PRT_TO_SCR
              MOV       SI,OFFSET D_SAVE_OK
              CALL      PRINTF

              MOV       AH,3EH
              MOV       BX,HANDLE
              INT       21H
              INT       20H
   FILE_ERR:
              MOV       SI,OFFSET D_SAVE_ERR
              CALL      PRINTF
              INT       20H
       QUIT:
              INT       20H

  WRITE_FILE  PROC

              PUSHA
              MOV       AH,40H
              MOV       FLAG,AL
              MOV       BX,HANDLE
              MOV       CX,1
              MOV       DX,OFFSET FLAG
              INT       21H
              POPA
              RET

  WRITE_FILE  ENDP

         PRE  PROC

              MOV       EAX,DIGITAL ;total 65536*5/4 points
              CMP       EAX,(65536/4-3)*5       ;comp max points
              JBE       PRE_1
              MOV       EAX,(65536/4-3)*5
      PRE_1:
              CMP       EAX,10      ;comp min points
              JAE       PRE_2       ;must > 10 and < 81915
              MOV       EAX,10
      PRE_2:
              XOR       EDX,EDX
              MOV       ECX,5
              DIV       ECX
              MOV       EBX,EAX
              INC       EBX
              MOV       POINT,BX    ;point for print control

              MUL       ECX
              MOV       DIGITAL,EAX ;

              MOV       EAX,EBX
              INC       EAX
              MOV       ECX,4
              MUL       ECX
              MOV       NUM_POINT,AX            ;max used memory

              RET

         PRE  ENDP


        CALC  PROC

              MOV       ES,BP
     C_LOP0:
              MOV       AH,1
              INT       16H
              JZ        CALC_0
              XOR       AH,AH
              INT       16H
              CMP       AL,1BH
              JNZ       CALC_00
              PUSH      CS
              POP       ES
              MOV       SI,OFFSET D_ABORT
              CALL      PRINTF
              INT       20H
    CALC_00:
              XOR       EAX,EAX
              MOV       AX,_GS_POINT
              MOV       ECX,500
              MUL       ECX
              MOV       ECX,4
              DIV       ECX
              XOR       EDX,EDX
              DIV       DIGITAL
              MOV       PERCENT,AX
              MOV       SI,OFFSET D_CAL1
              PUSH      CS
              POP       ES
              CALL      PRINTF
              MOV       ES,BP
     CALC_0:
              XOR       EAX,EAX
              MOV       ECX,100000
              MOV       _A,EAX
              MOV       _B,EAX
              XOR       FLAG,00000001B          ;init part
;============================================================
              MOV       EBX,57121
     C_LOP1:
              MOV       SI,_FS_POINT            ;if 4*5/25^n = 0 skip
              CMP       SI,NUM_POINT
              JAE       CALC_1
              CMP       DWORD PTR FS:[SI],0
              JNZ       C_LOP2
              ADD       _FS_POINT,4
              JMP       SHORT C_LOP1
     C_LOP2:
              MUL       ECX
              ADD       EAX,FS:[SI]
              ADC       EDX,0
              DIV       EBX
              MOV       FS:[SI],EAX
              MOV       EAX,EDX

              ADD       SI,4
              CMP       SI,NUM_POINT
              JB        C_LOP2
;=======================================================
     CALC_1:
              MOV       EBX,25
     C_LOP3:
              MOV       SI,_GS_POINT            ;if 4*5/25^n = 0 skip
              CMP       SI,NUM_POINT
              JAE       CALC_4
              CMP       DWORD PTR GS:[SI],0
              JNZ       C_LOP4
              ADD       _GS_POINT,4
              JMP       SHORT C_LOP3
     C_LOP4:
              MOV       EAX,_A
              MUL       ECX
              ADD       EAX,GS:[SI]
              ADC       EDX,0
              DIV       EBX
              MOV       GS:[SI],EAX
              MOV       _A,EDX

              MOV       EAX,_B
              MUL       ECX
              ADD       EAX,GS:[SI]
              ADC       EDX,0
              SUB       EAX,FS:[SI]
              SBB       EDX,0

              CMP       EDX,0
              JL        _T1
              DIV       _DIV
              MOV       _B,EDX
              JMP       SHORT _T2
        _T1:
              IDIV      _DIV
              DEC       EAX
              ADD       EDX,_DIV
              MOV       _B,EDX
        _T2:
              TEST      FLAG,00000001B
              JNZ       CALC_2
              SUB       ES:[SI],EAX
              JMP       SHORT CALC_3
     CALC_2:
              ADD       ES:[SI],EAX
     CALC_3:
              ADD       SI,4
              CMP       SI,NUM_POINT
              JB        C_LOP4
     C_LOP5:
              ADD       _DIV,2
              JMP       C_LOP0
;====================================================
     CALC_4:
              XOR       EAX,EAX
              MOV       _A,EAX
              MOV       SI,NUM_POINT
     C_LOP6:
              SUB       SI,4
              MOV       EAX,ES:[SI]
              ADD       EAX,_A

              CMP       EAX,ECX
              JGE       CALC_5
              CMP       EAX,0
              JLE       CALC_6
              MOV       _A,0
              MOV       ES:[SI],EAX
              JMP       SHORT CALC_8
     CALC_5:
              XOR       EDX,EDX
              DIV       ECX
              MOV       ES:[SI],EDX
              MOV       _A,EAX
              JMP       SHORT CALC_8
     CALC_6:
              MOV       EDX,-1
              IDIV      ECX
              DEC       EAX
              ADD       EDX,ECX
              MOV       ES:[SI],EDX
              MOV       _A,EAX
     CALC_8:
              CMP       SI,4
              JA        C_LOP6

              MOV       EAX,_A
              MOV       ES:[0],EAX

              PUSH      CS
              POP       ES
              RET

        CALC  ENDP

    INIT_NUM  PROC

              PUSH      ES
              MOV       ES,AX
              XOR       EAX,EAX
              CLD
              XOR       DI,DI
              MOV       CX,65536/4
              CLD
              REP       STOSD
              POP       ES
              RET

    INIT_NUM  ENDP

     NUM_OUT  PROC

              CLD
              XOR       ESI,ESI
              MOV       _COUNT,ESI
              MOV       DI,OFFSET TMP_NUM0
              MOV       CX,11
              CMP       CX,POINT
              JBE       NO_ADJUST
              MOV       CX,POINT
  NO_ADJUST:
              SUB       POINT,CX
              MOV       DS,BP
              REP       MOVSD
              PUSH      CS
              POP       DS

              PUSH      SI
              MOV       SI,OFFSET D_STR1
              CALL      PRINTF
              POP       SI
     NO_LOP:
              CMP       POINT,0
              JLE       NO_QUIT
              MOV       CX,10
              MOV       DI,OFFSET TMP_NUM
              XOR       EAX,EAX
              REP       STOSD

              MOV       CX,10
              MOV       DI,OFFSET TMP_NUM
              CMP       POINT,10
              JAE       NO1
              MOV       CX,POINT
        NO1:
              SUB       POINT,CX
              ADD       _COUNT,50
              MOV       DS,BP       ;bp = result segment
              REP       MOVSD
              PUSH      CS
              POP       DS

              PUSH      SI
              MOV       SI,OFFSET D_STR2
              CALL      PRINTF
              POP       SI
              JMP       SHORT NO_LOP
    NO_QUIT:
              RET

     NUM_OUT  ENDP

     INCLUDE  SCANF.ASM
     INCLUDE  PRINTF.ASM

        CODE  ENDS
              END       START
Windows 95 的长文件名给我们带来了很大的方便,本文讲述有关长文件名的使用方法。
长文件名的使用涉及到几个中断,在使用中,原先 DOS 中 INT 21H 的中断中参数有 ASC 文件名的地方,都有一个新的中断对应,如 INT 21H 的 3DH,6CH 对应 716CH (打开/建立文件),56H 对应 7156H(文件改名),41H 对应 7141H(文件删除)等等,其他参数中没有 ASC 文件名的,则使用原来的中断,如 3EH,3FH,40H (关闭/读/写)等等,一般编程中先检测系统是否支持长文件名,如果支持,则用新功能打开文件,再用普通的 3EH,3FH,40H 等对文件进行操作。本文中有几个例子,是用长文件名建立一个文件,写入一段文字,再打开文件、改名,然后删除,大家可以对应中断说明分析一下。
本程序要用到的 INT 21H 中断的 71xxH 功能如下:

INT 21H 中断的 716CH 功能:(打开或建立文件)

输入参数:

AX = 716Ch
BX = 存取模式和共享标志

Bit(s) Description
2-0 file access mode
000 read-only
001 write-only
010 read-write
100 read-only, do not modify file's last-access time
6-4 file sharing modes
7 no-inherit flag
8 do not buffer data (requires that all reads/writes be exact physical
sectors)
9 do not compress file even if volume normally compresses files
10 use alias hint in DI as numeric tail for short-name alias
12-11 unused??? (0)
13 return error code instead of generating INT 24h if critical error
CX = 文件属性
DX = 执行方式
Bit(s) Description (Table 01758)
0 open file (fail if file does not exist)
1 truncate file if it already exists (fail if file does not exist)
4 create new file if file does not already exist (fail if exists)
Note: the only valid combinations of multiple flags are bits 4&0 and 4&1
DS:SI -> ASC 码文件名
DI = 转化成短文件名后面加的编号

返回参数:CF 清除则成功
          AX  =         FILE HANDLE
          CX  =         ACTION TAKEN
0001h file opened
0002h file created
0003h file replaced
CF 置位则失败
AX = 7100H 则表示功能不被支持
返回参数:CF 清除则成功


INT 21H 中断的 7156H 功能:(文件改名)

输入参数:
AX = 7156h
DS:DX -> 原来的 ASC 码文件名
ES:DI -> 新的 ASC 码文件名
返回参数:CF 清除则成功
CF 置位则失败
AX = 7100H 则表示功能不被支持

INT 21H 中断的 7141H 功能:(文件删除)

输入参数:
AX = 7141h
DS:DX -> ASC 码文件名
SI = 属性及是否支持通配符
0000h 不支持通配符,忽略属性
0001h 支持通配符
CL = 属性
CH = must-match attributes
返回参数:CF 清除则成功
CF 置位则失败
AX = 7100H 则表示功能不被支持

源程序:

;长文件名示例之一,建立文件 This is a test file.txt,写入一句文字,再关闭

        CODE  SEGMENT
              ASSUME    CS:CODE,DS:CODE
              ORG       100H
      START:
              JMP       START1
   FILE_NAME  DB        'This is a test file.txt',0
      D_TEXT  DB        'Sample text',0DH,0AH
  D_TEXT_END  EQU       THIS BYTE
     D_ERROR  DB        'Create file error !',0DH,0AH,24H
     START1:
              MOV       AX,716CH
              MOV       BX,2        ;Read and write
              XOR       CX,CX       ;Normal attrib
              MOV       DX,0010H    ;Create new file
              MOV       SI,OFFSET FILE_NAME
              XOR       DI,DI
              INT       21H
              JB        ERROR
              MOV       BX,AX
              MOV       AH,40H
              MOV       CX,OFFSET D_TEXT_END - OFFSET D_TEXT
              MOV       DX,OFFSET D_TEXT
              INT       21H

              MOV       AH,3EH
              INT       21H
              INT       20H
      ERROR:
              MOV       AH,9
              MOV       DX,OFFSET D_ERROR
              INT       21H
              INT       20H
        CODE  ENDS
              END       START

;长文件名示例之二,打开文件 This is a test file.txt,显示文件内容,再关闭
        CODE  SEGMENT
              ASSUME    CS:CODE,DS:CODE
              ORG       100H
      START:
              JMP       START1
   FILE_NAME  DB        'This is a test file.txt',0
      BUFFER  DB        13 DUP (0),0DH,0AH,24H
     D_ERROR  DB        'Open file error !',0DH,0AH,24H
     START1:
              MOV       AX,716CH
              MOV       BX,2        ;Read and write
              XOR       CX,CX       ;Normal attrib
              MOV       DX,1        ;Open file
              MOV       SI,OFFSET FILE_NAME
              MOV       DI,1
              INT       21H
              JB        ERROR
              MOV       BX,AX
              MOV       AH,3FH
              MOV       DX,OFFSET BUFFER
              MOV       CX,13
              INT       21H
		
              MOV       AH,9
              MOV       DX,OFFSET BUFFER
              INT       21H
		
              MOV       AH,3EH
              INT       21H
              INT       20H
      ERROR:
              MOV       AH,9
              MOV       DX,OFFSET D_ERROR
              INT       21H
              INT       20H
        CODE  ENDS
              END       START
		
;长文件名示例之三,把文件 This is a test file.txt 改名到 This is another file name.txt
        CODE  SEGMENT
              ASSUME    CS:CODE,DS:CODE
              ORG       100H
      START:
              JMP       START1
   FILE_NAME  DB        'This is a test file.txt',0
    NEW_NAME  DB        'This is another file name.txt',0
     D_ERROR  DB        'Rename file error !',0DH,0AH,24H
     START1:
              MOV       AX,7156H
              MOV       DX,OFFSET FILE_NAME
              MOV       DI,OFFSET NEW_NAME
              INT       21H
              JB        ERROR
		
              INT       20H
      ERROR:
              MOV       AH,9
              MOV       DX,OFFSET D_ERROR
              INT       21H
              INT       20H
        CODE  ENDS
              END       START

;长文件名示例之四,把文件 This is another file name.txt 删除
        CODE  SEGMENT
              ASSUME    CS:CODE,DS:CODE
              ORG       100H
      START:
              JMP       START1
   FILE_NAME  DB        'This is another file name.txt',0
     D_ERROR  DB        'Delete file error !',0DH,0AH,24H
     START1:
              MOV       AX,7141H
              MOV       DX,OFFSET FILE_NAME
              XOR       SI,SI
              XOR       CX,CX
              INT       21H
              JB        ERROR
		
              INT       20H
      ERROR:
              MOV       AH,9
              MOV       DX,OFFSET D_ERROR
              INT       21H
              INT       20H
        CODE  ENDS
              END       START