Chinaunix首页 | 论坛 | 博客
  • 博客访问: 226386
  • 博文数量: 45
  • 博客积分: 1850
  • 博客等级: 上尉
  • 技术积分: 473
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-11 10:21
文章分类
文章存档

2006年(17)

2005年(28)

我的朋友

分类:

2006-04-27 13:01:29

程序文件的包含
为了方便编辑大型源程序,MASM允许把源程序分别放在几个文本文件中,在汇编时通过包含伪指令INCLUDE结合成一体,格式为:
INCLUDE 文件名
文件名的给定要符合DOS规范,扩展名任意,一般采用.ASM(汇编程序)、.MAC(宏定义库)、.INC(包含文件)。文件名可以包含路径,指明文件的存储位置,如果没有路径,MASM则优先在汇编命令行参数/I指定的目录下寻找,然后在当前目录下寻找,最后还会在环境参数INCLUDE指定的目录下寻找。汇编程序在对INCLUDE伪指令进行汇编时将他指定的文本文件内容插入在该伪指令所在的位置,与其他部分同时汇编。
例30.1:利用源程序包含的方法实现将输入的数据按升序输出
要求:从键盘输入最多100个待排序的数据,数据是0~255之间的无符号字节量,并以16进制形式输出在屏幕上显示出来。
分析:程序经常用到显示一个字符和一个字符串的功鞥,经他们定义成宏,单独保存在一个文件30-1.mac中,将处理数据输入、数据输出以及数据排序编写成子程序,保存在sub30-1.asm中,主程序提供入口参数和处理出口参数,顺序调用子程序,保存在30-1.asm中
;;;;;;;;30-1.mac宏库文件;;;;;;;;;
dispchar   macro char
           mov dl,char
           mov ah,2
           int 21h
           endm
dispmsg    macro message
           mov dx,offset message
           mov ah,9
           int 21h
           endm
;;;;;;;;;;30-1.asm主程序文件;;;;;;;;;;
        include   30-1.mac
        .model small
        .stack 256
        .data
msg1    db 'Please enter the number(xx):',0dh,0ah,'$'
msg2    db 'The numbers erntered are :','0dh,0ah,'$'
msg3    db 'The sorting result (ascending):',0dh,0ah,'$'
crlf    db 0dh,0ah,'$'
maxcount= 100
count   dw ?
buf     db maxcount dup(?)
        .code
        .startup
        dispmsg msg1
        mov bx,offset buf
        call input
        cmp cx,0
        je start4
        mov count,cx
        dispmsg crlf
        dispmsg msg2
        mov bx,offset buf
        mov cx,count
start2: mov al,[bx]
        call aldisp
        dispchar ','
        inc bx
        loop start2
        dispmsg crlf
        mov bx,offset buf
        mov cx,count
        call sorting
        dispmsg msg3
        mov bx,offset buf
        mov cx,count
start3: mov al,[bx]
        call aldisp
        dispchar ','
        inc bx
        loop start3
        dispmsg crlf
start4: .exit 0
        include sub30-1.asm        ;使用的子程序要安排在代码段中,所以在此包含进去
        end
;;;;;;;;sub30-1.asm;;;;;;;;;
aldisp   proc
         push ax
         push dx
         push ax
         mov dl,al
         shr dl,1
         shr dl,1
         shr dl,1
         shr dl,1
         or dl,30h
         cmp dl,39h
         jbe aldisp1
         add dl,7
aldisp1: mov ah,2
         int 21h
         pop dx
         and dl,0fh
         or dl,30h
         cmp dl,39h
         jbe aldisp2
         add dl,7
aldisp2: mov ah,2
         int 21h
         pop dx
         pop ax
         ret
aldisp   endp
sorting  proc
         cmp cx,0
         je sortend
         cmp cx,1
         je sortend
         push ax
         push dx
         push si
         mov si,bx
         dec cx
outlp:   mov dx,cx
         mov bx,si
inlp:    mov al,[bx]
         cmp al,[bx+1]
         jna next
         xchg al,[bx+1]
         mov [bx],al
next:    inc bx
         dec dx
         jnz inlp
         loop outlp
         pop si
         pop dx
         pop ax
sortend: ret
sorting  endp
convert  macro
         local input21,input22
         local input24,input25
         cmp dl,0
         je input21
         sub dl,7
input21: and dl,0fh
         cmp dh,0
         je input24
         cmp dh,'9'
         jbe input22
         sub dh,7
input22: shl dh,1
         shl dh,1
         shl dl,1
         shl dl,1
         or dl,dh
