Read without ads and support Scribd by becoming a Scribd Premium Reader.
 
;e
was asse
.
..
.
he
first
computer Ianguag
:m-
bIy
language, which
subsr~tvres
sym-
bols that almost look
Iike
EngIish words
for
the
1's
and
zeros
of
the
machine's"native" Ianguage. When you write inassembly language, you regularly
by-
pass
the applicarion
software
and com-municate directly with
the
CPU,
BIOS,
and
MS-DOS.
By using assembly lan-
I
guage, yo11
bogtn
to understand what
is
rcally happening inside
3eginner's
hecompu~er.Assembly language has other benefits, which are well known toprofessional programmers.
It
is fast-up
to
100
rimes faster
than
Basic.
It
uses
memory more eHicienrly than high-revel languages,
Course
in
and it is
usually
the
best
language
for controlling
the
compute<s
110
devices, especially the vidco screen.
Thc
main drawback-an often prohibitively
long
programmingrime-is rnanageabte
if
you
use
assemblylanguage sparingly. In
fact,
rhe
best soft-ware combines high-level
code
with assem-
bly
language
subroutines.Normally, to doanyserious assemblylanguage programming you
have
to buy
a
commercial
assembler,
which translates
L
commands into
the
binary
code
understcad
by
the computcr. Conveniently,
the
De-
bug.COM program that
comes
with
MS-
DOS
provides
a
window into machine-leveland
assembly
language programming.
In
this tutorial, FI1 show you how to
go
intoDebug
and
see how
the
compuFer stores
data
in numerical form:
Then
i'll offer someshort assembly languageprograms that
il-
lustrate conceptsthat
apply
to full-blown
by
Hardin
Brothers
assemblers.
Debug
Starts Here
To
begin, put your
MSDOS
isk
in
drive
A
and
type
DEBUG.
In
a
second or two,
you
should see
a
hyphen on
the
next
line.
The
hyphen
is
Debug's
prompt, and it means
that
Debug
is waitingfor your command. Debug expects you to
press
the
enter
key
after
each
command,
which you can
type
in
either upper- orlowercase.
-
.*
Type
in
the
letter
rand
press
enter.
You will
see
a
thrwtinedisplay.
The
chart
Breakdown of Initial Debug Display explainsthe significance of the entries.
Some
of the
numbers
and Ietters
might
be different, but the organization of the display will
be
essentially the same.
(In
all
of
the figures accompanying this article,the only characters you type
in
are those
next
to
the
Debug
promptand
memory
addresses [such as
16M:0138].
Everything
else
iscommentary
or
a
representation
of
what
you
should see on screen.)
By
typing
in
an
r,
you have
asked
Debug
w
show
you
the
current conrents
of
the
CPU's
registers. Registers
are
memory
locations
in the
CPU
instead of in
the
computefs
RAM).
You
can
use some registers for almost any purpose; others are limited
to
specific uses.
(IF
you
don't understand
bow
memory addresseswork, see the
sidebar
"The
Structure
of
Memory.")
The
CPU
n
MSDOS
computers
has
14
internat registers, each
of
which
can
hoId
16
bits. or
one
word.
The first four-AX
BX,
CX,
and DX-are called general-purpose registers; their
use
is
largely up ta
you.
Each
of
these
four registers
can
also serve
as
two 8-bit (the equivalent of two
I-byte)
registers. The top byte
WHAT
YOU
NEED:
IWS-DOS
and
hti.
PC
Resource
November
1987
 
.'c"
.'
'1'
,"
",../~-
~':-J
;;
Youdon'tneedtobeaprotowriteprogramsinassemblylanguage.
UsingtheMS-DOSDebugprogramasyourstartingpoint,
youcanmasterthebasicsinrecordtime.
..
ofAXiscalledAH;thelowbyteiscalledAL.Youdeterminewhentousetheseareasas16-bitregistersandwhentousethemas8-bitregisters.[ntheinitialDebugdisplay,thecontentsofeachgeneral-purposeregisteris0000.Thesamevalueisstoredinthreepointerregisters:BP,S[,andDr.Thebasepointer(BP)registergenerallyservesasaplacemarktohelpyoumanipulateacomplexdatastructurecalledastackframe.Thesourceindex(81)anddestinationindex(01)registersaremostlyformovinglargeblocksofmemory,whicharecalledstringsinassemblyparlanceregardlessofwhethertheycontaintextualdata.ThefinalregisterinthefirstrowoftheDebugdisplayisthestackpointer
(SP),anin-memorydatastructurethatservesmanypurposes.Whenever
a
programbranchestoasubroutine,itstoresthereturnaddressonthestack.Programmersalsousethestacktotemporarilystorethecontents
ofregisters
andsometimesto
passvalues
fromoneroutinetoanother.Thefirstfourregistersinthesecondrowofthedisplay
arethe
segmentregisters.Becauseofthewaymemoryisorganizedinan
MS-DOScomputer,tworegisters
arerequired
todesignatemem-
)rylocations.Thesegmentregisterspointtoalargechunk
of
)C
Resource
November
1987memory;thespecificaddresswithinthatchunkiskept
inoneoftheother
registers.
Thefifth
registerinthesecondrowistheinstructionpointer(IP).Italwayscontainstheaddress
ofthenext
instructiontobeexecuted,inthesamewaythatBasicalwayskeepstrackofwhichlinenumbershouldbeinterpretedandexecutednext.
Thecontentsofall13numeric
registersare
displayedinhexa-decimal(hex)format.Assemblylanguageprogrammers
rarelyusedecimal
numbers.Instead,theyuseeitherthebinary(base2)orhex(base16)numbersystem.
Thelastpart
ofthesecond
lineintheinitialDebugdisplay
containseighttwo-letterabbreviationsshowingthestate
ofthe
~
importantbitsintheflagregister(seethetableStatusFlags).The
~
CPUhasaspecial16-bitregistertokeeptrack
ofthese
statUs
't;:
flags.
Theyareupdatedtogiveinformationabouttheresultsof
~
manyassemblylanguageoperations
andcanbe
testedtodetermine
~whetheraprogramshould
branch
toanewsetof
instructions.1';
Theflags
arethe
basis
ofall
conditionaltestsinassemblylanguage.'§
Forexample,
thezero
flagmightchangefrom
NZtoZRtoshow
~
thattheresultofamathematicaloperationiszero.Notallflag-
:::!
register
bits
are
used..51
 
so obvious is where che information oriei-
Flags
<
nates and where the result is stored. Avoid
the
cemptacion
to
read the Iineas,
"Add
AX
and
BX."
Instead, read it as, "Add thevalue in the
BX
register
to
that in
the
AXregister."
Whcn you think of it that
way,
the expected result shouPd
be
dear:
TIlc
value En
BX
will
be
added to the value in
AX,
and the result win be left in
the
AX
register. This line
is
analogous
to
the
Basic
statement:
All
flags
4
All
flags
I
W
=
overf
3W
=
decrt
<I
=
enable
.-
Overflow
Direction
Enterruptt
m.
NV=noo
UP
=
ncre
DI
=
disabl
PL
=
plus
low
ment
I
led
I
31911:
Zero:
AuxiIiary
VCr
=
negal
!R
=
zero
iC
=
auxili::arry
carry:
One advantage of programming in Debugis that
you
can
watch the program executestep by
step.
Type
in
the letter
r
(for
tllc
Trace command) three times and watchthe
AX
and
BX
registers.
The
Trace
com-mand tells Debug to execute an instructionand display the registers again. Ft
lets
youwatch each register being loaded withthe appropriate value and
the
Final resultbeing placed in the
AX
regisrer. Yourscreen should resemble the one shown inFigure
2.
Parity:
Carry:
~arity
I
arry
t
'E
=
parity
2Y
=
carry
Video
Output
So
fat,
you've done
a
lot of looking but
no
programming. Jkfore writing
a
shortprogram, you need to
know
one more
De-
bug command. If you type in an
r,
Debugshows
you
the initial register display. But
if
you type in
an
r
plus the
name
of
a
register, Debug displays the contents of theregister
and
Iers
you enter a
new
value.For example, if you type:which tells Debug to "unassembIe0'
7
ytesstarting
at
address
100
hex. (The letter
I
stands for Icngth.) Your screen should looklike the one
shown
at
rhe
bottom of Figure
1.
Type
in an
r
co
produce the registerdisplay. It should
took
rhe
same as youroriginal register display, except
that
the
lastline shows the first instruction of your pro-gram.
Saving
and
Running
a
Program
Although
the
first program doesn't
ac-
complish
much,
it
helps
you
learn the ru-diments
of
Debugs rcgistcr display,
assem-
hting
and
unassernbling
a
program,
and
A
Basic
Vocabulary
Every
line in
a
Debug sssembly languageprogram has two parts. The first part ofDebug displays
AX
0000
on one line anda colon on the next.
If
you enter
11
11,
Debug
will change the contents
of
the
AX
register
to
1
11
hex.
Remember
that
num-
bers
typed
into and displayed
by
Debugthe line always contains
a
two-, three-, orfour-letter
command.
These
ommands arecalled mnemonics, or memory words,
be-
cause
they
represent exactly
one
CPU
in-struction and are easier to remember thanrracing
a
program
to
watch it execute.
Bc-
fore you
can
use Debug to create
a
program,you must be able to save
the
program todisk,
so
chat
you
can run it from the
MS
DOS
prompt.
The
program
in
Figure
3,
while simple,introduces several
new
concepts.
&fore
starting, you should clear the
lasc
programfrom Debug's memory. Type
in
the letter
g
to quit Debug, then
at
the
MS-DOS
prompt type
DEBUG.
Shuffling
data
around
in registers is austful skill that produces
no
output
to
watch
or evaluate.
The
Figure
3
listing, however,prints a message on screen-in
this
case,
the
phrase, "Hello world!"
The
programadds
two
new
skills to your repertoire: sav-are
in
hex format.
Now
type in the Ietter
o
ro
invoke theAssemble command. Debug answers
by
displaying a four-digit number,
a
colon,and 0100.
The
cursor
appears
without
a
prompt and waits for you to enter programstatements.
The
numbers displayed are the segmentand offset of the current location
in
mtm-
groups
of
binary digits.
They
are sometimescalled op-codes because they represent
CPU
operations.After
the
on-codc are one or two ooer-ands (or nond).
The
number
depend;
on
the
particular command
and
the types
of
information on
which
it operates. You mayconclude the line with a semicolon foIlowedby
a
remark.
The
irst op-code in
the
program
is
MOV,
a
mnemonic for the Move command. It isone of
the
most common mnemonics inany assembly language program. You use it
ory.
The segment addresses you
see
willprobably differ from those
in
the figures;
they
are determined by
the
MS-DOS
ver-sion
you
use and whether you
bsve
any
memory-resident utilities,
RAM
disks, or
print
spoolers.
The
number after
the
colon,which is calIed the offset, should
be
0100.
All
.COM
programs begin at address
100
hex
of their memory segment,
and
Debug
can create only
.COM
rograms.
If
theoffset
you
see
is not
0100,
press enter
and
to
move data into registers, into memory,
inga
program on disk and communicatingwith
MS-DOS.
The
CPU
knows
nothing about videoscreens,
disk
drives, keyboards, modems,or printers.
To
perform
110
functions,
a
program
must
either manipulate the
com-
puter's
hardware
directly, which is
a
com-plicated and difficult
task,
or
seek
help from
MS-DOS
r
the
BIOS.
To
write programs of your own, you need
a
list of
MS-DOS
routines
and an
expIa-
nation
of
how to use them.
You
can
findthis information
in
the
programmer's
rcf-
erence manual for any MS-DOS computer.
The
MSDOS
routines are
the
same re-gardless of which computer you own; theBIOS roucines are essentially
the
same
for
all IBM
PClXTlAT
compatibles.The first line of the program
places
th,
from
register to register, and berween reg-
isters
and
memory
Locations.
Its name
is
technicnlly incorrect, since it only
mpm
information from one
place
to another. Like
Basic's
Ler
statement, it leaves the value inthe source operand intact.The
MOV
operator is always followedtype:
by
two
operands: the destination and thesource.
In
the
Figure
1
program, (he firstIine tells the
CPU,
"Move the value
1
intothe
AX
register."
in
a similar manner, thesecond line loads
the
value
2
into the
BX
rgister.
The
two lines are analogous to the
to
start
assembly at location 0100 hex.
The
first
progam is exceedingly simple.Type in the three instruction lines in Figure
1.
Use the
tab
key to space betwen coI-
umns
and press enter at the end of eachline. Press the enter
key
once more, andyou should
bc
back
at Debug's hyphen
prompt.
To
check
your work, enter:
Basic
statement:
It
might be obvious to you that
the
thirdline is an addition instruction; what is
not
PC
Resource
November 19:
Search History:
Searching...
Result 00 of 00
00 results for result for
  • p.
  • Notes
    Load more