The"e #a$ %e onl$ one state#ent &e" line! A state#ent #a$ sta"t in an$ colu#n! A state#ent is eithe" an inst"uction' (hich the asse#%le" t"anslates into #achine co)e' o" an asse#%le" )i"ectie *&seu)o+o&,' (hich inst"ucts the asse#%le" to &e"-o"# so#e s&eci-ic tas.! S$nta/ o- a state#ent0 1na#e2 #ne#onic 1o&e"an)*s,2 13 co##ent2 *a, The cu"l$ %"ac.ets in)icate those ite#s that a"e not &"esent o" a"e o&tional in so#e state#ents! *%, The na#e -iel) is use) -o" inst"uction la%els' &"oce)u"e na#es' seg#ent na#es' #ac"o na#es' na#es o- a"ia%les' an) na#es o- constants! *c, MASM 6!4 acce&ts i)enti-ie" na#es u& to 567 cha"acte"s long! All cha"acte"s a"e signi-icant' (he"eas un)e" MASM 8!4' na#es a"e signi-icant to 94 cha"acte"s onl$! Na#es #a$ consist o- lette"s' )igits' an) the -ollo(ing 6 s&ecial cha"acte"s0 ? . @ _ $ % !:- a &e"io) is use)3 it #ust %e the -i"st cha"acte"! Na#es #a$ not %egin (ith a )igit! *), :nst"uction #ne#onics' )i"ectie #ne#onics' "egiste" na#es' o&e"ato" na#es an) othe" (o")s a"e "ese"e)! S$nta/ o- an inst"uction0 1la%el02 #ne#onic 1o&e"an) 1 ' o&e"an)2 2 13 co##ent2 The cu"l$ %"ac.ets in)icate those ite#s that a"e not &"esent o" a"e o&tional in so#e inst"uctions! Thus an inst"uction #a$ hae ;e"o' one' o" t(o o&e"an)s! O&e"ato"s0 The 8086/8088 Asse#%l$ language has a nu#%e" o- o&e"ato"s! An o&e"ato" acts on an o&e"an) o" o&e"an)s to &"o)uce a alue at asse#%l$ ti#e! E/a#&les a"e0 < ' + ' =' / ' >U?' an) OFFSET @o##ents0 A se#icolon sta"ts a co##ent! A co##ent #a$ -ollo( a state#ent o" it #a$ %e on a se&a"ate line! Multi&le+ line co##ents can %e ("itten %$ using the @OMMENT )i"ectie! The s$nta/ is0 @OMMENT delimiter 1co##ent2 co##ent . . . delimiter 1 co##ent 2 (he"e )eli#ite" is an$ non+%lan. cha"acte" not a&&ea"ing in co##ent! The cu"l$ %"ac.ets in)icate an ite# that is o&tional! e!g!' @OMMENT = This &"og"a# -in)s the #a/i#u# ele#ent in a %$te a""a$ =
Nu#%e"s0 *a, A %ina"$ nu#%e" is su--i/e) %$ % o" B! e!g!' 44040444B *%, A )eci#al nu#%e" is su--i/e) %$ an o&tional ) o" >! e!g!' 65) +55> 9878 *c, A he/a)eci#al nu#%e" #ust %egin (ith a )eci#al )igit an) it is su--i/e) %$ h o" A e!g!' 50A 0%F5Ah 4 @ha"acte"s0 A cha"acte" is enclose) in a &ai" o- single Buotes o" in a &ai" o- )ou%le Buotes! e!g!' C/D EBF St"ings0 A st"ing is enclose) in a &ai" o- single Buotes o" in a &ai" o- )ou%le Buotes! e!g!' CENTEG YOUG NAME0 D ETAE MAX:MUM HALUE :S F CO#a" shoute)' Ehel& IF D Esa$' ChelloD F EO#a"Ds %oo.sF Fo" a st"ing )eli#ite) %$ single Buotes' a &ai" o- consecutie single Buotes stan)s -o" a single Buote! e!g!' CO#a"D Ds %oo.sD >ata )e-inition Each a"ia%le has a )ata t$&e an) is assigne) a #e#o"$ a))"ess %$ the &"og"a#! The )ata+)e-ining )i"ecties a"e0 Directive >esc"i&tion o- :nitiali;e"s BYTE' DB *%$te, Allocates unsigne) nu#%e"s -"o# 0 to 588! SBYTE *signe) %$te, Allocates signe) nu#%e"s -"o# J458 to <457! WORD, DW *(o") K 5 %$tes, Allocates unsigne) nu#%e"s -"o# 0 to 68'898 *66L,! SWORD *signe) (o"), Allocates signe) nu#%e"s -"o# J95'768 to <95'767! DWORD, DD *)ou%le(o") K 6 %$tes,' Allocates unsigne) nu#%e"s -"o# 0 to 6'5M6'M67'5M8 *6 #ega%$tes,! SDWORD *signe) )ou%le(o"), Allocates signe) nu#%e"s -"o# J5'467'689'668 to <5'467'689'667! e!g!' AL?AA >B 6 HAG4 >B N AGGAY4 >B 60A' 98A' 60A' 90A HAG5 >O 9AB6h AGGAY5 >O 800' 686' 700' 600' 600 ?GOM?T >B CENTEG YOUG NAME PD ?O:NTEG4 >> 6BA7000AA A ? in &lace o- an initiali;e" in)icates $ou )o not "eBui"e the asse#%le" to initiali;e the a"ia%le! The asse#%le" allocates the s&ace %ut )oes not ("ite in it! Use ? -o" %u--e" a"eas o" a"ia%les $ou" &"og"a# (ill initiali;e at "un ti#e! intege" BYTE 46 negint SBYTE +46 e/&"ession OOG> 6=9 signe)e/& SOOG> 6=9 e#&t$ QOOG> N 3 Allocate uninitiali;e) long int BYTE 4'5'9'6'8'6 3 :nitiali;e si/ unna#e) %$tes long >OOG> 65M6M675M8
longnu# S>OOG> +5467699668
The >U? o&e"ato" can %e use) to gene"ate #ulti&le %$tes o" (o")s (ith .no(n as (ell as un+initiali;e) alues! e!g!' ta%le )( 400 >U?*0, sta"s )% 80 )u&*C=D, AGGAY9 >B 90 >U?*N, AGGAY6 >B 40 >U?*80,' 68' 55' 50 >U?*60, 5 STG:NGS >B 50A >U?*C>hah"anD,
Note: :- a a"ia%le na#e is #issing in a )ata )e-inition state#ent' #e#o"$ is allocate)3 %ut no na#e is associate) (ith that #e#o"$! Fo" e/a#&le0 >B 80 >U?*N, allocates 80 un+initiali;e) %$tes3 %ut no na#e is associate) (ith those 80 %$tes! :n MASM 6!4 an) o%oe' a co##a at the en) o- a )ata )e-inition line *e/ce&t in the co##ent -iel), i#&lies that the line continues! Fo" e/a#&le' the -ollo(ing co)e is legal in MASM 6!40 longst"ing BYTE RThis st"ing R' Rcontinues oe" t(o lines!R %it#as.s BYTE 80h' 60h' 50h' 40h' 08h' 06h' 05h' 04h Na#e) constants0 The EQU *eBuate, )i"ectie' (hose s$nta/ is0 na#e EQU constantSe/&"ession assigns a na#e to a constant e/&"ession! E/a#&le0 MAX EQU 95767 M:N EQU MAX + 40 LF EQU 0AA ?GOM?T EQU CTY?E YOUG NAME0 PD Note0 *i, No #e#o"$ is allocate) -o" EQU na#es *ii, A na#e )e-ine) %$ EQU #a$ not %e "e)e-ine) late" in a &"og"a#! The LABEL )i"ectie' (hose s$nta/ is0 na#e LABEL t$&e (he"e t$&e *-o" MASM He"sion 8!4 an) lo(e" e"sions, is BYTE' OOG>' >OOG>' QOOG>' TBYTE' NEAG' o" FAG &"oi)es a (a$ to )e-ine o" "e)e-ine the t$&e associate) (ith a a"ia%le o" a la%el! E/a#&le40 AGGAY4 LABEL OOG> AGGAY5 >B 400 >U?*0, Ae"e AGGAY4 )e-ines a 80+(o") a""a$' an) AGGAY5 )e-ines a 400+%$te a""a$! The sa#e #e#o"$ locations a"e assigne) to %oth a""a$s! Thus the a""a$ can %e accesse) as eithe" the %$te a""a$ AGGAY4 o" the (o") a""a$ AGGAY5! E/a#&le50 HAG9 LABEL >OOG> OOG>4 LABEL OOG> BYTE4 >B N BYTE5 >B N OOG>5 LABEL OOG> BYTE9 >B 80A BYTE6 >B 66A in this e/a#&le' each o- the (o")s' an) each o- the %$tes o- the )ou%le (o") a"ia%le HAG9 can %e accesse) in)ii)uall$!
SEGMENT >EF:N:T:ON An 8086/8088 asse#%l$ language &"og"a# -ile #ust hae the e/tension .asm The"e a"e t(o t$&es o- 8086/8088 asse#%l$ language &"og"a#s0 e/e+-o"#at an) co#+-o"#at! An e/e+-o"#at &"og"a# gene"ates e/ecuta%le -iles (ith e/tension .ee! A co#+-o"#at &"og"a# gene"ates e/ecuta%le -iles (ith e/tension .com ! An e/e+-o"#at &"og"a# #ust contain a co)e seg#ent an) a stac. seg#ent! :t #a$ contain a )ata seg#ent o" an e/t"a seg#ent! A co#+-o"#at &"og"a# contains onl$ the co)e seg#ent *the stac. seg#ent is e/&licit,! A &"og"a##e" chooses an a&&"o&"iate si;e -o" the stac. seg#ent' )e&en)ing on the si;e o- his &"og"a#! Halues in the "ange 400A to 600A a"e su--icient -o" #ost s#all &"og"a#s! 9 Note: :n a &"og"a#' the )ata' co)e' an) stac. seg#ents #a$ a&&ea" in an$ o")e"! Ao(ee"' to aoi) -o"(a") "e-e"ences it is %ette" to &ut the )ata seg#ent %e-o"e the co)e seg#ent! S!"#$!%!ED SE&"ENT D!RE'T!(ES MASM e"sion 8!0 an) a%oe' an) TASM &"oi)e a si#&li-ie) set o- )i"ecties -o" )ecla"ing seg#ents calle) si#&li-ie) seg#ent )i"ecties! To use these )i"ecties' $ou #ust initiali;e a #e#o"$ #o)el' using the .MO>EL )i"ectie' %e-o"e )ecla"ing an$ seg#ent! The -o"#at o- the .MO>EL )i"ectie is0 .MO>EL #e#o"$+#o)el The #e#o"$+#o)el #a$ %e T:NY' SMALL' ME>:UM' @OM?A@T' LAGGE' AUGE o" FLAT 0 #e#o"$+#o)el )esc"i&tion T:NY One seg#ent! Thus co)e an) )ata togethe" #a$ not %e g"eate" than 66L SMALL One co)e+seg#ent! One )ata+seg#ent! Thus neithe" co)e no" )ata #a$ %e g"eate" than 66L ME>:UM Mo"e than one co)e+seg#ent! One )ata+seg#ent! Thus co)e #a$ %e g"eate" than 66L @OM?A@T One co)e+seg#ent! Mo"e than one )ata+seg#ent! Thus )ata #a$ %e g"eate" than 66L LAGGE Mo"e than one co)e+seg#ent! Mo"e than one )ata+seg#ent! No a""a$ la"ge" than 66L! Thus %oth co)e an) )ata #a$ %e g"eate" than 66L AUGE Mo"e than one co)e+seg#ent! Mo"e than one )ata+seg#ent! A""a$s #a$ %e la"ge" than 66L! Thus %oth co)e an) )ata #a$ %e g"eate" than 66L FLAT One seg#ent u& to 6GB! All )ata an) co)e *inclu)ing s$ste# "esou"ces, a"e in a single 95+%it seg#ent! All o- the &"og"a# #o)els e/ce&t T:NY "esult in the c"eation o- e/e+-o"#at &"og"a#s! The T:NY #o)el c"eates co#+-o"#at &"og"a#s! "emor) "odel O*erati+, S)stem >ata an) @o)e @o#%ine) Tin$ MS+>OS Yes S#all MS+>OS' Oin)o(s No Me)iu# MS+>OS' Oin)o(s No @o#&act MS+>OS' Oin)o(s No La"ge MS+>OS' Oin)o(s No Auge MS+>OS' Oin)o(s No Flat Oin)o(s NT Yes The si#&li-ie) seg#ent )i"ecties a"e0 .@O>E ' .>ATA ' .STA@L ! The .@O>E )i"ectie #a$ %e -ollo(e) %$ the na#e o- the co)e seg#ent! The .STA@L )i"ectie #a$ %e -ollo(e) %$ the si;e o- the stac. seg#ent' %$ )e-ault the si;e is 4L i!e!' 4'056 %$tes! The )e-inition o- a seg#ent e/ten)s -"o# a si#&li-ie) seg#ent )i"ectie u& to anothe" si#&li-ie) seg#ent )i"ectie o" u& to the EN> )i"ectie i- the )e-ine) seg#ent is the last one! TAE GENEGAL STGU@TUGE OF AN EXE+FOGMAT ?GOGGAM The #e#o"$ #a& o- a t$&ical e/e+-o"#at &"og"a#' (ith seg#ents )e-ine) in the o")e" co)e' )ata' an) stac. is0 S? Stac. seg#ent SS >ata seg#ent 6 @o)e seg#ent @S ' :? ?S? *400A %$tes, >S ' ES The @S an) :? "egiste"s a"e auto#aticall$ initiali;e) to &oint to the %eginning o- the co)e seg#ent! The SS "egiste" is initiali;e) to &oint to the %eginning o- the stac. seg#ent! The S? "egiste" is initiali;e) to &oint one %$te %e$on) the stac. seg#ent! The >S an) ES "egiste"s a"e initiali;e) to &oint to the %eginning o- the ?S? *?"og"a# Seg#ent ?"e-i/, seg#ent! This is a 400A *i!e!' 586, %$te seg#ent that >OS auto#aticall$ &"e-aces to a &"og"a# (hen that &"og"a# is loa)e) in #e#o"$! The ?S? contains i#&o"tant in-o"#ation a%out the &"og"a#! Thus i- a &"og"a# contains a )ata seg#ent' the >S "egiste" #ust %e initiali;e) %$ the &"og"a##e" to &oint to the %eginning o- that )ata seg#ent! Si#ila"l$ i- a &"og"a# contains an e/t"a seg#ent' the ES "egiste" #ust %e initiali;e) %$ the &"og"a##e" to &oint to the %eginning o- that e/t"a seg#ent! 8 :nitiali;ation o- >S Note: The inst"uctions (hich initiali;e the >S "egiste" -o" an e/e+-o"#at &"og"a# (ith si#&li-ie) seg#ent )i"ecties a"e0 MOH AX ' T>ATA MOH >S ' AX (he"e AX #a$ %e "e&lace) %$ an$ othe" 46+%it gene"al &u"&ose "egiste"! At loa) ti#e' T>ATA is "e&lace) (ith the 46+%it %ase a))"ess o- the )ata seg#ent! Thus T>ATA ealuates to a constant alue3 such an o&e"an) is usuall$ calle) an i##e)iate o&e"an)! Since MOH inst"uctions o- the -o"#0 MOH Seg#entGegiste" ' :##e)iateO&e"an) a"e inali)' an initiali;ation o- the -o"#0 MOH >S ' T>ATA is inali)! Such an initiali;ation is )one in)i"ectl$ using an$ 46+%it gene"al+&u"&ose "egiste"! E/a#&le0 MOH AX ' T>ATA MOH >S ' AX Note: Ee"$ 8086 asse#%l$ language &"og"a# #ust en) (ith the EN> )i"ectie! This )i"ectie #a$ %e -ollo(e) %$ an ent"$ la%el' (hich in-o"#s the asse#%le" the &oint (he"e &"og"a# e/ecution is to %egin! The ent"$ la%el can hae an$ ali) na#e! The gene"al st"uctu"e o- an e/e+-o"#at &"og"a# is0 .MO>EL SMALL !STA@L 500 .>ATA 3 )ata )e-initions using >B' >O' >>' etc! co#e he"e .@O>E STAGT0 MOH AX ' T>ATA 3 :nitiali;e >S MOH >S ' AX 3
! ! ! 3 Getu"n to >OS MOH AX ' 6@00A :NT 54A EN> STAGT E/a#&le0 .MO>EL SMALL !STA@L 500 .>ATA MESSAGE >B C:@S 595D ' CPD .@O>E STAGT0 MOH AX ' T>ATA 3 :nitiali;e >S MOH >S ' AX 3 3 >is&la$ the st"ing MOH AA ' 0MA MOH >X ' OFFSET MESSAGE :NT 54A 3 Getu"n to >OS MOH AX ' 6@00A :NT 54A EN> STAGT 6 TAE GENEGAL STGU@TUGE OF A @OM+FOGMAT ?GOGGAM The #e#o"$ #a& o- a t$&ical co#+-o"#at &"og"a# is0 S? Stac. a"ea @o)e seg#ent *co)e an) )ata, :? ?S? *400A %$tes, @S' >S ' ES' SS To (o". out the locations co""es&on)ing to s$#%ols *la%els an) a"ia%les, in the sou"ce &"og"a#' the asse#%le" uses a a"ia%le calle) the location counte"! Be-o"e asse#%l$ o- each seg#ent %egins the location counte" is set to ;e"o! As each state#ent in that seg#ent is scanne)' the location counte" is inc"e#ente) %$ the nu#%e" o- %$tes "eBui"e) %$ that state#ent! Since the @S "egiste" is initiali;e) to &oint to the %eginning o- the ?S? (hen a co#+-o"#at &"og"a# is loa)e) in #e#o"$' the location counte" #ust %e set to 400A instea) o- the usual ;e"o' so that0 *i, the asse#%le" assigns o--set a))"esses "elatie to the %eginning o- the co)e seg#ent an) not the ?S?' an) *ii, the :? "egiste" is set to 400A (hen the &"og"a# is loa)e)! The location counte" is set to 400A %$ the )i"ectie0 OGG 400A Aence this )i"ectie #ust a&&ea" at the %eginning o- ee"$ co#+-o"#at &"og"a# %e-o"e the &"og"a# ent"$ &oint! Since a co#+-o"#at &"og"a# contains onl$ one e/&licit seg#ent i!e!' the co)e seg#ent' )ata' i- an$' #ust %e )e-ine) (ithin the co)e seg#ent an$(he"e a )ata )e-inition state#ent (ill not %e t"eate) as an e/ecuta%le state#ent! This can %e )one at the %eginning o- the co)e seg#ent %$ Uu#&ing ac"oss )ata )e-initions using a VM? inst"uction! The gene"al st"uctu"e o- a co#+-o"#at &"og"a# is0 !MO>EL T:NY !@O>E OGG 400A ENTGY0 VM? L4 3 )ata )e-initions using >B' >O' >>' etc! co#e he"e . . . L40 . . . 3 Getu"n to >OS MOH AX ' 6@00A :NT 54A EN> ENTGY E/a#&le0 .MO>EL T:NY .@O>E OGG 400A ENTGY0 VM? STAGT MESSAGE >B C:@S 595D ' CPD STAGT0 3 >is&la$ the st"ing MOH AA ' 0MA 7 MOH >X ' OFFSET MESSAGE :NT 54A 3 Getu"n to >OS MOH AX ' 6@00A :NT 54A EN> ENTGY 8 Othe" >i"ecties .STARTUP Gene"ates &"og"a# sta"t+u& co)e! The .E-!T )i"ectie acce&ts a 4+%$te e/it co)e as its o&tional a"gu#ent0 .EXIT 1 ; Return exit code 1 .E-!T gene"ates the -ollo(ing co)e that "etu"ns cont"ol to MS+>OS' thus te"#inating the &"og"a#! The "etu"n alue' (hich can %e a constant' #e#o"$ "e-e"ence' o" 4+%$te "egiste"' goes into AL0 mov al, value mov ah, 04Ch int 21h :- $ou" &"og"a# )oes not s&eci-$ a "etu"n alue' .E-!T "etu"ns (hatee" alue ha&&ens to %e in AL! .586 Enables assembly of nonprivileged instructions for the Pentium processor. .686 Enables assembly of nonprivileged instructions for the Pentium Pro processor. The USE16, USE32, and FLAT Options When working with an 80386 or later processor, MASM generates different code for 16 versus 32 it seg!ents" When writing code to e#ecute in real !ode under $%S, &ou !ust alwa&s use 16 it seg!ents" 'hirt&(two it seg!ents are onl& applicale to progra!s running in protected !ode" )nfortunatel&, MASM often defaults to 32 it !ode whenever &ou select an 80386 or later processor using a directive like .386, .486, .586 or .686 in &our progra!" *f &ou want to use 32 it instructions, &ou will have to e#plicitl& tell MASM to use 16 it seg!ents" 'he use16, use32, and flat operands to the segment directive let &ou specif& the seg!ent si+e" ,or !ost $%S progra!s, &ou will alwa&s want to use the use16 operand" 'his tells MASM that the seg!ent is a 16 it seg!ent and it asse!les the code accordingl&" *f &ou use one of the directives to activate the 80386 or later instruction sets, &ou should put use16 in all &our code seg!ents or MASM will generate ad code" *f &ou want to force use16 as the default in a progra! that allows 80386 or later instructions, there is one wa& to acco!plish this" -lace the following directive in &our progra! efore an& seg!ents. option segment:use16 Example: .586 option segment:use16 M