Ch 26 ¥¨¶°

¤p¤ì°¸³Ì±`¥Îªº²ÕĶ¾¹¬O·L³n¥X«~ªº MASM¡A¨ä¤¤ªº¡yASM¡z·íµM¬O«ü¡yassembly¡z¤§Â²¼g¡A¦ý¬O±z¥iª¾¹D²Ä¤@­Ó¡yM¡z¥Nªí¤°»ò·N¸q¡H¥i¯à¦³³\¦h¤H¥H¬°¬O·L³n ( Microsoft ) ªº²Ä¤@­Ó¦r¥À¡A¤£¹L³o¬O¤£¹ïªº¡A¦]¬°¤@¯ë¹ï·L³nªºÂ²ºÙ¬O¡yMS¡z¡A©Ò¥H¤p¤ì°¸¥H¬°³o­Ó¡yM¡z»P¨ä¥Nªí·L³n¤£¦p¥Nªí¥¨¶° ( macro ) ¨Ó±o¥¿½T¡C¥Ñ©R¦W´N¥i¥Hª¾¹D¨Ï¥Î¥¨¶°À³¸Ó¤]¬O¬Û·í­«­nªº¡A©Ò¥H¦b³o¤@³¹ùØ¡A¤p¤ì°¸¥´ºâ¤¶²Ð¦p¦ó¨Ï¥Î¥¨¶°¡C

¦ó¿×¥¨¶°¡H²³æªº»¡¬O¥Î¨Ó²¤Æ¼¶¼g­ì©lµ{¦¡ªº¤@ºØ»yªk¡C²Õ¦X»y¨¥¤ñ¨ä¥L°ª¶¥»y¨¥³Ì¤jªº¦n³B¬O®Ä²v»P©Ò¦ûÅé¿n¤p¡A¦ý¬O¼¶¼g²Õ¦X»y¨¥µ{¦¡©Òªáªººë¤O«o»·¤ñ°ª¶¥»y¨¥¦h¡A¦Ó¨äºûÅ@¦¨¥»¤]°ª¡C­ì¦]¬O²Õ¦X»y¨¥¨C°õ¦æ¤@­Ó°Ê§@­n¼Æ¦æ¡A¬Æ¦Ü¼Æ¤Q¦æ¡B¼Æ¦Ê¦æ«ü¥O¤~¯à§¹¦¨¡C°²¦p±z¥Î BASIC ¼g¹Lµ{¦¡¡A­n¦b¿Ã¹õ¤W¦L¥X¡yHellow, world!¡z³o­Ó¦r¦êªº¸Ü¡A¥u»Ý¼g

PRINT "Hellow, world!"

§Y¥i¡A¦ý¥Î²Õ¦X»y¨¥ªº¸Ü¥²¶·³o¼Ë¼g

message db      'Hellow, world!$'
        mov     ah,9
        mov     dx,offset message
        int     21h

¦Ü¤Ö»Ý­n¥|¦æ«ü¥O¤~¯à§¹¦¨¡C·L³n¦³Å²©ó¦¹¡A©ó¬O§â¥Lµo¦æªº²ÕĶ¾¹¥[¤J¥¨¶°ªº¼gªk¡A¨Ï¨ä¯à²¤Æ­ì©lµ{¦¡¤§¼¶¼g¡A¬Æ¦Ü¥i¥H©M°ª¶¥»y¨¥ªº¼gªk«ÜÃþ¦ü¡C


¥Î¥¨¶°¼g²Õ¦X»y¨¥µ{¦¡

©³¤U¤p¤ì°¸±N¤¶²Ð¦p¦ó²¤Æ¡H¥H²Ä¤@³¹ªº EXAM01.ASM ¬°¨Ò§a¡I­ì¨Óªºµ{¦¡½X¬O¡G

;***************************************    ;01
code    segment                             ;02.code ¬q¶}©l¦ì§}
        assume  cs:code,ds:code             ;03.°²³]µ{¦¡¬q¤Î¸ê®Æ¬q
        org     100h                        ;04.¥i§ï¦¨ *.COM ÀÉ
;---------------------------------------    ;05.
start:  jmp     begin                       ;06.µ{¦¡¶i¤JÂI
mes     db      'Hi, I learn assembly.$'    ;07.­n¦L¥Xªº°T®§
begin:  mov     dx,offset mes               ;08.«ü¦V mes ªº¦ì§}
        mov     ah,9                        ;09.«ü©w­n©I¥sªºªA°È¸¹½X
        int     21h                         ;10.©I¥s DOS ªA°Èµ{¦¡
        mov     ax,4c00h                    ;11.«ü©w­n©I¥sªºªA°È¸¹½X
        int     21h                         ;12.©I¥s DOS ªA°Èµ{¦¡
;---------------------------------------    ;13.
code    ends                                ;14.code ¬qµ²§ô
;***************************************    ;15.
        end     start                       ;16.¨Ï²ÕĶ¾¹ª¾¹Dµ{¦¡¶i¤JÂI

¤ÀªRµ{¦¡½X (¥Õ¦â³¡¤À)¡A¥i¤À¬°¨â­Ó¨BÆJ¡A²Ä¤@¨B¬O¦L¥X¦r¦ê¡A¥Î¥h¤T¦æµ{¦¡½X¡A²Ä¤G¨B¬Oµ²§ôµ{¦¡¡A¥Î¥h¨â¦æ¡C¤p¤ì°¸¤À§O¥H display¡Bexit ³o¨â­Ó¥¨¶°¥N´À¡Aµ{¦¡Åܦ¨

        page	,132                  ;01 ³]©w­¶ªø»P­¶¼e ( µù¤@ )
display macro   string                  ;02 ©w¸q display ¥¨¶°
        local   st_addr,dsp_str         ;03 ³]©w¼Ð¸¹ dsp_str ¬°§½³¡¼Ð¸¹
        jmp     short dsp_str           ;04 µuµ{¸õÅD
st_addr db      string,'$'              ;05 ¥Ñ¥¨¶°¿é¤J¤Þ¼Æ¨M©w©Ò©w¸q¤§¦r¦ê
dsp_str:                                ;06 ©w¸q dsp_str ¼Ð¸¹
        mov     ah,9
        mov     dx,offset st_addr       ;08 ¨ú±o st_addr ¤§¦ì§}
        int     21h                     ;09 ©ó¿Ã¹õ¤W¦L¥X st_addr ¦r¦ê
        endm                            ;10 display ¥¨¶°µ²§ô

exit    macro   exit_code               ;12 ©w¸q exit ¥¨¶°
        mov     ah,4ch
        mov     al,exit_code            ;14 ¨Ï AL µ¥©óªð¦^½X
        int     21h                     ;15 ©I¥s DOS ªA°È¤¤Â_¥Hµ²§ô¥»µ{¦¡
        endm                            ;16 exit ¥¨¶°µ²§ô

;***************************************
code    segment
        assume  cs:code,ds:code
        org     100h
;---------------------------------------
start:  display 'Hi, I learn assembly.' ;23 ¦L¥X¦r¦ê
        exit    0                       ;24 µ²§ôµ{¦¡
;---------------------------------------
code    ends
;***************************************
        end     start

³o¼Ëµ{¦¡½X ( ¥Õ¦â³¡¤À ) ¥uÅܦ¨¨â¦æ¡A¨Ï¥Î display ¤Î exit ³o¨â­Ó¥¨¶°¡A¦bµ{¦¡¤p®É¤£©ö¬Ý¥X¨äÀuÂI¡A¦ý¬Oµ{¦¡¤j®É´N·|¨Ï±o­ì©lµ{¦¡«Ü®e©ö¾\Ū»P­×§ï¡C­n¨Ï¥Î¥¨¶°¡A¥²¶·¥ý©w¸q¥¨¶°¡A¤@¯ë¥¨¶°¥i¥H¼g¦b­ì©lµ{¦¡ùØ¡A¤]¥i¥H¼g¦b¥¨¶°µ{¦¡®w ( §t¤JÀÉ ) ùØ¡A¦pªGª½±µ¼g¦b­ì©lµ{¦¡ùØ¡A¤@¯ë¬O¦bµ{¦¡³Ì«e­±¥ý©w¸q¨ä¤º®e¡C

©w¸q¥¨¶°¡GMACRO »P ENDM

¥¨¶°ªº¤º®e¥²¶·©w¸q¦b macro ©M endm ³o¨â­Ó¦r¤§¶¡¡A¨ä§Î¦¡¹³

name    macro   x,y,z¡K¡K
        ¡K¡K¡K
        µ{¦¡½X
        ¡K¡K¡K
        endm

name ¬O¸Ó¥¨¶°ªº¦WºÙ¡A¤]´N¬Oµ¹µ{¦¡¨Ï¥Î®É¡y©I¥s¡z¤§¥Îªº¡Cx¡By¡Bz¡K¡Kµ¥¬O¸Ó¥¨¶°ªº¿é¤J¤Þ¼Æ¡A¿é¤J¤Þ¼Æ¥i¥H¦³¤@­Ó¡B©Î¨â­Ó¡B©Î¤T­Ó¡K¡Kª½¨ì MASM ²ÕĶ¾¹ªº²Å¸¹°Ï³Q¶ñº¡¬°¤î¡A¥¨¶°¤]¥i¥H¨S¦³¿é¤J¤Þ¼Æ¡A¥þµø»Ý­n¦Ó©w¡C¥¨¶°¦WºÙ¥i¥H¥ô·N¨ú¦W¡A¦ý¤£¥i¥Î¨ì MASM ªº«O¯d¦r©Î CPU «ü¥O¡C¥¨¶°µ²§ô®É¥Î endm ªí¥Ü¡A³o­Ó¡ym¡z§Yªí¥Ü¥¨¶°¤§·N¡A­nª`·Nªº¬O endm ¤§«e¤£¥i¥[¥¨¶°¦WºÙ¡A²ÕĶ¾¹·|¦Û°Ê°t¦X¡C°²¦p±z¦³»Ý­n¡A¤]¥i¥H¦b¥¨¶°¤¤¨Ï¥Î¥t¥~¤@­Ó¥¨¶°¡C