input24: mov [bx],dl
         inc bx
         inc cx
input25:
         endm
input    proc
         push ax
         push dx
         xor cx,cx
input01: xor dx,dx
input02: mov ah,1
         int 21h
input10: cmp al,0dh
         je input30
         cmp al,' '
         je input20
         cmp al,','
         je input20
         cmp al,08h
         je input17
         cmp al,'0'
         jb input17
         cmp al,'f'
         ja input17
         cmp al,'a'
         jb input11
         sub al,20h
         jmp input12
input11: cmp al,'F'
         ja input17
         cmp al,'A'
         jae input12
         cmp al,'9'
         ja input17
input12: cmp dl,0
         jne input13
         mov dl,al
         jmp input02
input13: cmp dh,0
         jne input17
         mov dh,dl
         mov dl,al
         jmp input02
input17: mov dl,7
         mov ah,2
         int 21h
         mov dl,'?'
         mov ah,2
         int 21h
         jmp input01
input20: convert
         jmp input01
input30: convert
         pop dx
         pop ax
         ret
input    endp
编译连接形成的.LST和.MAP文件如下:
Microsoft (R) Macro Assembler Version 6.11      04/27/06 12:50:15
30-1.asm           Page 1 - 1

    ;;;;;;;;;;30-1.asm;;;;;;;;;;
            include   30-1.mac
         C ;;;;;;;;30-1.mac;;;;;;;;;
         C dispchar   macro char
         C            mov dl,char
         C            mov ah,2
         C            int 21h
         C            endm
         C
         C dispmsg    macro message
         C            mov dx,offset message
         C            mov ah,9
         C            int 21h
         C            endm
         C
            .model small
            .stack 256
 0000            .data
 0000 50 6C 65 61 73 65  msg1    db 'Please enter the number(xx):',0dh,0ah,'$'
       20 65 6E 74 65 72
       20 74 68 65 20 6E
       75 6D 62 65 72 28
       78 78 29 3A 0D 0A
       24
 001F 54 68 65 20 6E 75  msg2    db 'The numbers erntered are :',0dh,0ah,'$'
       6D 62 65 72 73 20
       65 72 6E 74 65 72
       65 64 20 61 72 65
       20 3A 0D 0A 24
 003C 54 68 65 20 73 6F  msg3    db 'The sorting result (ascending):',0dh,0ah,'$'
       72 74 69 6E 67 20
       72 65 73 75 6C 74
       20 28 61 73 63 65
       6E 64 69 6E 67 29
       3A 0D 0A 24
 005E 0D 0A 24   crlf    db 0dh,0ah,'$'
 = 0064    maxcount= 100
 0061 0000   count   dw ?
 0063  0064 [   buf     db maxcount dup(?)
        00
       ]
 0000            .code
            .startup
 0000      :
 0000  BA ---- R    *     mov    dx, DGROUP
 0003  8E DA     *     mov    ds, dx
 0005  8C D3     *     mov    bx, ss
 0007  2B DA     *     sub    bx, dx
 0009  D1 E3     *     shl    bx, 001h
 000B  D1 E3     *     shl    bx, 001h
 000D  D1 E3     *     shl    bx, 001h
 000F  D1 E3     *     shl    bx, 001h
 0011  FA     *     cli   
 0012  8E D2     *     mov    ss, dx
 0014  03 E3     *     add    sp, bx
 0016  FB     *     sti   
            dispmsg msg1
 0017  BA 0000 R      1            mov dx,offset msg1
 001A  B4 09       1            mov ah,9
 001C  CD 21       1            int 21h
 001E  BB 0063 R          mov bx,offset buf
 0021  E8 00C1           call input
 0024  83 F9 00           cmp cx,0
 0027  74 5B           je start4
 0029  89 0E 0061 R          mov count,cx
            dispmsg crlf
 002D  BA 005E R      1            mov dx,offset crlf
 0030  B4 09       1            mov ah,9
 0032  CD 21       1            int 21h
            dispmsg msg2
 0034  BA 001F R      1            mov dx,offset msg2
 0037  B4 09       1            mov ah,9
 0039  CD 21       1            int 21h
 003B  BB 0063 R          mov bx,offset buf
 003E  8B 0E 0061 R          mov cx,count
 0042  8A 07   start2: mov al,[bx]
 0044  E8 0042           call aldisp
            dispchar ','
 0047  B2 2C       1            mov dl,','
 0049  B4 02       1            mov ah,2
 004B  CD 21       1            int 21h
 004D  43           inc bx
 004E  E2 F2           loop start2
            dispmsg crlf
 0050  BA 005E R      1            mov dx,offset crlf
 0053  B4 09       1            mov ah,9
 0055  CD 21       1            int 21h
 0057  BB 0063 R          mov bx,offset buf
 005A  8B 0E 0061 R          mov cx,count
 005E  E8 005A           call sorting
            dispmsg msg3
 0061  BA 003C R      1            mov dx,offset msg3
 0064  B4 09       1            mov ah,9
 0066  CD 21       1            int 21h
 0068  BB 0063 R          mov bx,offset buf
 006B  8B 0E 0061 R          mov cx,count
 006F  8A 07   start3: mov al,[bx]
 0071  E8 0015           call aldisp
            dispchar ','
 0074  B2 2C       1            mov dl,','
 0076  B4 02       1            mov ah,2
 0078  CD 21       1            int 21h
 007A  43           inc bx
 007B  E2 F2           loop start3
            dispmsg crlf
 007D  BA 005E R      1            mov dx,offset crlf
 0080  B4 09       1            mov ah,9
 0082  CD 21       1            int 21h
 0084    start4: .exit 0
 0084  B8 4C00     *     mov    ax, 04C00h
 0087  CD 21     *     int    021h
            include sub30-1.asm        ;
         C ;;;;;;;;sub30-1.asm;;;;;;;;;
 0089         C aldisp   proc
 0089  50        C          push ax
 008A  52        C          push dx
 008B  50        C          push ax
 008C  8A D0        C          mov dl,al
 008E  D0 EA        C          shr dl,1
 0090  D0 EA        C          shr dl,1
 0092  D0 EA        C          shr dl,1
 0094  D0 EA        C          shr dl,1
 0096  80 CA 30        C          or dl,30h
 0099  80 FA 39        C          cmp dl,39h
 009C  76 03        C          jbe aldisp1
 009E  80 C2 07        C          add dl,7
 00A1  B4 02        C aldisp1: mov ah,2
 00A3  CD 21        C          int 21h
 00A5  5A        C          pop dx
 00A6  80 E2 0F        C          and dl,0fh
 00A9  80 CA 30        C          or dl,30h
 00AC  80 FA 39        C          cmp dl,39h
 00AF  76 03        C          jbe aldisp2
 00B1  80 C2 07        C          add dl,7
 00B4  B4 02        C aldisp2: mov ah,2
 00B6  CD 21        C          int 21h
 00B8  5A        C          pop dx
 00B9  58        C          pop ax
 00BA  C3        C          ret
 00BB         C aldisp   endp
         C
 00BB         C sorting  proc
 00BB  83 F9 00        C          cmp cx,0
 00BE  74 24        C          je sortend
 00C0  83 F9 01        C          cmp cx,1
 00C3  74 1F        C          je sortend
 00C5  50        C          push ax
 00C6  52        C          push dx
 00C7  56        C          push si
 00C8  8B F3        C          mov si,bx
 00CA  49        C          dec cx
 00CB  8B D1        C outlp:   mov dx,cx
 00CD  8B DE        C          mov bx,si
 00CF  8A 07        C inlp:    mov al,[bx]
 00D1  3A 47 01        C          cmp al,[bx+1]
 00D4  76 05        C          jna next
 00D6  86 47 01        C          xchg al,[bx+1]
 00D9  88 07        C          mov [bx],al
 00DB  43        C next:    inc bx
 00DC  4A        C          dec dx
 00DD  75 F0        C          jnz inlp
 00DF  E2 EA        C          loop outlp
 00E1  5E        C          pop si
 00E2  5A        C          pop dx
 00E3  58        C          pop ax
 00E4  C3        C sortend: ret
 00E5         C sorting  endp
         C
         C convert  macro
         C          local input21,input22
         C          local input24,input25
         C          cmp dl,0
         C          je input21
         C          sub dl,7
         C input21: and dl,0fh
         C          cmp dh,0
         C          je input24
         C          cmp dh,'9'
         C          jbe input22
         C          sub dh,7
         C input22: shl dh,1
         C          shl dh,1
         C          shl dl,1
         C          shl dl,1
         C          or dl,dh
         C input24: mov [bx],dl
         C          inc bx
         C          inc cx
         C input25:
         C          endm
         C
 00E5         C input    proc
 00E5  50        C          push ax
 00E6  52        C          push dx
 00E7  33 C9        C          xor cx,cx
 00E9  33 D2        C input01: xor dx,dx
 00EB  B4 01        C input02: mov ah,1
 00ED  CD 21        C          int 21h
 00EF  3C 0D        C input10: cmp al,0dh
 00F1  74 72        C          je input30
 00F3  3C 20        C          cmp al,' '
 00F5  74 46        C          je input20
 00F7  3C 2C        C          cmp al,','
 00F9  74 42        C          je input20
 00FB  3C 08        C          cmp al,08h
 00FD  74 30        C          je input17
 00FF  3C 30        C          cmp al,'0'
 0101  72 2C        C          jb input17
 0103  3C 66        C          cmp al,'f'
 0105  77 28        C          ja input17
 0107  3C 61        C          cmp al,'a'
 0109  72 04        C          jb input11
 010B  2C 20        C          sub al,20h
 010D  EB 0C        C          jmp input12
 010F  3C 46        C input11: cmp al,'F'
 0111  77 1C        C          ja input17
 0113  3C 41        C          cmp al,'A'
 0115  73 04        C          jae input12
 0117  3C 39        C          cmp al,'9'
 0119  77 14        C          ja input17
 011B  80 FA 00        C input12: cmp dl,0
 011E  75 04        C          jne input13
 0120  8A D0        C          mov dl,al
 0122  EB C7        C          jmp input02
 0124  80 FE 00        C input13: cmp dh,0
 0127  75 06        C          jne input17
 0129  8A F2        C          mov dh,dl
 012B  8A D0        C          mov dl,al
 012D  EB BC        C          jmp input02
 012F  B2 07        C input17: mov dl,7
 0131  B4 02        C          mov ah,2
 0133  CD 21        C          int 21h
 0135  B2 3F        C          mov dl,'?'
 0137  B4 02        C          mov ah,2
 0139  CD 21        C          int 21h
 013B  EB AC        C          jmp input01
 013D         C input20: convert
 013D  80 FA 00       1C          cmp dl,0
 0140  74 03       1C          je ??0000
 0142  80 EA 07       1C          sub dl,7
 0145  80 E2 0F       1C ??0000: and dl,0fh
 0148  80 FE 00       1C          cmp dh,0
 014B  74 12       1C          je ??0002
 014D  80 FE 39       1C          cmp dh,'9'
 0150  76 03       1C          jbe ??0001
 0152  80 EE 07       1C          sub dh,7
 0155  D0 E6       1C ??0001: shl dh,1
 0157  D0 E6       1C          shl dh,1
 0159  D0 E2       1C          shl dl,1
 015B  D0 E2       1C          shl dl,1
 015D  0A D6       1C          or dl,dh
 015F  88 17       1C ??0002: mov [bx],dl
 0161  43       1C          inc bx
 0162  41       1C          inc cx
 0163        1C ??0003:
 0163  EB 84        C          jmp input01
 0165         C input30: convert
 0165  80 FA 00       1C          cmp dl,0
 0168  74 03       1C          je ??0004
 016A  80 EA 07       1C          sub dl,7
 016D  80 E2 0F       1C ??0004: and dl,0fh
 0170  80 FE 00       1C          cmp dh,0
 0173  74 12       1C          je ??0006
 0175  80 FE 39       1C          cmp dh,'9'
 0178  76 03       1C          jbe ??0005
 017A  80 EE 07       1C          sub dh,7
 017D  D0 E6       1C ??0005: shl dh,1
 017F  D0 E6       1C          shl dh,1
 0181  D0 E2       1C          shl dl,1
 0183  D0 E2       1C          shl dl,1
 0185  0A D6       1C          or dl,dh
 0187  88 17       1C ??0006: mov [bx],dl
 0189  43       1C          inc bx
 018A  41       1C          inc cx
 018B        1C ??0007:
 018B  5A        C          pop dx
 018C  58        C          pop ax
 018D  C3        C          ret
 018E         C input    endp       
         C
            end
Microsoft (R) Macro Assembler Version 6.11      04/27/06 12:50:15
30-1.asm           Symbols 2 - 1
 

Macros:
                N a m e                 Type
convert  . . . . . . . . . . . . Proc
dispchar . . . . . . . . . . . . Proc
dispmsg  . . . . . . . . . . . . Proc

Segments and Groups:
                N a m e                 Size     Length   Align   Combine Class
DGROUP . . . . . . . . . . . . . GROUP
_DATA  . . . . . . . . . . . . . 16 Bit  00C7   Word   Public  'DATA' 
STACK  . . . . . . . . . . . . . 16 Bit  0100   Para   Stack   'STACK' 
_TEXT  . . . . . . . . . . . . . 16 Bit  018E   Word   Public  'CODE' 

Procedures,  parameters and locals:
                N a m e                 Type     Value    Attr
aldisp . . . . . . . . . . . . . P Near  0089   _TEXT Length= 0032 Public
  aldisp1  . . . . . . . . . . . L Near  00A1   _TEXT 
  aldisp2  . . . . . . . . . . . L Near  00B4   _TEXT 
input  . . . . . . . . . . . . . P Near  00E5   _TEXT Length= 00E9 Public
  input01  . . . . . . . . . . . L Near  00E9   _TEXT 
  input02  . . . . . . . . . . . L Near  00EB   _TEXT 
  input10  . . . . . . . . . . . L Near  00EF   _TEXT 
  input11  . . . . . . . . . . . L Near  010F   _TEXT 
  input12  . . . . . . . . . . . L Near  011B   _TEXT 
  input13  . . . . . . . . . . . L Near  0124   _TEXT 
  input17  . . . . . . . . . . . L Near  012F   _TEXT 
  input20  . . . . . . . . . . . L Near  013D   _TEXT 
  ??0000 . . . . . . . . . . . . L Near  0145   _TEXT 
  ??0001 . . . . . . . . . . . . L Near  0155   _TEXT 
  ??0002 . . . . . . . . . . . . L Near  015F   _TEXT 
  ??0003 . . . . . . . . . . . . L Near  0163   _TEXT 
  input30  . . . . . . . . . . . L Near  0165   _TEXT 
  ??0004 . . . . . . . . . . . . L Near  016D   _TEXT 
  ??0005 . . . . . . . . . . . . L Near  017D   _TEXT 
  ??0006 . . . . . . . . . . . . L Near  0187   _TEXT 
  ??0007 . . . . . . . . . . . . L Near  018B   _TEXT 