¥¨¶°®i¶}

­ì©lµ{¦¡¤¤¨Ï¥Î¥¨¶°®É¡Aª½±µ¼g¥X¥¨¶°¦WºÙ¤Î¨ä¿é¤J¤Þ¼Æ§Y¥i¡A´N¹³¤W­zµ{¦¡ªº²Ä 23¡B24 ¦æ¡C·í²ÕĶ¾¹²ÕĶ­ì©lÀɮɡA²ÕĶ¾¹·|¦Û°Ê¥H¥¨¶°¤º®e¨ú¥N¸Ó¦æ¥¨¶°¦WºÙ¡A¥H¥¨¶°¦WºÙ«á¤§¼Æ­È¨ú¥N¤Þ¼Æ¡C¹³¤W­±ªº¨Ò¤l¡A·í²ÕĶ¾¹¬Ý¨ì­ì©lµ{¦¡ªº²Ä 24 ¦æ¡Aexit 0¡A¥Ñ«e­±©w¸qª¾¹D³o¬O¤@­Ó¥¨¶°¡A©ó¬O exit 0¡A³o¤@¦æ¦Û°ÊÅܦ¨

        mov     ah,4ch
        mov     al,quit_code
        int     21h

¦Ó quit_code ³Q¿é¤J¤Þ¼Æ¡A0¡A¨ú¥N¡A©ó¬O¶i¤@¨BÅܦ¨

        mov     ah,4ch
        mov     al,0
        int     21h

¹³³oºØ¨ú¥N¤u§@ºÙ¤§¬°¥¨¶°¥N´« ( macro subtitution ) ©Î¥¨¶°®i¶} ( macro expansion )¡C¥¨¶°®i¶}»P©I¥s°Æµ{¦¡¨Ã¤£¬Û¦P¡A¨C¤@¦¸®i¶}¥¨¶°®É¡A²ÕĶ¾¹³£¬O§â¥¨¶°µ{¦¡½X·Ó§Û¤@¹M¡A¥u¦³¿é¤J¤Þ¼Æ¤£¦P¦Ó¤w¡A©Ò¥H¦pªG±zªºµ{¦¡®i¶} 10 ¦¸¬Û¦Pªº¥¨¶°¡A¨ä°õ¦æÀÉ®×¼W¥[ªº¤j¤p¤j¬ù¬O¥¨¶°ªº 10 ­¿¡A·í°õ¦æ¨ì®i¶}ªº¥¨¶°®É¡Aµ{¦¡·ÓµÛ®i¶}ªºµ{¦¡½X°õ¦æ¡A¤£Âಾ¬yµ{¡C¦Ó©I¥s°Æµ{¦¡¬O¨Ïµ{¦¡¬yµ{Âಾ¨ì°Æµ{¦¡³B°õ¦æ¡A°õ¦æ§¹°Æµ{¦¡«á¤Sªð¦^¥Dµ{¦¡¡A¬G§Y¨Ï±z©I¥s 10 ¦¸¬Û¦Pªº°Æµ{¦¡¡A¦Ó¸Ó°Æµ{¦¡¥u¦³¤@¬q¡A¦Ó¥B©I¥s°Æµ{¦¡®É¥²¶·¥ý¨Ï¤U­Ó«ü¥O¦ì§}±À¤J°ïÅ|¡A¦A²¾Âà¦Ü°Æµ{¦¡³B¡C

°²«ü¥O¡GLOCAL ( §½³¡ )

¦n¤F¡Aexit ¥¨¶°¤w¸g¤¶²Ð¹L¤F¡A¨º display ¥¨¶°¸Ì­±¦h¤F¤@­Ó©_©Çªº°²«ü¥O LOCAL ¬O¤°»ò§@¥Î©O¡H¥ý¤£ºÞ LOCAL ³o­Ó°²«ü¥O¡A§Ú­Ì¥ý§â dispaly ¥¨¶°®i¶}¡A¬Ý¨ì²Ä 23 ¦æ¦³ display ªº¿é¤J¤Þ¼Æ¡Astring¡A¬°¤@­Ó¦r¦ê¡A¡yHi, I learn assembly.¡z¡A©Ò¥H¸Ó¥¨¶°©Ò¦³ªº string ³£Åܦ¨¡yHi, I learn assembly.¡z¡A¦A¬Ý²Ä 5 ¦æ¡A´NÅܦ¨¡G

st_addr db      'Hi, I learn assembly.','$'

©Ò¥H²Ä 23 ¦æ®i¶}«áÅܦ¨²Ä 4 ¦æ¨ì²Ä 9 ¦æ¡A¥u¬O st_addr ¦r¦êÅܦ¨¤W­z¤º®e¤F¡C³o¼Ë¦ü¥G¤@¤Á³£«Ü§¹¬ü¡AÀ³¸Ó¤£»Ý­n local ³o­Ó°²«ü¥O§a¡Hªº½T¡A¦b³o­Óµ{¦¡ùØ¡A¦³¨S¦³ local ¬O¤@¼Ëªº¡A¦ý¬O¦pªG¦b³o­Óµ{¦¡ùئA¨Ï¥Î¤@¦¸ display ¥¨¶°®É¡AMASM ´N·|µLªk²ÕĶ¡A¥¦·|§i¶D±z­«½Æ©w¸qÅܼƦW¤Î¼Ð°O¦W¡A­ì¦]¬O display ®i¶}®É¡A·|²£¥Í¨â­Ó st_addr ¦r¦êÅܼƤΨâ­Ó dip_str ¼Ð°O¡C

¨Æ¹ê¤W¡A±zª¾¹D¡A³o­Ó¦r¦ê»P¼Ð°O¥Î¤°»ò¦WºÙ¨ä¹ê¨Ã¤£­«­n¡A¬°¤F¸Ñ¨M³o­Ó°ÝÃD¡AMASM ´£¨Ñ¤@­Ó local °²«ü¥O¡Alocal ªí¥Üst_addr¡Bdsp_str ¬O¤@­Ó§½³¡ÅܼƻP§½³¡¼Ð°O¡A¥¦ªº¦WºÙ¥u¦b¥»¦¸¨Ï¥Îªº¥¨¶°ùئ³®Ä¡A·í¤U¦¸¦A¨Ï¥Î³o­Ó¥¨¶°®É¡A°Ï°ìÅܼƻP§½³¡¼Ð°Oªº¦WºÙ§ïÅÜ¡C¨Æ¹ê¤W¡AMASM °Ï°ìÅܼƷ|¨Ì¡y??NNNN¡zªº¶¶§Ç§ó´«¡A¦¹³Bªº NNNN ¬O¥Ñ 0000 ¶}©lªº¤Q¤»¶i¦ì¾ã¼Æ¨Ì¦¸»¼¼W¡C©Ò¥H²Ä¤@¦¸¨Ï¥Î display ®É¡Ast_addr ·|Åܬ°¡y??0000¡z¡Adsp_str Åܦ¨¡y??0001¡z¡A²Ä¤G¦¸¨Ï¥Î display ®É¡Ast_addr Åܦ¨¡y??0002¡z¡Adsp_str Åܦ¨¡y??0003¡z¡K¡K¡A³o¼Ë display ´N¯à­«½Æ¨Ï¥Î¤F¡C

¦¹¥~¡A¨Ï¥Î local °²«ü¥O®É¡AÁÙ¦³¤@ÂI­nª`·N¡A§_«h¥²©w·|¥X¿ù¡C¨º´N¬O local ¤@©w­n©ñ¦b¥¨¶°¤º®eªº²Ä¤@¦æ¡C¦n§a¡I©³¤U§Ú­Ì¨Ó¬Ý¬Ý EXAM03.ASM ²ÕĶ®É©Ò«Ø¥ßªº EXAM03.LST ÀÉ¡A¥¨¶°®i¶}ªº¼Ë¤l¡C

H:\HomePage\SOURCE>masm exam03 [Enter]
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987.  All rights reserved.

Object filename [exam03.OBJ]: [Enter]
Source listing  [NUL.LST]: exam03 [Enter]  ¡÷¿é¤J LST ÀÉ¥HÆ[¹î¥¨¶°
Cross-reference [NUL.CRF]: [Enter]

  50964 + 365948 Bytes symbol space free

      0 Warning Errors
      0 Severe  Errors

H:\HomePage\SOURCE>link exam03; [Enter]

Microsoft (R) Personal Computer Linker  Version 2.40
Copyright (C) Microsoft Corp 1983, 1984, 1985.  All rights reserved.

Warning: no stack segment

H:\HomePage\SOURCE>exe2bin exam03 exam03.com [Enter]

H:\HomePage\SOURCE>dir exam03.* [Enter]

 Volume in drive H is DATA_1
 Volume Serial Number is 0330-08F6
 Directory of H:\HomePage\SOURCE

EXAM03   LST         3,236  02-08-03   0:19 EXAM03.LST
EXAM03   OBJ           107  02-08-03   0:19 EXAM03.OBJ
EXAM03   EXE           805  02-08-03   0:19 EXAM03.EXE
EXAM03   COM            37  02-08-03   0:20 EXAM03.COM
EXAM03   ASM         1,279  02-08-03   0:00 EXAM03.ASM
         5 file(s)          5,464 bytes
         0 dir(s)        3,689.07 MB free

H:\HomePage\SOURCE>

LST ¬O¤@­Ó¦CªíÀÉ¡AÄÝ©ó¯Â¤å¦rÀÉ¡A±z¥i¥H¥Î¥ô¦ó¤@­Ó¤å®Ñ³B²z¾¹¶}±Ò¥¦¡A¨ÃÆ[¹î¥¦¡A·|¬Ý¨£¤U­±ªºµ{¦¡½X¡C

   start:  display 'Hi, I learn assembly.'
1          jmp     short ??0001
1  ??0000 db      'Hi, I learn assembly.',
1  ??0001:
1          mov     ah,9
1          mov     dx,offset ??0000
1          int     21h
           exit    0
1          mov     ah,4ch
1          mov     al,0
1          int     21h

¶À¦âªº¡y1¡zªí¥Ü¥¨¶°®i¶}¡Aª`·N¨ì¬õ¦â³¡¤ÀªºÅܼƦW»P¼Ð°O¦W³£³Q§ó´«¤F¡C


¥¨¶°µ{¦¡®w

°²¦p±z¼g¤F³\¦h¥¨¶°µ{¦¡¡A¨º»ò±z¤]¥i¥H§â³o¨Ç¥¨¶°¶°¦X°_¨ÓÅܦ¨¤@­Ó¯Â¤å¦rÀÉ¡A³o¼Ë´N«Ø¥ß°_¥¨¶°µ{¦¡®w¡A·í±z¼¶¼g·sªºµ{¦¡®É¥i¥H§â³o­Ó¥¨¶°µ{¦¡®w¥]§t¶i¨Ó¡C¦p¦¹¤@¨Ó¡A±z¥u­n¼g¤@¦¸¥¨¶°¡A´N¥i¥H¦b³\¦hµ{¦¡ùبϥΡA¸`¬Ù³\¦h·Ðº¾ªº¤u§@¡A°£¦¹¤§¥~¡A±zªºµ{¦¡¬Ý°_¨Ó·|«Ü²¼ä¡A´N·|¹³°ª¶¥»y¨¥¤@¼Ë¤ñ¸û¦nºûÅ@¡C

¨Æ¹ê¤W¡AMASM ¦w¸Ë¦n«á¡AINCLUDE ¤l¥Ø¿ýùØ­±¡A´N´£¨Ñ³\¦h¥¨¶°µ{¦¡®w¡C¥¨¶°µ{¦¡®w©M²Ä 10 ³¹©Ò´£¨ìªºµ{¦¡®w¬O¤£¤@¼Ëªº¡A¥¨¶°µ{¦¡®w¬O¥Ñ¤å®Ñ³B²z¾¹½s¿è¦Ó¦¨ªº¯Â¤å¦rÀÉ¡A´£¨Ñ MASM ²ÕĶ¥Îªº¡A¤@¯ë°ÆÀɦW¬O *.H¡B*.INC ©Î *.MAC ( H¡BINC¡BMAC ¤À§O¬O head¡Binclude¡Bmacro ¤§·N )¡F¦Óµ{¦¡®w¬O°Æµ{¦¡ªº¥ØªºÀɸg¥Ñ LIB ½s½X¨Ã¶°¦X°_¨Ó©Ò»s§@ªº¤G¶i¦ìÀÉ¡A¬O´£¨Ñµ¹ LINK ³sµ²¥Îªº¡A°ÆÀɦW¬O *.LIB¡C

°²«ü¥O¡GINCLUDE

INCLUDE °²«ü¥O´N¬O¬°¤F¦b­ì©lÀÉùاt¬A¥¨¶°µ{¦¡Àɪº¡A»yªk¬O

include ¥¨¶°µ{¦¡ÀÉÀɦW

³o­ÓÀɦW¥i¥H¥]§tºÏºÐ¾÷¦W¡B¸ô®|¡C

°²«ü¥O¡GPURGE

°²¦p±zªº¥¨¶°µ{¦¡ÀɤӤj¡A¥H­P¥]§t¤F³\¦h¥¨¶°¡A¦ý±z¥i¯à¥u¥Î¤F¨ä¤¤ªº¤@³¡¥÷¡A¦Ó¨S¥Î¨ìªº³¡¤ÀÁöµM¤£·|³Q²ÕĶ¡A¦ý¬O²ÕĶ®É·|®ø¯Ó°O¾ÐÅé¡A¥H­P¨Ï MASM ²Å¸¹ªÅ¶¡¤£°÷¥Î¡A³o®É­Ô¥i¥H¥Î purge °²«ü¥O¨Ï MASM ©¿²¤¥¦¡C¨ä»yªk¬°

purge   ¥¨¶°¦W1, ¥¨¶°¦W2, ¥¨¶°¦W3¡K¡K

±µ¦b purge ¤§«áªº´N¬O­n¨Ï¨ä¤£§@¥Îªº¥¨¶°¡C

­×¥¿µ{¦¡½X»P¸ê®Æ¥æ¤e¦w¸m

EXAM03.ASM ³o­Óµ{¦¡ùØ¡Aµ{¦¡½X»P¸ê®Æ¤¬¬Û¥æ¤e¡A¬°¤F§ïµ½³o­Ó¯ÊÂI¡A¤p¤ì°¸§â display ¥¨¶°¥[¥H§ï¨}¡C©³¤U¬O§ï¨}«áªº¥¨¶°µ{¦¡®w¡G

display macro   string
        local   str
data    segment para    public  'data'
str     db      string,'$'
data    ends
code    segment para    public  'code'
        mov     ah,9
        mov     dx,offset str
        int     21h
code    ends
endm

exit    macro   quit_code
code    segment para    public  'code'
        mov     al,quit_code
        mov     ah,4ch
        int     21h
code    ends
endm

initial macro
stack   segment stack
        db      128 dup ('stack   ')
stack   ends
data    segment para    public  'data'
data    ends
code    segment para    public  'code'
        assume  cs:code,ds:data
start:  push    ds
        sub     ax,ax
        push    ax
        mov     ax,data
        mov     ds,ax
code    ends
endm

§â¤W­zµ{¦¡¦s¦¨ MYMAC.INC ¥¨¶°µ{¦¡®w¡A¦Ó­ì©lµ{¦¡Åܦ¨¡G

        include mymac.inc

        initial
        display 'I learn Macro Assembly.'   ;¦L¥X¡yI learn Macro Assembly.¡z¦r¦ê
        display <0dh,0ah>                   ;´«¦æ
        display 'It is very fun!'           ;¦L¥X¡yIt is very fun!¡z¦r¦ê
        exit    0

        end     start

¤p¤ì°¸§â³o­Óµ{¦¡¨ú¦W¬° EXAM04.ASM¡A±z¬Ý³o­Óµ{¦¡¤@ÂI¤]¤£¹³²Õ¦X»y¨¥¡A­Ë¹³¬O¤@ºØ°ª¶¥»y¨¥¡A¦ý¬O¥¦²ÕĶ«á¥i°õ¦æÀɤj«o«Ü¤p¡A¥Î DEBUG Æ[¹î¡A§¹§¹¥þ¥þ¬O²Õ¦X»y¨¥¡A³o´N¬O¤p¤ì°¸¦b¥»³¹¤@¶}©l»¡¡A¥¨¶°Â²¤Æ¤F²Õ¦X»y¨¥¡A¨Ï²Õ¦X»y¨¥»yªkÁͦV°ª¶¥»y¨¥¡C¤£¹L©O¡A±z±o°O±o¡A³o¥u¬Oµ{«×¤Wªº°ÝÃD¡A¨Æ¹ê¤W­n¥Î¥¨¶°¨Ï²Õ¦X»y¨¥¯à§¹¥þ¹³°ª¶¥»y¨¥¤@¼Ë¡AÁÙ¦³¦n¤j¤@¬q¶ZÂ÷¡AÁÙ±o°t¦X¡y±ø¥ó²ÕĶ¡z¤~¯à±µªñ³o­Ó¥Øªº¡Cª`·N¡I¤p¤ì°¸¥u»¡¡y±µªñ¡z¦Ó¤w¡A¨Ã¨S¦³»¡¯à§¹¥þ¹³°ª¶¥»y¨¥¤@¼Ë¡C¦n§a¡I©³¤U§Ú­Ì¬Ý¬Ý³o­Óµ{¦¡»P¥¨¶°®w¡C

¦b³o­Ó¥¨¶°ùØ¡A¤p¤ì°¸¥Î¤F©³¤Uªº¤@­ÓÆ[©À¡C·í§Ú­Ì¦bµ{¦¡¤¤©w¸q¤@­Ó°Ï¬q®É¡A¥i¥H§â³o­Ó°Ï¬q¤À´²¶}¨Ó¡A¥u­n¦b¦X¨Ö§Î¦¡»PÃþ§O¦W¥[¥H¾A·íªº©w¸q¡A³sµ²¾¹´N·|¦Û°Ê¦a§â³o­Ó¤À´²©ó­ì©lµ{¦¡¦U³Bªº°Ï¬q¤ù¬q¡A¦X¦¨¤@­Ó³sÄòªº°Ï¬q¡A½Ð°Ñ¦Ò²Ä 11 ³¹¦³Ãö segment °²«ü¥Oªº»¡©ú¡A¥H³o­Óµ{¦¡¦Ó¨¥¡A¤p¤ì°¸§â©Ò¦³¤À´²¦U³Bªº data °Ï¬q¤§Ãþ§O¦W¨ú¦W¬°¡ydata¡z¡Acode °Ï¬q¨ú¦W¬°¡ycode¡z¡A¨Ã¥B¦X¨Ö§Î¦¡«ü©w¬°¡ypublic¡z¡Aªí¥Ü¬Û¦P¤§Ãþ§O¦W»P°Ï¬q¦W¦X¦¨¤@³sÄò°Ï¬q¡C¦p¤U¹Ï¡A¥ªÃä¬O®i¶}«áªº­ì©lµ{¦¡¡A¦Ó¥kÃä¬O³sµ²¾¹¦X¨Ö«áªº¥i°õ¦æÀɤºªºµ{¦¡½X¡C

stack   segment stack
        db      128 dup ('stack   ')
stack   ends
data    segment para    public  'data'
data    ends
code    segment para    public  'code'
        assume  cs:code,ds:data
start:  push    ds
        sub     ax,ax
        push    ax
        mov     ax,data
        mov     ds,ax
code    ends
data    segment para    public  'data'
??0000  db      'I learn Macro Assembly.','$'
data    ends
code    segment para    public  'code'
        mov     ah,9
        mov     dx,offset ??0000
        int     21h
code    ends
data    segment para    public  'data'
??0001  db      0dh,0ah,'$'
data    ends
code    segment para    public  'code'
        mov     ah,9
        mov     dx,offset ??0001
        int     21h
code    ends
data    segment para    public  'data'
??0002  db      'It is very fun!','$'
data    ends
code    segment para    public  'code'
        mov     ah,9
        mov     dx,offset ??0002
        int     21h
code    ends
code    segment para    public  'code'
        mov     al,0
        mov     ah,4ch
        int     21h
code    ends
stack   segment stack
        db      128 dup ('stack   ')
stack   ends
data    segment para    public  'data'
??0000  db      'I learn Macro Assembly.','$'
??0001  db      0dh,0ah,'$'
??0002  db      'It is very fun!','$'
data    ends
code    segment para    public  'code'
start:  push    ds
        sub     ax,ax
        push    ax
        mov     ax,data
        mov     ds,ax
        mov     ah,9
        mov     dx,offset ??0000
        int     21h
        mov     ah,9
        mov     dx,offset ??0001
        int     21h
        mov     ah,9
        mov     dx,offset ??0002
        int     21h
        mov     al,0
        mov     ah,4ch
        int     21h
code    ends

°ïÅ|¬q¥u¦³¤@­Ó¡A¤£»P¨ä¥L°Ï¬q¦X¨Ö¡Fµ{¦¡½X°Ï¬q¤À´² 5 ³B¡A¤p¤ì°¸¥H¶À¦âªí¥Ü¡F¸ê®Æ°Ï¬q¤À´² 4 ³B¡A¥HÂŦâªí¥Ü¡C³sµ²«á¡A5 ³Bµ{¦¡½X°Ï¬q·|¦X¨Ö¦¨¤@­Ó³sÄòªº°Ï¬q¡A4 ³B¸ê®Æ°Ï¬q¤]·|¦X¨Ö¦¨¤@­Ó³sÄòªº°Ï¬q¡C

< »P > ¦r¦ê¹Bºâ¤l

¤p¤ì°¸·Q¡A±z¥i¯à¤w¸g²q±oµÛ¡A³o¹ï¨¤¬A¸¹ªº·N«ä¬O¨Ï¦b¨¤¬A¸¹¤ºªº©Ò¦³¹Bºâ¤¸Åܦ¨¤@­Ó¦r¦êÅܼơA%¡B&¡B;;¡B! µ¥¯S®í¦r³£·|¥¢¥h®Ä¤O¡AÅܦ¨¦r¤¸¡A©M¨¤¬A¸¹ªº¨ä¥L¦rÅܦ¨¤@­Ó¦r¦ê¡C¦b³o­Ó¨Ò¤lùØ¡A°²¦p±z¥Î

display 0dh,0ah

ªº¸Ü¡Aµ²ªG¥u¦³ 0dh ³QÅã¥Ü©ó¿Ã¥ú¹õ¤W¡A³o¬O¦]¬° display ¥¨¶°¥u¦³¤@­Ó¤Þ¼Æ¡A©Ò¥Hµ²ªG¨Ã¤£¬O§Ú­Ì·Q­nªº¡C©³¤U¦A¤¶²Ð´XºØ¦b¥¨¶°ùر`¥Îªº¤å¦r¹Bºâ¤l¡C

& ¨ú¥N¹Bºâ¤l

& ¬O±j­¢¨Ï¿é¤J¤Þ¼Æ¨ú¥N¡y&¡z«á­±ªº¹Bºâ¤¸¡A¦Ó¥B¤£ºÞ & ¥X²{¦b¨ºùØ¡C¨Ò¦p¦³­Ó¥¨¶°¬O

error_code      macro   x,y
err&x   db      'Error &x : &y'
        endm

­Y¬O¥Î¤U­±±Ô­z¨Ï¥Î¦¹¥¨¶°

error_code      5,<ºÏºÐ¾÷µLªkŪ¨ú>

®i¶}«áÅܦ¨

err5    db      'Error 5 : ºÏºÐ¾÷µLªkŪ¨ú'

;; µù¸Ñ¹Bºâ¤l

Âù¤À¸¹¤§«áªº¤å¦r·|³Q²ÕĶ¾¹©¿²¤¡A¨Ã¥B¦b *.LST Àɤ¤®i¶}¥¨¶°®É¤£·|¥X²{¡A¥u¦³¦b©w¸q¥¨¶°®É¤~¥X²{¡A¦]¦¹±`¦b¥¨¶°¤¤³Q¥Î¨Ó°µ¬°µù¸Ñ¡C·íµM¡A¦b¥¨¶°¤¤±z¤]¥i¥H¨Ï¥Î¡y;¡z°µ¬°µù¸Ñ¡A¦ý¬O¡y;¡z¦b©w¸q¥¨¶°¥H¤Î®i¶}¥¨¶°®É³£·|¥X²{µù¸Ñ¡C

% ¹Bºâ¦¡¹Bºâ¤l

³o­Ó¹Bºâ¤l¬O¨Ï¨ä«á©Ò±µªº¹Bºâ¦¡ºâ¥X¨Ó¡A¨Ï¥¦±a¶i¥¨¶°ªº¤Þ¼ÆùØ¡A¤]´N¬O»¡¡A¡y%¡z¹Bºâ¤l¥u¯à¥Î¦b¥¨¶°¤Þ¼Æ¦CùØ¡C

! ¦r¤¸¹Bºâ¤l

³o­Ó¹Bºâ¤l¨Ï¨ä«á±µªº¤@­Ó¦r¤¸¤£ºÞ¬O¤£¬O¯S®í¦r¤¸ ( ¨Ò¦p <¡B>¡B%¡B&¡B;¡B! ) ³£¬Ý¦¨¬O¤@­Ó´¶³qªº¦r¤¸¡C¨Ò¦p¨Ï¥Î¤W¨Ò¥¨¶°

error_code      103,<¹Bºâ¤¸ !> 255 >

®i¶}«áÅܦ¨

err103  db      'Error 103 : ¹Bºâ¤¸ > 255 '

­«½Æ°Ï¶ô

¦b¼¶¼gµ{¦¡®É¡A±`±`·|­n©w¸q¸ê®Æ¡A¨Ò¦p©w¸q­^¤å¦r¥À¡A³Ìª½±µªº¤èªk¬O¡G

alpha   db      'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

¦ý¬O¡A¦pªG¸ê®Æ¤Óªø®É¡A´N«Ü¤£¤è«K¡A³o®É¥i¥H¥Î¡y­«½Æ°Ï¶ô¡z¨Ó©w¸q¡CMASM ´£¨Ñªº­«½Æ°Ï¶ô¦³¤TºØ¡A¤£ºÞ¬O¨º¤@ºØ­«½Æ°Ï¶ô¡A³£¨S¦³¦W¦r¡A¦]¦¹¤£¯à³Qµ{¦¡¡y©I¥s¡z¡A¤@¯ë¦Ó¨¥¡A­«½Æ°Ï¶ô³£¬Oª½±µ¤º´O©óµ{¦¡¤¤¡A©Î¥H¥t¤@­Ó¥¨¶°¥]§t°_¨Ó¡C

REPT

³o¬O¥Î¨Ó«Ø¥ß¦³³W«hªº­«½Æ°Ï¶ô¡A¦³³W«hªº·N«ä¬O«ü¨C¦¸ªºÅܤƶq³£¬O¬Û¦Pªº¡C¨ä»yªk¬O¡G

REPT    ¼Æ¾Ç¦¡
        ¡K¡K
        ±Ô­z
        ¡K¡K
ENDM

¼Æ¾Ç¦¡ªº¹Bºâµ²ªG¥²¶·¬O¤@­Ó 16 ¦ì¤¸ªº¥¿¼Æ¡A¥¦¥Nªí¥Ñ REPT ¨ì ENDM ¤§¶¡ªº­«½Æ¦¸¼Æ¡CREPT ¹ê»Ú¤W¦b¨Ï¥Î®É¡A¨C¤@¦¸ªº±¡§Î±`±`¬O©M¤W¤@¦¸¤£¦P¡A¦]¦¹±`±`­n³]¤@­ÓÅܼƨӰO¿ý³o­Ó­È¡A¦Ó³o­ÓÅܼƨ䣷|¦b°O¾ÐÅéùئû¾Ú¤@¶ôªÅ¶¡¡A©Ò¥HºÙ¬°¡yµêÀÀÅܼơz¡A¨Æ¹ê¤W¥¨¶°¤Þ¼Æ¤]¬O¤@ºØµêÀÀÅܼơC¦n§a¡I¦^¹LÀY¨ÓÄ~Äò»¡©ú REPT ªº¨Ï¥Î¤èªk¡A³o­ÓµêÀÀÅܼƪº°_©l­È¥²¶·­n¦b REPT¡KENDM °Ï¶ô¤§«e¥ý³]©w¡AµM«á¦b°Ï¶ô¤¤¥[¥H§ïÅܳo­ÓµêÀÀÅܼƤ§­È¡C

= °²«ü¥O

¥Î¨Ó³]©w³o­ÓµêÀÀÅܼƪº«ü¥O¬O¡y=¡z¡A¥L©M EQU °²«ü¥O¤£¦Pªº¦a¤è¬O¡AEQU ©Ò³]©w¤§ª½¬O±`¼Æ¡AµLªk¦A§ó§ï¡Q¦Ó¥Î¡y=¡z©Ò«ü©wªºÅܼƬOÁÙ¥i¥H¦A§ó§ïªº¡A³o¨â­Ó°²«ü¥O¬Û¦P³B«K¬O¸ÓÅܼƳ£¤£·|¦û¾Ú°O¾ÐÅé¡C= ªº»yªk¬O

ÅܼƦW  =        ¼Æ¾Ç¦¡

©³¤U³o­Ó¥¨¶°¬O¥Î¨Ó¦b°O¾ÐÅ餤©w¸q 41H¡B42H¡B43H¡B44H¡K¡K5AH¡A¤]´N¬O

        db      'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

·íµM¤W­±¬O³Ìª½±µªº¤èªk¡A¦ý¤]¥i¥H¥Î¤U­±ªº¤èªk

x       =       0
        REPT    26
        db      'A'+x
x       =       x+1
        ENDM

¤p¤ì°¸§â¤W­z­«½Æ°Ï¶ô¼g¦¨¤@­Ó§¹¾ãªºµ{¦¡¡G

        include mymac.inc

alpha   MACRO
data    segment para    public  'data'
x       =       0               ;05 ³]©wµêÀÀÅܼư_©l­È
letters EQU     THIS BYTE       ;06 ©w¸q'ABC¡KZ'¦r¦ê¤§¦ì§}
        REPT    26              ;07 ¦@¦³ 26 ­Ó­^¤å¦r¥À
        db      'A'+x           ;08 ©w¸q¨C¤@­Ó­^¤å¦r¥À
x       =       x+1             ;09 ³]©w¤U¤@¦¸¤§µêÀÀÅܼÆ
        ENDM                    ;10 ­«½Æ°Ï¶ôµ²§ô
        db      '$'             ;11 'ABC¡KZ'¦r¦êµ²§ô
data    ends
        ENDM                    ;13 alpha ¥¨¶°µ²§ô

print   MACRO   string
code    segment para    public  'code'
        mov     ah,9
        mov     dx,offset string
        int     21h
code    ends
        ENDM

        initial
        alpha
        print   letters
        exit    0
        end     start

³o­Óµ{¦¡¦b²ÕĶ»P³sµ²«á¡A°õ¦æ®É·|¦b¿Ã¹õ¤W¦L¥X¡yABCDEFGHIJKLMNOPQRSTUVWXYZ¡z26 ­Ó­^¤å¦r¥À¡C

IRP °²«ü¥O

IRP ³QºÙ¬°¤£©w­«½Æ ( indefinite repeat )¡A¥Nªí¨C¦¸­«½ÆªºµêÀÀÅܼƨ䣳W«h¡A¨ä»yªk¬O

IRP     °Ñ¼Æ,<¤Þ¼Æ1, ¤Þ¼Æ2, ¤Þ¼Æ3¡K¡K>
        ¡K¡K¡K
        ±Ô­z
        ¡K¡K¡K
ENDM

·í MASM ¹J¨ì IRP °²«ü¥O®É¡A­º¥ý·|¥H¡y¤Þ¼Æ1¡z¤§­È¥N¤J°Ñ¼ÆùزÕĶ¡A¤@ª½¹J¨ì ENDM ®É¡A¦A¥H¡y¤Þ¼Æ2¡z¥N¤J°Ñ¼Æ²ÕĶ¡K¡K¦p¦¹­«½Æª½¨ì©Ò¦³ªº¤Þ¼Æµ²§ô¬°¤î¡C¦p¦¹¤@¨Ó¡A¨C¤@¦¸²ÕĶ±¡§Î¦]¬°¿é¤J¤Þ¼Æ¥i¥H¥Ñ³]­pªÌ¦Û¥Ñ¨M©w¡A©Ò¥H¥i¥H¬O¨S¦³³W«hªº¡C

IRPC °²«ü¥O

»yªk¬°

IRPC    °Ñ¼Æ¡A¦r¦ê
        ¡K¡K¡K
        ±Ô­z
        ¡K¡K¡K
ENDM

·í MASM ¹J¨£ IRPC °Ï¶ô®É¡A­º¥ý·|¥H¦r¦êªº²Ä¤@­Ó¦r¤¸¥N¤J°Ñ¼Æ¤¤²ÕĶ¨ä«áªº±Ô­z¡Aª½¨ì¹J¨£ ENDM¡AµM«á¦A¥H¦r¦êªº²Ä¤G­Ó¦r¤¸¥N¤J°Ñ¼Æ¤¤²ÕĶ¡K¡K¡Aª½¨ì¦r¦ê¤¤ªº©Ò¦³¦r¤¸³£³B²z¹L¡C¥¦±`±`¥Î¨ÓÀˬd¦r¦êùجO§_§t¦³¬Y¨Ç¦r¤¸¡C¨Ò¦p©³¤Uªºµ{¦¡¤ù¬q¡G

IRPC    letter,ABCDE
        db      '&letter'
        db      '&letter'+20h
ENDM

¨ä¹ê´N¬O

        db      'ABCDE'
        db      'abcde'

¦³Ãö IRP¡BIRPC ÁÙ¦³§ó¦³¥Îªº¨Ò¤l¡A½Ð¬Ý¤U¤@³¹ªº³q¥Î±À¤J°ïÅ|¥¨¶°¡C


µù¤@¡G

PAGE °²«ü¥O

PAGE ¬O¤@­Ó°²«ü¥O¡A¨ä§@¥Î±±¨î¦CªíÀɪºªø«×¥H¤Î¼e«×¡C²Õ¦X»y¨¥ªº¦CªíÀɨC¤@¦æªø«×±`±`¶W¹L 80 ­Ó¦r¥À¡A¦ý¦­´Á¿Ã¹õªº¤å¦r¼Ò¦¡¨C¤@¦æ³Ì¦h¥u¯à¦³ 80 ­Ó­^¤å¦r¥À¡A¦]¦¹¦h¥X¨Óªº±`±`·|¨ì¤U¤@¦æ¡A¤£©öÆ[¹î¡C¦Ó¦Cªí¾÷¥i¥H¹F 132 ­Ó¦r¥À¡A¤ñ¸û¤è«K¡A¦]¦¹¥Î page ¨Ó±±¨î¦CªíÀɪºªø«×¥H¤Î¼e«×¡A»yªk¦p¤U¡G

page    [ªø«×][,¼e«×]

ªø«×¥²¶·¦b 10 ¨ì 255 ¤Q¶i¦ì¾ã¼Æ½d³ò¤º¡A¦pªG¨S³]©wªø«×¡A¤º©w­È¬O 50¡C¼e«×­n¦b 80 ¨ì 132 ¤Q¶i¦ì¾ã¼Æ½d³ò¤º¡A¦pªG¨S³]¼e«×¡A¤º©w­È¬O 80¡C°²¦p¥u³]©w¼e«×¦Ó¤£³]©wªø«×¡A¥²¶·¦b«e­±¥[¤W¡y,¡z§_«h MASM ·|»~¥H¬°¬Oªø«×¡C°²¦pªø«×»P¼e«×³£¬Ù²¤¡A«h·|±j­¢¸õ­¶¡C


¦^¨ì­º­¶¡A ¨ì²Ä¤G¤Q¤­³¹¡A ¨ì²Ä¤G¤Q¤C³¹