sorting  . . . . . . . . . . . . P Near  00BB   _TEXT Length= 002A Public
  outlp  . . . . . . . . . . . . L Near  00CB   _TEXT 
  inlp . . . . . . . . . . . . . L Near  00CF   _TEXT 
  next . . . . . . . . . . . . . L Near  00DB   _TEXT 
  sortend  . . . . . . . . . . . L Near  00E4   _TEXT 

Symbols:
                N a m e                 Type     Value    Attr
@CodeSize  . . . . . . . . . . . Number  0000h 
@DataSize  . . . . . . . . . . . Number  0000h 
@Interface . . . . . . . . . . . Number  0000h 
@Model . . . . . . . . . . . . . Number  0002h 
@Startup . . . . . . . . . . . . L Near  0000   _TEXT 
@code  . . . . . . . . . . . . . Text     _TEXT
@data  . . . . . . . . . . . . . Text     DGROUP
@fardata?  . . . . . . . . . . . Text     FAR_BSS
@fardata . . . . . . . . . . . . Text     FAR_DATA
@stack . . . . . . . . . . . . . Text     DGROUP
buf  . . . . . . . . . . . . . . Byte  0063   _DATA 
count  . . . . . . . . . . . . . Word  0061   _DATA 
crlf . . . . . . . . . . . . . . Byte  005E   _DATA 
maxcount . . . . . . . . . . . . Number  0064h 
msg1 . . . . . . . . . . . . . . Byte  0000   _DATA 
msg2 . . . . . . . . . . . . . . Byte  001F   _DATA 
msg3 . . . . . . . . . . . . . . Byte  003C   _DATA 
start2 . . . . . . . . . . . . . L Near  0042   _TEXT 
start3 . . . . . . . . . . . . . L Near  006F   _TEXT 
start4 . . . . . . . . . . . . . L Near  0084   _TEXT 
    0 Warnings
    0 Errors
 
30-1.MAP
 
 Start  Stop   Length Name                   Class
 00000H 0018DH 0018EH _TEXT                  CODE
 0018EH 00254H 000C7H _DATA                  DATA
 00260H 0035FH 00100H STACK                  STACK
 Origin   Group
 0018:0   DGROUP
  Address         Publics by Name
 0000:0089       aldisp
 0000:00E5       input
 0000:00BB       sorting
  Address         Publics by Value
 0000:0089       aldisp
 0000:00BB       sorting
 0000:00E5       input
Program entry point at 0000:0000
       
形成这三个文件后,只要汇编、连接主程序,就可以将另外2个文件包含进去,最终生成一个可执行程序。
阅读(3579) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~