Sei sulla pagina 1di 69

Assembly Language Primer for the Absolute Beginner

Doug Dingus (potatohead)


opengeek@gmail.com
02/09/08 Version 07
1
Table of Contents
Introduction................................................................................................4
Core Concepts..............................................................................................7
Number Representations..............................................................................7
Computer number sizes...............................................................................9
Computer Concepts...................................................................................10
Computer Math...........................................................................................14
Addition (ADD........................................................................................14
Add !ith carr".........................................................................................1#
De$imiters..............................................................................................17
Modu$o..................................................................................................1%
&ubtraction.............................................................................................19
Mu$tip$ication ' Di(sion..............................................................................)0
Decima$ to *inar" con(ersion.......................................................................)1
Ne+ati(e or si+ned numbers.........................................................................)1
,-amp$e math operations............................................................................).
Addin+ t!o unsi+ned .) bit numbers/..........................................................).
&et 0$a+ states (1ero and Carr" 0$a+s...........................................................).
Addin+ !ith carr" to represent $ar+er numbers o0 di00erent size2 unsi+ned..............)4
Add #4 bit number to #4 bit number2 unsi+ned...............................................)4
3rope$$er &peci0ic Core Concepts......................................................................)4
Instructions............................................................................................)7
5itt$e ,ndian...........................................................................................)%
&o0t!are &etup............................................................................................)
6,AR (7he 3rope$$er Debu++er 8 ,mu$ator........................................................)
*ui$d "our &etup.........................................................................................
9eri0" "our &etup.......................................................................................4
Dissectin+ some Assemb$" 5an+ua+e Code............................................................7
And no!2 the code/ (0ina$$":........................................................................%
6ettin+ the code into the 3rope$$er 7oo$...........................................................%
6ear &imu$ation........................................................................................9
3ro+ram &ections......................................................................................40
(un$abe$ed 3re;pro+ram comment area.......................................................41
C<N (Constants...................................................................................4)
2
3=* (3ub$ic........................................................................................4)
DA7 (Data...........................................................................................44
Assemb$" 3ro+ram Detai$.............................................................................47
3ro+ram.............................................................................................47
Data..................................................................................................47
Instruction >0? mo( >destination C<6 address?2 >source C<6 address?.....................4%
Instruction >1? mo( >destination C<6 address?2 >source C<6 address?.......................49
Instruction >)? add > destination operand C<6 address?2 >@immediate source (a$ue
operand to be added?................................................................................49
Immediate addressin+.............................................................................40
Instruction >.? !aitcnt >co+ memor" $ocation containin+ counter (a$ue to !atch 0or?
>co+ memor" $ocation containin+ de$a" to be added in preparation 0or ne-t !atch?......40
Instruction >4? -or >destination C<6 memor" address? >source C<6 memor" address?. 41
Instruction >4? Amp >@immediate C<6 memor" $ocation to Aump to?.......................4)
3ro+ram Discussion.......................................................................................4)
*it 5o+ic and Manipu$ation <perators.................................................................44
7ruth 7ab$es............................................................................................44
<R (*it!ise <R.......................................................................................44
Bhat about the Carr"C (3arit"..................................................................4#
*it MasDin+..........................................................................................47
<ther 7ruth 7ab$es....................................................................................47
E<R (,-c$usi(e <r................................................................................4%
AND (*it!ise AND.................................................................................49
ANDN (*it!ise AND o0 one number2 !ith N<7 o0 another..................................49
N<7 (*it!ise N<7................................................................................#0
3
Introduction
Be$come to the binar" !or$d: Computers2 despite appearin+ 0ar more comp$e-2 rea$$" do
on$" t!o thin+s/
;add numbers together, or perform logic operations on them (operate)
2and
;copy them around.
Additiona$$"2 the on$" numbers computers rea$$" understand are 1 and 0. 7hese are Dno!n
as on and o002 set and reset2 hi+h and $o!2 etc... &omethin+ is either happenin+2 or it isnFt.
7hatFs it: (Be$$ most$" it. 7here are the bit $o+ic operators/ AND2 <R2 N<72 E<R2 etc... !e
!i$$ +et into those $ater on.
7he rea$it" is most computers do these thin+s (er" 0ast and in combination. 7he computer
"ou are usin+ ri+ht no! is more than Aust coo$ hard!are. ItFs contains $ots o0 code Gspeci0ic
combinations o0 ones and zeros2 chained to+ether to per0orm the man" tasDs !e norma$$"
taDe 0or +ranted. 7his code2 is the sum o0 man" man "ears o0 thou+ht app$ied to !hat
appear to be the simp$est o0 prob$ems. Most o0 these prob$ems are simp$e actua$$". It is
the combinations that are hard.
Assemb$" $an+ua+e is the art o0 !orDin+ ri+ht at the computers $e(e$. ItFs Aust one step up
0rom enterin+ in ones and zeros to +et stu00 done. =$timate$"2 e(er"thin+ "our computer
does2 boi$s do!n to assemb$" $an+ua+e instructions. ItFs computin+ in the ra!. ItFs a$so
!here a$$ the rea$ 0un is at2 0or those in the Dno!.
IFm !ritin+ this because I !ant to share some o0 the 0un that comes !ith $earnin+ computin+
at this $e(e$. IFm a$so !ritin+ it because itFs not a$$ that eas" to 0ind an"more.
Computin+ toda" happens at the hi+her $e(e$s. Be ha(e $an+ua+es2 pro+rams2 means and
methods o0 doin+ thin+s2 that trans$ate do!n to assemb$" $an+ua+e and this is +ood. *ut2
!here does it a$$ come 0romC Ho! does one Aust bui$d a computer 0rom the +round upC
Bh" are some computers s$o!2 some 0astC Bhat maDes the +raphics appear on the screen2
or that sound come out o0 the speaDerC
7o someone2 !ho has not "et $ooDed at computin+ at this assemb$" $e(e$2 it a$$ seems to be
a bit o0 (oodoo. Iou do stu00 and other stu00 happens.
Readin+ this +uide !i$$ chan+e a$$ o0 that. Idea$$"2 "ou !i$$ come to appreciate !hat
computers do in a !a" thatFs Aust not so common an"more. Assemb$" is becomin+
somethin+ o0 a b$acD art2 0or+otten2 i+nored in $ieu o0 hi+her $e(e$ thin+s that appear to
!orD Aust 0ine.
4
7his +uide is aimed at the 3ara$$a- 3rope$$er Micro contro$$er. *e0ore "ou asD2 IF$$ Aust +et
J!hat is a micro contro$$erCK out o0 the !a" ri+ht no!. 7he 3rope$$er is a $itt$e computer
on a sin+$e chip. 7here are other booDs2 some !ith that e-act tit$e2 that ans!er the
Luestion better than I !i$$. Mor the purposes o0 this +uide2 sin+$e chip computer !i$$ ser(e
Aust 0ine.
&in+$e chip computers ha(e a number o0 ad(anta+es o(er their bi++er and 0ar more comp$e-
persona$ computers. Bhere assemb$" $an+ua+e pro+rammin+ is concerned2 the primar"
ad(anta+e is there bein+ abso$ute$" nothin+ bet!een "ou and the computer. 7his means itFs
+oin+ to do e-act$" !hat "ou te$$ it to2 e(er" time "ou asD it to.
More comp$e- computers are 0o$$o!in+ instructions that come 0rom man" sources. 7he
<peratin+ &"stem is a set o0 instructions2 as is the s"stem 0irm!are2 the pro+rams "ou run
and use2 a$on+ !ith the dri(er so0t!are that connects the (arious de(ices to the <peratin+
&"stem to 0orm a !ho$e.
*" the time "ou2 the user2 +et to doin+ an"thin+2 the computer has a$read" been (er" bus":
&ometimes these instructions 0rom others are con0$ictin+2 incomp$ete2 or ma"be Aust donFt
do e-act$" !hat "ou are $ooDin+ 0or. Man" peop$e !i$$ $ooD 0or ne! instructions (pro+rams2
or ma"be start to $earn to pro+ram in an" number o0 Jpro+rammin+ $an+ua+esK2 !hich
rea$$" are more instructions that he$p "ou !rite "our o!n instructions.
Assemb$" is nothin+ $iDe an" o0 that. In assemb$" $an+ua+e2 itFs "ou and the C3=. Actua$$"2
on the 3rope$$er2 itFs "ou and % C3=Fs2 a$so Dno!n as C<6Fs. 3rope$$er speci0ic termino$o+"
!i$$ be introduced throu+hout this +uide2 as needed.
7here is another imp$ication here as !e$$. In assemb$" $an+ua+e2 the computer does
e-act$" !hat "ou te$$ it to. 7his means2 i0 "our pro+ram does not !orD2 itFs "our 0au$t:
7he prope$$er2 $iDe most an" computer2 comes !ith some instructions that he$p to maDe
pro+rammin+ easier. 7his is the &3IN $an+ua+e "ou ma" a$read" be !orDin+ !ith. &oon the
3rope$$er !i$$ +et a rea$ C comp$ier2 !hich taDes hi+her $e(e$ pro+rams and auto;+enerates
assemb$" instructions 0or "ou.
I0 "ou are readin+ this2 "ou are $ooDin+ to +et the most 0rom "our 3rope$$er. 7hat means
"ou need to be ab$e to !orD at the assemb$" $e(e$ 0or one and on$" one reason/
you can't afford to hae anything get bet!een you and the Propeller"
Bith computers2 maDin+ thin+s easier o0ten taDes time. &peci0ica$$"2 computer time G
c"c$es2 potentia$ to +et !orD done2 etc...
3ro+rammin+ in assemb$" $an+ua+e is as 0ast as it +ets. AsD the computer to do somethin+2
and it does it ri+ht then2 no de$a"s. &end "our 3rope$$er a &3IN pro+ram and it !i$$ do a$$
5
sorts o0 thin+s be0ore it actua$$" does an"thin+ "ou !ant it to do. None o0 this is bad. It is
ho!e(er2 a trade o00 !orth notin+.
7his document is no! comp$ete2 !ith one basic assemb$" $an+ua+e pro+ram 0u$$" detai$ed.
7he re$ated concepts2 instructions2 and termino$o+" are e-p$ained in a detai$ed and
con(ersationa$ 0ashion.
7he reader !i$$ be $e0t !ith a so$id 0oundation understandin+ o0 the e$ements o0 assemb$"
$an+ua+e pro+rammin+ +ood enou+h to consume more ad(anced te-ts and pro+ress 0rom
there2 Aust as the author is doin+:
,nAo" the ride:
6
Core Concepts
*e0ore !e +et to doin+ an" rea$ assemb$" $an+ua+e pro+rammin+2 !eF(e +ot to +et some
+round co(ered 0irst. Computer math is $iDe ordinar" math2 0or the most part. Most o0 the
troub$e comes 0rom ha(in+ to $earn di00erent number representations2 a$on+ !ith some
termino$o+".
Be a$so need to co(er the basic math computers do. It is this basic math !e use to do
hi+her $e(e$ thin+s2 in a 0ashion not un$iDe ho! 5,6< to"s combine to+ether to 0orm $ar+er
structures.
5etFs Aump ri+ht in:
#umber $epresentations
7he rea$it" is !e share the same numbers computers do. 7he bi+ di00erence is in ho! !e
represent them: &Dip this section2 i0 "ou understand number representation basics. 7he
taDe a!a" here is that binar" 1 (N2 octa$2 or base % (NN12 decima$ 12 and he-adecima$ O1
are a$$ the same (a$ueP name$"2 one:
<ur representation o0 choice is base 10. <ne character2 0or each 0in+er2 arran+ed in di+its2
each representin+ +reater po!ers o0 ten2 bui$din+ to the $e0t thus/
1234 = (1 x 10
3
) + (2 x 10
2
) + (3 x 10
1
) + (4 x 10
0
)
Be depend on the po!ers o0 102 0or this representation to !orD. 7hese are/
10
0 =
1
10
1 =
10
10
2 =
100
10
3 =
1000
7!o (er" common numerica$ representations !e use to interact !ith computers are/
Binary (base % & ' () )) 'N'
%1101 = (1 x 2
3
) + (1 x 2
2
) + (0 x 2
1
) + (1 x 2
0
) = 13
*inar" depends on the po!ers o0 )2 Aust as base 10 depends on the po!ers o0 10. ,ach
binar" di+it is Dno!n as a JbitK.
2
0
= 1
2
1
= 2
2
2
= 4
2
3
= 8
2
4
= 16
7
2
5
= 32
2
6
= 64
2
7
= 128
*e+adecimal (base &, -./CBA012,345%&() )) 678
,ach he- di+it can ho$d 1# (a$ues2 instead o0 Aust t!o or 10. 7he" are represented b" the
characters 0 G M2 noted abo(e2 !here A Q 10 and M Q 142 !ith the other di+its in seLuence.
$F0E4 = ( F x 16
3
) + (0 x 16
2
) + (E x 16
1
) + (4 x 16
0
) = 61668
5iDe binar"2 he-adecima$ depends on a di00erent base set o0 po!ers/
16
0
= 1
16
1
= 16
16
2
= 256
16
3
= 4096
7he he-adecima$ number abo(e can be broDen do!n thus/
(15 x 1096) + (0 x 256) + (14 x 16) + (4 x 1)
7hese numerica$ representations are an essentia$ part o0 assemb$" $an+ua+e pro+rammin+2
and 0or understandin+ ho! computers do thin+s2 !h" some numbers are 0aster than others2
etc...
It is an e-treme$" +ood idea to memorize the 0irst 1# po!ers o0 t!o. 7he" doub$e2 so itFs
not a hard tasD. Bith these2 one can easi$" (isua$ize binar" numbers in decima$ terms2 and
con(ert 0rom binar" to he-adecima$2 b" hand2 i0 necessar".
Rno!in+ the 0irst % po!ers o0 1# is a nice bonus:
7here are other numerica$ representations2 such as octa$ (base %2 not co(ered here.
7he important thin+ to remember is a +i(en (a$ue2 ma" be represented man" di00erent
!a"s2 "et it is the same (a$ue. I0 !e !ere to count up to the number 42 !e !ou$d count/
12)2.2424
In binar"2 this is/
12 102 112 1002 101
In both cases2 !e are at the (a$ue represented b" 4 or N101.
Beore !e mo"e onto si#es and other core math elements$ it is important to note that
8
computers deal !ith "alues the same !a% !e do. &o!e"er$ !hen counting or inde'ing
things$ computers start !ith #ero$ !here !e start !ith one. (his !ill )e e'plained in
more detail later.
Computer number si9es
Computers hand$e speci0ic sized numbers. =n$iDe us2 the" cannot Aust add a di+it2 or taDe
one a!a" 0or c$arit" or con(enience. 7he" are2 a0ter a$$ machines2 and machines !orD in
speci0ic and $o+ica$ !a"s.
7he most common number sizes are/ (in binar" 0orm
Bit
N0 ; 7his is a sin+$e binar" di+it2 or bit. ItFs (a$ue is either one or zero.
#ibble
%0000 ; Nibb$es are ha$0 a b"te2 or 0our binar" bits. 7heir (a$ue ran+es 0rom 0 to 14. A$so
Dno!n as one he-adecima$ di+it. ,+/ OA
Byte
%11110000 G *"tes consist o0 % bits or t!o he-adecima$ di+its. 7he" ran+e 0rom 0 to )44
7he b"te abo(e is OM0 in he-adecima$.
:ord
%1111111100000000 ; Bords are t!o b"tes2 1# bits2 4 he-adecima$ di+its.
7he !ord abo(e is OMM00
Long
%11111111000000001111111100000000 G 5on+s are t!o !ords.
7he $on+ abo(e is/ $FF00FF00
Mrom no! on2 the notations 0or he-adecima$ and binar" JO NK !i$$ be used2 a$on+ !ith the
number size notation. No! !e +et to ta$D bits2 b"tes2 nibb$es2 he-adecima$ and binar". Be
are no! read" to be+in $earnin+ about the computer2 ha(in+ $earned somethin+ about itFs
$an+ua+e.
9
Common computer numerical units
Ri$ob"te (Rb Q 10)4 b"tes Q )
9
Q N1000000000
Me+ab"te (Mb Q 10)4 Ri$ob"tes
(here are some ne! units )eing introduced that are )ased o o *000$ not *02+. (he% are
ki) and mi)$ or kilo)%te and mega)%te. , don-t think these make a lot o sense$ largel%
)ecause the% do not le"erage the po!ers o t!o$ ound e"er%!here in computers. ,-m
putting them here )ecause %ou ma% )ump into them$ not )ecause the% ha"e an% real
"alue.
Pages
A pa+e re0ers to a discrete unit o0 size2 +enera$$" based on one o0 the core po!ers o0 t!o2
and or number size. A pa+e o0 memor"2 0or e-amp$e2 mi+ht be de0ined as bein+ )4# b"tes G
Aust the e-act amount one b"te can di00erentiate. 3a+es can be an"thin+ thou+h. 1# b"tes2
#44.# b"tes (#4Rb"tes2 or #4R2 etc...
In modern computers2 a pa+e is +enera$$" much $ar+er than that. Mor no!2 Aust Dno! a pa+e
re0ers to a chunD o0 seLuentia$ memor" addresses2 a$most a$!a"s De"ed to a po!er o0 t!o.
Personification
Mor a $ot o0 reasons2 itFs easier to personi0" the computer. 7reatin+ it $iDe a partner2 instead
o0 Aust some arbitrar" thin+2 he$ps both !ith !ritin+ and to 0aci$itate communication. 7his
te-t !i$$ 0o$$o! that con(ention2 so donFt be a$armed !hen "ou see it2 instead o0 Jthe
computerK a$$ the time.
3ersoni0ication2 in +enera$ is a use0u$ too$. <ur brains are !ired to re$ate to other peop$e
better than an"thin+ e$se. Bhen !e thinD in terms o0 peop$e2 o0ten di00icu$t concepts
become easier to +rasp in a manner simi$ar to ho! di00icu$t socia$ situations are more easi$"
+rasped !hen one ro$e p$a"s2 or identi0ies !ith one or more participants.
ItFs a $earnin+ too$2 nothin+ more.
Computer Concepts )) 6:hat is it; *o! does it !or<; :hat are the parts of it;8
CP=
7he core o0 an" computer is itFs Centra$ 3rocessin+ =nit2 or C3=. 7his is !here the !orD
+ets done. Instructions and data come in2 di00erent data and instructions come out. ItFs a$$
Aust ones and zeros. It a$$ happens (er" 0ast ho!e(er2 and thatFs !here the po!er is.
7hinD o0 the C3= $iDe a bi+ ca$cu$ator. In 0act2 itFs he$p0u$ to ima+ine "ou bein+ the C3=2
10
and "our ca$cu$ator bein+ one part o0 !hat a C3= does. Mor a computer2 the C3= is the
brain. It can maDe decisions2 0o$$o! directions2 read and !rite data and do math
computations.
$A>
*rains and C3=Fs need memor". Memor" is !here instructions are stored2 a$on+ !ith resu$ts
and other data. Most C3=Fs are connected to a Luantit" o0 Random Access Memor"2 or RAM.
7he RAM is addressed2 in seLuence 0rom the 0irst stora+e $ocation J0K to the $ast one2 sa"
O7MMM in the case o0 the 3rope$$er.
6i(en the abo(e units and termino$o+"2 !e then can sa" the 3rope$$er has .)Db o0 RAM.
7his is .) S 10)4 b"tes Q .)7#% Q O7000
Iou mi+ht ha(e noticed that O7000 does not eLua$ .)7#%: ItFs rea$$" .)7#72 so !h" is thatC
7his is the 0irst e-amp$e o0 !here a Luantit" di00ers 0rom an address or inde-. Remember2
computers count 0rom zero. &o the 0irst e$ement o0 an"thin+ is zero2 not one $iDe !e are
accustomed to.
Bhen ta$Din+ about the amount o0 ram2 !e use a Luantit". 7hatFs the .)7#% number. Bhen
ta$Din+ about addresses2 and in this case speci0ica$$"2 !hich addresses address the RAM 0or
us2 !e then start 0rom 0 and arri(e at .)7#72 or O7MMM.
In the 3rope$$er2 !e ha(e !hat is ca$$ed 1# bit address space. 7his +i(es us addresses that
$ie bet!een O0000 and OMMMM. Ha$0 o0 that address space is RAM2 endin+ at O7MMM. 7he
other ha$02 startin+ 0rom O%000 and endin+ at OMMMM is R<M.
$?>
Read on$" memor" is memor" that is addressed Aust $iDe RAM is2 but it does not store
an"thin+ ne!. <ne cannot put an"thin+ into R<M2 on$" read 0rom it. <0 course2 the
Luestion comes up2 J!hat +ood is thatCK.
Computers po!er up each time Dno!in+ nothin+. In 0act2 their RAM is o0ten 0i$$ed !ith
!hate(er numbers happen to resu$t 0rom e(er"thin+ po!erin+ up. R<M then is stab$e
memor" that contains instructions 0or the computer to 0o$$o! be0ore doin+ an"thin+ e$se.
In the case o0 the 3rope$$er2 it needs to +et started $ooDin+ 0or pro+rams to run2 c$ean up itFs
RAM2 and do some other basic thin+s 0or us. 7he R<M ho$ds those instructions2 p$us some
hand" data2 $iDe characters2 con(ersion tab$es and other +oodies !e mi+ht 0ind hand" to
use in our o!n pro+rams.
7he R<M a$so contains the interpreter 0or the &3IN $an+ua+e "ou !ere $iDe$" usin+ be0ore
11
decidin+ to tr" assemb$" $an+ua+e.
I@?
<n the prope$$er2 it +ets its input and sends itFs output to and 0rom the outside !or$d2 (ia
itFs .) I8< pins. ,ach o0 these pins can be in a binar" state2 or dependin+ on ho! "ou
instruct the computer. 7here are other states too2 $iDe those used 0or the (ideo2 but those
are be"ond the scope o0 this document.
Mor no!2 this is enou+h to continue to $earn about ho! assemb$" $an+ua+e !orDs. 5ater !e
!i$$ e-p$ore the 3rope$$er and itFs % C3=Fs (C<6s2 the H=* and other on;chip capabi$ities.
-lags
C3=s note thin+s !hi$e the" are computin+. I0 an operation resu$ts in a zero2 or perhaps
has a bit to carr" o(er2 these thin+s are stored in the 0$a+s. 7hinD o0 it $iDe this. Iou are
the C3=2 and "ou are operatin+ on numbers !ith "our ca$cu$ator.
&o "ou read the number2 thatFs $iDe readin+ 0rom RAM or R<M memor". Iou input it to the
ca$cu$ator2 then "ou input another number and per0orm the addition. I0 that resu$t ended
up bein+ zero2 "ou !ou$d maDe a menta$ note o0 that. I0 that operation ended up bein+
bi++er than "our ca$cu$ator can hand$e2 "ou note the o(er0$o! and carr" that bit o(er to
another addition ma"be.
7hatFs !hat the status 0$a+s are a$$ about.
<n the 3rope$$er2 !eF(e +ot a 9ero flag and a carry flag.
7here is a$so another conte-t 0or the term F0$a+F2 and that is a$$ about preser(in+ transient
states that ma" need to be acted upon $ater2 a0ter the state has come and +one. In this
conte-t2 a 0$a+ then is set2 or reset to note an e(ent or resu$t 0or processin+ $ater on.
An"thin+ can be a 0$a+. 3in states2 (a$ues Gin 0act an" (a$ues2 not Aust true or 0a$se2 but Aust
(a$ues2 such as/ O402 N000101012 or OMMMM. A 0$a+2 used in this conte-t2 is compared to a
Dno!n 0$a+ state2 then decisions are made based on the resu$t.
7aDe (ideo as an e-amp$e. 9ideo disp$a"s happen scan $ine b" scan $ine. I0 there !as a
counter2 ticDin+ o00 the scan $ines as the" are dra!n2 it can act $iDe a 0$a+: A$$ the
pro+rammer needs to do is instruct the computer to !atch that scan $ine counter and maDe
a decision !hen itFs state matches a Dno!n one. ,+/ branch2 i0 scan $ine counter Q O40
?pcodes (Instructions)
12
,ach assemb$" $an+ua+e instruction is associated !ith a number. Mor sanit"2 !e use an
assemb$er to associate those numbers !ith mnemonics2 such as >?A (7&(&(((). 7his
instruction e-amp$e mo(es a number 0rom one p$ace to another. 7here is a $ist o0 a$$ the
3rope$$er opcodes2 a$on+ !ith (erbose descriptions o0 their beha(ior in the Assemb$"
Re0erence section o0 the 3rope$$er Manua$. <pcodes de0initions !i$$ not be repeated here2
other than 0or c$arit".
At some $e(e$2 e(er"thin+ !e do !ith a computer is mathematica$. Computers are number
manipu$ation machines. 7his means !e need to +et +ood at representin+ thin+s !ith
numbers2 so !e can instruct the computer to manipu$ate them in use0u$ !a"s. Ha(in+
co(ered enou+h core concepts2 itFs time to ta$D some math.
13
Computer >ath
As discussed be0ore2 computers rea$$" on$" add. 7he" do $ots o0 additions on $ots o0
numbers (er" LuicD$" to per0orm the tasDs !e reLuire o0 them. I0 "ou are readin+ this on a
computer2 or near a computer2 that computer is $iDe$" per0ormin+ bi$$ions o0 adds per
second2 i0 not more.
<n a side note2 bacD !hen I 0irst started computin+2 !e !ere ta$Din+ mi$$ions o0 adds e(er"
second. 7he pro+ression o0 computer speed is sta++erin+2 +i(en the short time !e ha(e
been actua$$" computin+.
7he 3rope$$er chip does mi$$ions o0 adds per second. 7his is one o0 the thin+s that
di00erentiate it 0rom a 0u$$ on 3ersona$ Computer. DonFt !orr" thou+h. Mi$$ions o0 adds per
second is sti$$ 0ast enou+h to do amazin+ thin+s:
*e patient p$ease: BeF(e co(ered a 0air amount o0 +round. ItFs a$most time to be+in !ritin+
some assemb$" $an+ua+e pro+rams: &oon a0ter that2 !e !i$$ be runnin+ them on the actua$
3rope$$er chip and m" purpose here !i$$ ha(e been accomp$ished. ItFs eas" to sDip the core
basics and Aust start doin+ stu00. ItFs a$so harder to +o bacD and 0i+ure out !hat !ent !ron+
that !a" too.
I0 "ou Aust ha(e to sDip 0or!ard and do stu002 do it: 7hen come bacD and continue !ith the
core stu00 here.
Addition (A//
7his is the core operation. Mor our +enera$ sanit"2 t"pica$ C3=Fs pro(ide a number o0
operations2 such as/ subtract2 shi0t2 and i0 "ou are $ucD" an mu$tip$". A$$ o0 these depend
on addition2 so it maDes sense to co(er that 0irst.
*inar" addition is Aust $iDe ordinar" addition2 but 0or the 0act !eF(e rea$$" on$" +ot t!o
di+its: 7his maDes thin+s rea$$" eas" actua$$". Mor an" co$umn o0 bits to be added2 the
resu$t !i$$ either be 02 12 10 or 11 (0212)2. !ith t!o and three needin+ a carr" to o(er to
the ne-t co$umn thus/
1 1 <---- Carry
23 010
+49 + 011
-- ---
72 101 <----- Sum
5etFs taDe a c$ose $ooD at the e-amp$e abo(e. <n the $e0t2 !e ha(e ordinar" decima$
addition2 Aust $iDe !eF(e a$$ done on paper at one time or another. 7o +et to the so$ution2
!e start !ith the ri+ht hand co$umn2 add the . and the 9 and +et 1). 7hatFs t!o di+its:
More than !i$$ 0it in that co$umn2 ri+htC
&o2 !e carr" the 12 note the ) in the sum2 then continue !orDin+ on the ne-t co$umn.
14
7hatFs a ) p$us 42 p$us the 12 carried o(er2 0or a tota$ o0 7. Be then can read the sum as
bein+ 7).
7he binar" e-amp$e is no di00erent: ItFs actua$$" a $ot easier because !eF(e on$" +ot 02 12 10
or 11 to !orr" about. 7hatFs on$" 0our possibi$ities.
BorDin+ 0rom the ri+ht then2 !eF(e +ot 0 p$us 12 0or a 1 in the sum. 7he ne-t co$umn o(er is
1 p$us 12 so that! 10 Ga carr". Carr" the 1 to the third co$umn2 note the 0 in the sum2
and add the $ast co$umn 0 p$us 0 p$us 12 eLua$s a 12 !hich +oes in the $ast sum co$umn2 0or
a tota$ o0 101.
Bhat did !e addC 5etFs taDe a LuicD $ooD at con(ertin+ binar" to decima$ !ith the po!ers
o0 t!o/
010 = 2 011 = 3 101 = 5
""" """ """
"""### ( 0 x 1) = 0 """### ( 1 x 1) = 1 """### ( 1 x 1) = 1
"" "" ""
"" ####( 1 x 2) = 2 + ""#### ( 1 x 2) = 2 = ""#### ( 0 x 2) = 0
" " "
"######( 0 x 4) = 0 "##### ( 0 x 4) = 0 "##### ( 1 x 4) = 4
,ssentia$$"2 a$$ "ou need to do is taDe the po!ers o0 t!o2 checD to see i0 there is a one in
their co$umn2 then add them a$$ up. ,ither that po!er o0 t!o is represented in the number
or it isnFt: 7his is the beaut" o0 binar". In the e-amp$e abo(e2 !e added ) T . Q 4.
<ne more e-amp$e/
N10010011
5etFs sa" the bits are numbered 0 to 72 startin+ at the ri+ht most bit. 7he con(ersion then
$ooDs $iDe this/
1 T ) T 0 T 0 T 1# T 0 T 0 T 1)% Q 147
Be number bits2 0rom ri+ht to $e0t2 accordin+ to the po!er o0 t!o2 the" represent. 7he
ri+ht most bit is ca$$ed $east si+ni0icant di+it and the $e0t most is ca$$ed most si+ni0icant
di+it. (LB/ C >B/ &i+ni0icance is a$$ about the (a$ue an" one di+it represents. 7he
0arther $e0t "ou +o2 the more si+ni0icant it is.
.ost personal computers ha"e a calculator program that !ill do these
num)er con"ersions or %ou. /se it$ )ut onl% ater %ou ha"e !orked through
some addition and con"ersion on paper to understand !hat the digits mean$
remem)er %our po!ers o t!o and !hen to carr%. (hese things are important
going or!ard.
0 great e'ercise is to take some common num)ers$ con"ert them to )inar%$
15
then con"ert them to he'adecimal$ then )ack to decimal.
1n a .icrosot 2indo!s computer$ %ou choose "ie!$ scientiic rom the
program menu.
>ore on addition (The carry)
Bhat happens !hen the resu$t e-ceeds the number o0 co$umns in an additionC Mor us
peop$e2 !e Aust add another co$umn and 0inish out the operation thus/
11 11 <---- Carry
23 110
+ 99 + 111
-- ---
122 1101 <----- Sum
Bhen !orDin+ on paper2 !e can do the same 0or binar" numbers. Computers !orD a bit
di00erent$" ho!e(er. Computers !orD !ith 0i-ed number sizes. I0 !e are addin+ b"tes2 !e
are rea$$" addin+ numbers that ran+e 0rom 0 to )44. I0 the resu$t is bi++er than that2 an
e-tra di+it2 or carr" is reLuired. 7his can easi$" happen and has some interestin+ e00ects
that !i$$ $ead us to subtraction and modu$o operations.
5etFs start !ith a b"te addition e-amp$e/
%11001110
+ %01100001
---------
1 00101111
In this case2 !eF(e +ot a resu$t that is bi++er than one b"te can ho$d2 so !here does the
e-tra di+it +oC
Dependin+ on !hat "ouF(e asDed the computer to do2 that e-tra di+it mi+ht end up in a
specia$ $ocation2 ca$$ed a 0$a+2 0or use in a subseLuent addition operation2 or decision
(branch operation2 or it is Aust i+nored:
*oth o0 these cases are meanin+0u$ and use0u$. 5etFs taDe the e-amp$e !here the e-tra
di+it is stored in the carry flag. <n the 3rope$$er2 the C3= maintains t!o 0$a+s2 to be
discussed in more detai$ $ater. Mor no!2 a$$ !e need to Dno! is that the carr" 0$a+ !i$$ ho$d
that o(er0$o! di+it 0or us2 i0 !e !ant to.
In the e-amp$e abo(e2 !e are +onna need t!o b"tes to ho$d the resu$t. 7hat $ooDs $iDe this/
1 <------
%00000000 %11001110
%00000000 Carry F$a% + %01100001
--------- ---------
16
%00000001 1 <----- %00101111
In this e-amp$e2 the ne-t b"te to be added is zero. 7he carr" then Aust JcarriesK o(er to
the $east si+ni0icant di+it2 and the addition be+ins a+ain. 7he resu$t o0 our addition then is
a t!o b"te number/ %00000001&00101111
/elimiters
7he underscore is a thin+ ca$$ed a de$imiter. ItFs i+nored b" the 3rope$$er pro+rammin+ too$2
and e-ists 0or c$arit". *i+ computer numbers are hard 0or us to process. Actua$$"2 bi+
numbers in +enera$ are hard 0or us to process.
<(er time2 it has been 0ound that an ordinar" person 0ee$s +ood about . G 4 di+its. 3ast
that2 the"F(e +ot to $ooD the number o(er a 0e! times and ma" +et con0used. 7hatFs !hat
the de$imiter is 0or.
7he number o0 di+its prob$em is !h" he-adecima$ is so hand". ,ach di+it is po!ers o0 1#2 so
!e can represent (er" $ar+e numbers !ith 0e! di+its.
Be !ou$d use the comma J2K $iDe !e do ordinar" decima$ numbers2 but that character
s"mbo$ sees a $ot o0 use in computer pro+rammin+. Mor a $ot o0 reasons2 itFs Aust easier to
use somethin+ e$se. 7he 3rope$$er too$ actua$$" $ets "ou picD. In this document2 the
underscore !i$$ be used. ItFs a$so the most common seen in the !i$d.
Bac< to the math"""
7he carr" e-amp$e abo(e opens the door to addin+ rea$$" bi+ numbers. 7he 3rope$$er can
hand$e up to $on+ sized binar" numbers. 7hatFs .) bits2 or a ran+e o0/ 0 to 4)949#7)9#:
Mor most tasDs2 a .) bit number is Aust 0ine. ItFs ran+e is p$ent" 0or computation2 inde-in+2
etc...
Ho!e(er2 there !i$$ come times !hen itFs Aust not bi+ enou+h2 so !e need to brie0$" e-pand
on the carr" e-amp$e and add t!o discrete$" sized numbers to+ether. Mor c$arit"2 b"tes !i$$
be used. It a$$ !orDs the same ho!e(er2 no matter !hat sizes are actua$$" in p$a". Addin+ a
one b"te number to a t!o b"te one $ooDs $iDe this/
1 1111
%00110000&11001110
+ %10010011
------------------
%00110001&01100001
Note the carr" o(er to the most si+ni0icant b"te abo(e.
I0 the computer !ere on$" ab$e to !orD !ith b"te sized numbers2 t!o adds !ou$d be
reLuired2 and that $ooDs 0ami$iar/
17
1 <---
%00110000 %11001110
%00000000 Carry F$a% + %10010011
--------- ---------
%00110001 1 <----- %01100001
Another important concept comes into p$a" here2 and that is e-tendin+ a number. In the
case o0 addin+ one b"te2 to a t!o b"te number2 !e rea$$" per0orm t!o adds2 and that
means ha(in+ t!o operands 0or the addition.
3icture it $iDe this/
1 1111
%00110000&11001110
+ %00000000&10010011
------------------
%00110001&01100001
Addin+ t!o b"te numbers is rea$$" no di00erent2 because !e e-tend the sma$$er one an"!a".
In the e-amp$e abo(e2 !e rea$$" cou$d Aust put some ones in there and be addin+ t!o b"tes.
,t is important to note that adding same si#ed num)ers ()%te to )%te 3 !ord
to !ord)$ !ill result in an o"erlo! 3that e'tra carr% digit. (his is al!a%s
true$ no matter !hat si#e is !orked !ith.
0n%time %ou are adding num)ers$ %ou need to decide !hat %ou !ant to do
!ith the carr% )it. 4ither ignore it$ make a decision )ased on it$ or add it to
the remainder o the addition )eing perormed.
7hat brin+s us bacD to case number t!o abo(e2 !here !e i+nore the carr". Bhat happens
thenC
>odulo (or !raparound)
7his is a (er" important assemb$" $an+ua+e concept. Bhen "ou per0orm an operation2
!hose resu$t is $ar+er than the number size bein+ !orDed !ith2 that o(er 0$o! +oes into the
carr" 0$a+2 or is i+nored.
7his causes numbers to !raparound and start a+ain at zero: 5etFs sa" !e are !orDin+ !ith
nibb$es2 !hich are ha$0 a b"te.
Nibb$es ho$d the numbers 0 throu+h 14. I0 !e add one to a 0u$$ nibb$e2 !e end up !ith an
empt" one bacD at 0: It !raps around.
18
Another !a" to $ooD at it is !hen countin+2 the $east si+ni0icant di+its +o throu+h a re+u$ar
pattern. 7he" do their thin+ o(er and o(er. In decima$2 a +reat e-amp$e is the odometer
on "our car. Most cars ha(e t!o o0 these. <ne (er" $ar+e one to record tota$ mi$es and one
sma$$er one to note sma$$er distances.
I0 both o0 these are set to zero2 and the sma$$er one ho$ds three di+its2 it !i$$ !raparound
bacD to zero2 !hen the $ar+er one continues on to note 1000. Computer numbers !orD the
same !a"2 !rappin+ around !hen a$$ their bits are set to one.
0000 + 1 = 0001 1110 + 1 = 1111
0001 + 1 = 0010 1111 + 1 = 0000 ('arry a 1)
7hatFs ho! modu$o !orDs.
Computer numbers then2 because o0 their discrete sizes are a$$ modu$o numbers. 7his is
not a bu+2 but a 0eature: A use0u$ one too.
7his $eads us to....
Bubtraction
<n paper2 subtraction !orDs Aust $iDe addition does2 on$" !eF(e +ot a borro! instead o0 a
carr". 7here is a short cut ho!e(er and that shortcut taDes ad(anta+e o0 the modu$o 8
!raparound concept discussed abo(e.
Rea$$"2 !e subtract b" addin+. 7his in(o$(es chan+in+ one o0 the operands so that !hen it is
added to the other one2 the resu$t !raps around to end up correct2 Aust as i0 !e actua$$"
subtracted in the ordinar" !a". 7his is ca$$ed comp$imentin+ a number and shou$d be
discussed brie0$" in the conte-t o0 subtraction.
Complements
Mirst2 $etFs tr" it !ith decima$. 5etFs sa" !e are !orDin+ !ith a sin+$e di+it/
4 4
-2 +8 <---- 8 (! th) '*m+$(m),t *- 2
- -
2 (1)2
7he De" here is to i+nore the carr" and on$" $ooD at the modu$o 8 !raparound resu$t. In this
simp$e e-amp$e2 % is the comp$iment o0 t!o2 because !hen added it does the same thin+ as
subtractin+ a t!o !ou$d do.
No! $etFs $ooD at some binar"/
%1001 %1001
- %0110 + %1010
---- -----
0011 (1)0011
19
*oth o0 these +i(e the same resu$t2 i0 !e i+nore the carr". &ubtraction then can be done
throu+h addition. Mor b"tes2 one cou$d add O00 to subtract 12 b" !a" o0 another e-amp$e.
7he 3rope$$er has a subtract instruction2 so !e rea$$" can Aust o00er it t!o numbers2 asD it
to subtract and mo(e on. Comp$iments do crop up a$$ o(er the p$ace ho!e(er. Mor no!2
Aust Dno! "ou can e-press an" subtraction as an addition2 and understand ho! that ties in
!ith the modu$o and !raparound concepts.
>ultiplication
5iDe addition2 mu$tip$ication !orDs the same !a" addition does. 7he manua$ steps !orD the
same as the" do 0or decima$ mu$tip$ication. Here is a LuicD e-amp$e/
Note/ 0S0 Q 0 1 S 0 Q 0 1 S 1 Q 1 (thatFs it:
%101
x % 11
----
101
+ 101
-----
1111
Bhere decima$ mu$tip$ication maDes mu$tip$" b" 10 rea$$" eas"2 binar" !orDs the same !a"2
but 0or mu$tip$"in+ b" ). Uust tacD a zero on the end2 and "ouF(e mu$tip$ied b" t!o:
/iision
Here is a LuicD di(ision e-amp$e.
10001
&&&&&&&&
11)110011
-11
--
000
- 000
---
011
011
20
/ecimal to Binary conersions
Remember those po!ers o0 t!oC 7he" come in e-treme$" hand" 0or this tasD. *asica$$" "ou
need to 0ind the +reatest po!er o0 t!o2 that can be subtracted 0rom the decima$ number
and start 0rom there2 repeated$" subtractin+ po!ers o0 t!o2 Deepin+ tracD o0 those "ouF(e
subtracted up a$on+ the !a". Bhen there is no more subtraction bein+ done2 "ou ha(e "our
binar" number.
Here is an e-amp$e/
)7 Q
.) is the #
th
binar" di+it2 but itFs too bi+. 1# is the ne-t $ar+est po!er o0 t!o2 and itFs $ess
than )72 so !e start there.
)7 G 1# Q 11
No! the process repeats. % is the ne-t $ar+est po!er o0 t!o that !orDs2 so !e subtract
that.
11 G % Q .
4 is too bi+2 so !e subtract t!o.
. G ) Q 1
7hat $ea(es 12 and !e subtract that as !e$$.
1 G 1 Q 0
A$$ done: No! !e ta$$" up the po!ers o0 t!o !e subtracted and arri(e at our binar"
number/
,ach po!er o0 t!o is a di+it. Bhere !e didnFt use one2 !e p$ace a zero2 !here !e did2 !e
p$ace a 1.
% 0 1 1 0 1 1 ----. %011011
" " " " " "
32 16 8 4 2 1
7he $eadin+ zero can be $e0t o002 $ea(in+ us !ith N11011
#egatie or signed numbers
21
&o 0ar2 a$$ !eF(e !orDed !ith is positi(e inte+ers. ItFs time no! to discuss si+ned numbers.
,ssentia$$"2 !eF(e +ot to encode the si+n into !hate(er bits !eF(e +ot to !orD !ith. 7his
breaDs do!n a 0e! !a"s/
Bigned magnitude
7his is !here !e use the upper most bit as a si+n indicator. A zero is positi(e and a one is
ne+ati(e. I0 our number size is b"te2 then !eF(e +ot % bits2 !ith the $e0t most bit bein+ the
si+n thus/
%00000101 (+5) /)'*m)! %10000101 (-5)
Be trade the hi+hest number represented ()44 0or ha(in+ a si+n (T1)7 G 1)%
?ne's compliment
*asica$$"2 "ou Aust 0$ip a$$ the bits: 7his $ea(es the upper most bit as the si+n2 Aust as in the
e-amp$e abo(e. 7his is Aust a di00erent representation.
%00000101 (+5) /)'*m)! %11111010 (-5)
01*! '*m+$(m),t
&tart !ith ones comp$iment2 then Aust add one.
%00000101 (+5) /)'*m)! %11111011 (-5)
,t-s important to note !hich representation !as used. 2ithout this$ one
cannot kno! !hat the original "alue !as.
Most computers toda" operate direct$" on 7!oFs comp$iment si+ned numbers. 7his inc$udes
the 3rope$$er.
22
.+ample math operations
I0 this is "our 0irst pass throu+h this document2 "ou can sDip this section2 then come bacD
a0ter ha(in+ !orDed throu+h the pro+ram e-amp$e. &ometimes one needs to Dno!
somethin+2 a$on+ !ith somethin+ e$se2 0or a concept to become c$ear. 7oo much at the
!ron+ time can cause troub$e and con0usion.
Ha(in+ started to read this section2 "ou ma" e-perience this. Mee$ 0ree to mo(e on to the
other 0oundation concepts and return $ater: 7hese e-amp$e bits o0 code are not meant to
be run direct$". 7he" do per0orm their 0unction2 but are rea$$" intended to be consumed
then used in the conte-t o0 a +reater pro+ram.
Adding t!o unsigned 5% bit numbersD
230
456 0 7)%(, at C*% 538 a99r 0
a99 :;82< :;81

:;81 $*,% %0001000&00000111&11111111&00000000
:;82 $*,% %1111000&00110110&11100011&00000001
:;82 $*,% %0000000&00111110&11100010&00000001 (Carry !)t)
A0ter the add instruction e-ecutes2 N=M) !i$$ contain the second resu$t sho!n2 and the
carr" 0$a+ !i$$ be set2 as the resu$t is $ar+er than .) bits can ho$d.
7he De" here2 to remember is the Destination contains the resu$t o0 the math operation.
Bet flag states (Eero and Carry flags)
&ome operations reLuire a Dno!n 0$a+ state to !orD proper$". 7here are no instructions in
the 3rope$$er to direct$" manipu$ate the 0$a+ states. 7he easiest !a" to accomp$ish this is
to simp$" per0orm an operation !ith a Dno!n resu$t. Here are some Dno!n operations that
!i$$ set or c$ear the carr" and zero 0$a+s. 7he" depend on t!o C<6 memor" $ocations bein+
de0ined as a$$ ones or a$$ zeros.
456 0 7)%(, at C*% 538 a99r 0
Carry x*r &05;E< =0 ,r< 1' C$)ar Carry< E>), ?ar(ty = 0
x*r &05;E< =1 ,r< 1' S)t Carry< 499 ?ar(ty = 1
@)r* x*r &05;E< &05;E ,r< 1A S)t @)r*< 5)!u$t = 0
x*r &05;E< &F3BSE ,r< 1A C$)ar @)r*< 5)!u$t = ,*t A)r*

&05;E $*,% $FFFFFFFF a$$ *,)!
&F3BSE $*,% $00000000 a$$ A)r*!
23
7he instructions ha(e t!o e00ects speci0ied. 7he FnrF e00ect sa"s2 Jdo the operation2 but
donFt !rite it to C<6 memor".K 7he F!zF or F!cF e00ect sa"s2 Japp$" the resu$t to the 0$a+K.
7hese t!o combined resu$t in on$" the 0$a+ state chan+ed2 nothin+ e$se.
Adding !ith carry to represent larger numbers of different si9e, unsigned
in the e-amp$e abo(e2 the resu$t is $ar+er than .) bits. Here is an unsi+ned addition o0 a .)
bit number to a #4 bit one2 to 0u$$" capture that resu$t/
456 0 7)%(, at C*% 538 a99r 0
Start a99 :;82< :;81 a99 :;81 t* :;82< 'arry
a99x :;82a< =0 a99 th) 'arry t* :;82a
:;81 $*,% %0001000&00000111&11111111&00000000 32 /(t !*ur')
:;82 $*,% %1111000&00110110&11100011&00000001 $*1)r 32 /(t!
:;82a $*,% %0000000&00000000&00000000&00000000 u++)r 32 /(t!
&05;E $*,% $FFFFFFFF a$$ *,)!
&F3BSE $*,% $00000000 a$$ A)r*!
Be start !ith the add instruction2 because it !i$$ i+nore the state o0 the carr"2 per0orm the
add2 then set the carr" state in preparation 0or subseLuent add- instructions. In the second
addition2 the @0 is used to represent the empt" upper .) bits o0 N=M12 to comp$ete the
addition. 7his is $iDe the e-tension discussed in the addition section ear$ier.
Add ,4 bit number to ,4 bit number, unsigned
456 0 7)%(, at C*% 538 a99r 0
Start a99 :;82< :;81 a99 :;81 t* :;82< !)t 'arry
a99x :;82a< :;81a a99 th) 'arry t* :;82a
:;81 $*,% %0001000&00000111&11111111&00000000 $*1)r 32 /(t!
:;81a $*,% %0001000&00000001&00000000&00000000 u++)r 32 /(t!
:;82 $*,% %1111000&00110110&11100011&00000001 $*1)r 32 /(t!
:;82a $*,% %0000000&00000000&00000000&00000000 u++)r 32 /(t!
7his is rea$$" no di00erent than doin+ the .) bit to #4 bit. &ince both numbers are the same
size2 the F@0F is rep$aced !ith FN=M1aF indicatin+ the upper .) bits o0 N=M1 are to be added.
7he #4 bit resu$t ends up in N=M) and N=M)a.
24
Propeller Bpecific Core Concepts
Ha(in+ co(ered the e$ementar" math and termino$o+" necessar"2 itFs time no! to start
!orDin+ on the actua$ instructions2 memor"2 0$a+s and ho! the" a$$ interact.
ItFs +ettin+ time to start thinDin+ about some actua$ assemb$" $an+ua+e pro+rams.
7he prope$$er is an interestin+ desi+n2 in that it actua$$" has % C3=Fs2 Dno!n as C<6s2
!orDin+ to+ether. Mor the saDe o0 simp$icit"2 that !i$$ be i+nored 0or this section2 so that
instructions and memor" can be discussed.
7here are some ne! concepts and terms to $earn in this section. 3$ease re0er to the
3rope$$er *$ocD Dia+ram 2 0i+ure 12 0ound in the Propeller P8X32A Preliminary
Datasheet, available from http://www.parallax.om
Cloc<
7he 3rope$$er runs 0rom a s"stem c$ocD2 common to a$$ the C<6s and the H=*. 7he speed
o0 this c$ocD depends on the attached osci$$ator and !hether or not the Interna$ c$ocD2 or
an e-terna$ re0erence is used. &ee the data sheet or 3rope$$er Manua$ 0or more detai$.
7he re0erence c$ocD imp$ementation is %0Mhz.
Bystem Counter (C#T)
7he s"stem counter is a +$oba$2 read on$" counter2 that increments once e(er" s"stem c$ocD
c"c$e.
*=B
7he H=* has a 1# bit address space that is di(ided into RAM and R<M2 discussed ear$ier. A$$
co+s share this memor" space and must coordinate their acti(ities in this space. (7(((( )
72fff) 7he H=* R<M contains the instructions necessar" to +et the 3rope$$er started
runnin+ "our pro+rams. 3ro+rams can come 0rom an attached ,,3R<M memor"2 direct$" or
$oaded into H=* Ram (ia the 3rope$$er 7oo$ ID,.
H=* memor" is addressab$e on a b"te2 !ord or $on+ basis.
7he H=* does not e-ecute an"thin+. It is a data and communications 0aci$it" on$". 7his
means there is no centra$ processor. A$$ C<6s can per0orm identica$$". C<6 0 does ha(e
some si+ni0icance2 in that it is the 0irst C<6 started !ith "our pro+rams.
25
Mo(in+ in0ormation to and 0rom the H=* taDes some time that depends on !here the C<6
doin+ the !orD is in the round;robin access seLuence. A(era+e access is V14 c"c$es.
/ata alignment
As discussed2 computer number sizes are b"tes2 !ords and $on+s on the 3rope$$er. Bhen
these number sizes are stored in H=* memor"2 the issue o0 a$i+nment comes into p$a". It
!orDs $iDe this/
7he $east si+ni0icant t!o bits (bits 0 and 1 o0 an address determines an a$i+nment. Mor
b"tes2 both di+its are si+ni0icant. In other !ords2 b"tes can Aust be an"!here in the H=*
memor". ,ither di+it can be a zero or one2 a$$o!in+ 0or an" address.
Mor !ords2 the $ast di+it is a$!a"s set to zero.
Mor $on+s2 the $ast t!o di+its are set to zero.
,+/ 5etFs sa" !e are $ooDin+ at the address O100.. Bhere is the c$osest proper$" a$i+ned
addressC
/yt) $-) ---. $1003 (,* a$(%,m),t (!!u))
1*r9 $))99 ----. $1002 ($)a!t !(%,(-('a,t /(,ary 9(%(t a A)r*)
$*,% $FFFF0000 ----. $1000 ($)a!t t1* !(%,(-('a,t 9(%(t! ar) A)r*)
C?F
7he % C<6s are a$$ connected to the centra$ H=* and +et access to it in round;robin
0ashion. A$$ o0 the C<6s are identica$ and 0eature their o!n counters2 (ideo +enerator and
RAM memor" space. Additiona$$"2 a$$ C<6 share access to the .) I8< pins.
C<6 memor" is on$" addressab$e on a $on+ basis. 7he )Db o0 C<6 memor" diides into 3&%
longs2 addressed 0rom 7( to 7&ff
C<6 memor" addressin+ is a 0reLuent source o0 con0usion. 6oin+ 0or!ard2 pa" specia$
attention to !here "ou are !orDin+ !ith data to miti+ate this di00erence.
Bhen a C<6 is started !ith the C?F#.: instruction2 itFs memor" space is copied 0rom H=*
memor"2 prior to e-ecutin+ !hate(er instructions are contained therein.
C<6 memor" is not associati(e to H=* memor". <nce $oaded 0rom the H=*2 the H=*
memor" it came 0rom ma" be chan+ed !ithout a00ectin+ the pro+ram and data on the
runnin+ C<6.
26
C?F (
C<6 0 is some!hat di00erent 0rom the other C<6s2 because it is the startup C<6. Bhen
the 3rope$$er chip starts2 C<6 0 runs the Boot Loader pro+ram. Iour pro+rams start 0rom a
sma$$ bit o0 &3IN $an+ua+e code2 at a minimum2 e-ecuted b" the &3IN $an+ua+e interpreter2
a0ter the *oot 5oader comp$etes itFs tasDs.
Mor more 3rope$$er detai$ p$ease re0er to the 3rope$$er Manua$2 or Data sheet. &ome o0 this
materia$ !i$$ be reproduced here 0or c$arit" or conte-t. 6oin+ 0or!ard2 it is assumed "ou
ha(e one or both o0 the 3rope$$er Manua$ and Data &heet.
Mor a si+ni0icant portion o0 this +uide +oin+ 0or!ard2 the discussion !i$$ 0ocus on a sin+$e
C<62 or perhaps t!o C<6Fs. <ne that !i$$ run a &3IN pro+ram2 and another runnin+ an
assemb$" pro+ram.
Instructions
A$$ prope$$er assemb$" $an+ua+e instructions are one 5on+ in size. 7he" ha(e binar" bit
0ie$ds in the 0orm/
100000 001( 1111 999999999 !!!!!!!!!
Instruction 1CRi Condition Destination (d &ource (s
Instruction Q 7he actua$ binar" opcode associated !ith the mnemonic
1CRI Q ,00ects (Instruction modi0iers
1 Q Brite 1ero M$a+ (!z
C Q Brite Carr" M$a+ (!c
R Q Brite resu$t (!r (Destination Re+ister Modi0ied
1 Q Modi0ied (!r
0 Q Not Modi0ied (NR
i Q Immediate addressin+
1perati"e "alue is in the instruction itsel$ rather than in the 516 memor%
location normall% associated !ith the "alue in the 7ource (s) ield.
Conditions
(hese are conditional e'ecution )its that can )e added to most an%
instruction. 2hether or not the instruction e'ecutes$ then depends on the
states o the t!o lags. (%pical conditions are8
i0Wz ; I0 the 1ero M$a+ Q 12 then e-ecute the instruction
i0Wnc ; I0 the Carr" M$a+ Q 02 then e-ecute the instruction
27
3rope$$er assembly instructions normally consume 4 system cloc< cycles. *ranches taDe
either 4 or %2 dependin+ on i0 the" are taDen or not. Hub memor" access taDes more c"c$es
and the e-act amount is determined b" !here the C<6 happens to be in the round robin
access scheme.
7o Deep thin+s simp$e ri+ht no!2 instructions are 4 c"c$es. Bith an %0Mhz c$ocD2 this means
)0 mi$$ion instructions per second2 per C<6.
Little .ndian
7he 3rope$$er is a $itt$e endian desi+n. 7his is !hat "ou need to Dno!/
Aalues and Inde+es
Numbers are stored accordin+ to their Most &i+ni0icant *"te (>BB)2 in seLuence to their
5east &i+ni0icant *"te (LBB).
Here is an e-amp$e/
$13&27&3C&42 $1000 - $42
$1001 - $3C
$1002 - $27
$1003 - $13
(he .7B is stored lo!er in memor% than the 97B.
Instructions in $A>
&a" an instruction assemb$es into the 0o$$o!in+ binar" $on+/
%101010&001(&1111&999999999&!!!!!!!!!
It ends up stored in memor" $iDe this/
%!!!!!!!!&9999999!&1(111199&10101000
7here is $itt$e need to direct$" manipu$ate instructions in memor"2 but "ou ma" 0ind this
use0u$ i0 "ou are $ooDin+ direct$" at H=* memor" !ith another pro+ram or process 0or
debu++in+.
/ata (Btrings, loo<up tables, etc""")
Iou are comp$ete$" 0ree to or+anize data ho!e(er "ou !ant to. ,ndian issues on$" app$" to
(a$ues direct$" manipu$ated b" the 3rope$$er. ,+/
230 7yt) $32< $4E< $5F $1F00 - $32
28
$1F01 - $4E
$1F02 - $5F
Program Counter
,ach C<6 has a pro+ram counter that Deeps tracD o0 !here in memor" it is. 7he 3ro+ram
Counter !orDs up!ard throu+h RAM as instructions are comp$eted. *ranch and Uump
instructions essentia$$" $oad the 3ro+ram Counter !ith a ne! tar+et address2 a$$o!in+ 0or
pro+ram contro$. 7he 3ro+ram Counter a$!a"s counts up.
BPI#
&pin is the hi+her $e(e$2 interpreted $an+ua+e2 nati(e to the 3rope$$er. A$$ prope$$er
pro+rams contain at $east the bare minimum &3IN pro+ram reLuired to start the 3rope$$er
runnin+2 and hand contro$ o00 to an assemb$" $an+ua+e pro+ram.
In this +uide2 !e !i$$ !orD !ith simp$e assemb$" $an+ua+e pro+rams that operate as a sub;
pro+ram to a main &3IN pro+ram2 0o$$o!ed b" assemb$" $an+ua+e pro+rams that on$" need
&3IN to +et started.
ItFs eas" to !orD !ith assemb$" in sma$$ bits2 then bui$d up as understandin+ +ro!s. It !onFt
be necessar" to 0u$$" understand ho! the &3IN pro+rams presented !orD2 in order to
understand the assemb$" $an+ua+e portions.
7here is an e-ce$$ent introduction to &3IN in the 3rope$$er Manua$.
:hat is a $egister;
<n a $ot o0 C3=Fs there are these memor" stora+e areas de0ined inside the C3= itse$0. I0 the
C3= +oes to do somethin+ !ith an e$ement o0 memor"2 it $oads that into a re+ister2 then
proceeds to operate on it. 5etFs pretend 0or a moment that "ou are the C3= and "ou are
+oin+ to add t!o numbers in "our head. &o2 "ou acLuire both numbers2 then operate on
them2 ho$din+ the sum in "our head a$so.
7his is !hat re+isters do.
No! that sum rea$$" isnFt an" +ood2 un$ess recorded some!here. <0 course it mi+ht a$so be
an intermediate (a$ue as !e$$. I0 "ou !rite the number do!n some!here2 thatFs $iDe a
memor" stora+e operation. I0 "ou Aust Deep the sum in "our head2 perhaps to be added to
somethin+ e$se2 thatFs $iDe Deepin+ the (a$ue in a re+ister.
C3=Fs that ha(e re+isters on$" ha(e so man" o0 them. Additiona$$"2 the" operate on their
re+isters2 !hich reLuires them to 0etch data, better <no!n as operands2 0rom memor"2 as
29
!e$$ as !rite them bacD to memor" !hen operations are comp$ete. 5iDe us2 C3=Fs can on$"
Deep tracD o0 so man" thin+s at an" +i(en time. 7hatFs !h" !e ha(e memor" stora+e.
Memor" is there 0or !hen thin+s +et too comp$e- 0or re+isters a$one.
5on+ a+o2 on the #40)2 I !orDed !ith itFs re+isters. 7he" !ere ca$$ed the Accumu$ator2 E
inde- Re+ister and I inde- re+ister. 7here !ere others too.
(he take a!a% here is that registers are dierentiated largel% )% ho! the% are addressed$
and that the% are part o operations. 1n man% 5:/-s registers are memor% locations that
are not addressed like ;0. or ;1. storage is. (he% ha"e names$ like accumulator$
program counter$ etc...
<n "our t"pica$ C3= then2 !eF(e +ot memor" (RAM or R<M and re+isters as p$aces !here
numbers ma" be copied to2 0rom and operated on. <n C3=Fs !ith !e$$ de0ined re+isters2
their purpose is o0ten sharp$" de0ined as !e$$2 meanin+ one has to !orr" about !hat
re+ister is bein+ used 0or !hat2 and !hen and ho!. Considerab$e time and e00ort is
reLuired to maDe best use o0 the $imited re+isters.
<n the 3rope$$er2 memor" $ocations are the re+isters2 !here operations are concerned: It is
not necessar" to $oad an operand into a re+ister2 operate on it2 then store it a+ain2 as !ith
man" C3=s. 7his a$$ happens !ithin the operation instruction2 !hich !orDs direct$" to and
0rom memor".
(his has lead to some!hat conusing nomenclature as memor% !ill )e reerred to as a
register$ !hen it-s a part o an operation and memor% !hen it-s holding "alues. ,n m%
opinion$ this classiication is not all that correct$ )ut it-s out there$ so ,-m including it
here$ so that %ou ma% understand !hat people are reerring to !hen the% use -register- in
this ashion.
7he 3rope$$er does ha(e internal registers2 in the sense o0 the !ord e-pressed abo(e. 7he
pro+ram counter is one such re+ister. As discussed be0ore2 itFs Aob is to maintain a
re0erence to the ne-t instruction to be e-ecuted. 7here are other interna$ re+isters that
per0orm operations2 shi0tin+2 etc... 7hese are not direct$" addressab$e in most cases
ho!e(er.
Another use o0 the term re+ister re0ers to speci0ic C3= 0unctiona$it" bein+ e-posed as
memor" address space. 7his is ca$$ed a memory mapped register. Instead o0 bein+ Aust
RAM or R<M2 itFs an acti(e memor" $ocation. I0 one !rites (a$ues to2 or reads (a$ues 0rom a
memor" mapped re+ister2 one is communicatin+ to and 0rom other interna$ parts o0 the
3rope$$er2 not Aust maDin+ use o0 stora+e. 7he specia$ C<6 memor" re+isters are a +ood
e-amp$e o0 this beha(ior.
Assemb$" $an+ua+e pro+rams run in C<6s. 7he C<6 memor" is addressed as 41) $on+s.
,ach C<6 $on+ can be an instruction2 memor" stora+e $ocation2 or re+ister 0or use in an
operation. Bhat "ou ca$$ them is more about the conte-t o0 !hat "ou happen to be doin+2
than it is an"thin+ e$se.
30
, reer to the :ropeller as a memor% to memor% design. (his is )ecause it reall% does
operate to and rom memor% instead o rom memor% to a register$ operate$ then rom
register )ack to memor%$ in terms o the instructions !e gi"e it. ,t-s a simpler !a% to go$
and "er% eicient as !ell ,-"e no real idea i that is an oicial term. ,t-s <ust ho! , choose
to dierentiate 5:/s.
Pre)/efined Constants
7hese are $abe$s that point to the addresses o0 thin+s. 7he" !orD a $ot $iDe an instruction
mnemonic does. CN7 Q O1012 0or e-amp$e. Here is a tab$e o0 some constants that identi0"
C?F memory registers. 7hese are presented 0or conte-t on$"2 "ou rea$$" on$" need to
Dno! !hat each constant does 0or "ou2 +oin+ 0or!ard.
?35 $1-0 5)a9-4,$y 7**t ?aram)t)r
C:0 $1-1 5)a9-4,$y Sy!t)m C*u,t)r
C:3 $1-2 5)a9-4,$y C,+ut Stat)! -*r ?31 - ?0
C:7 $1-3 5)a9-4,$y C,+ut Stat)! -*r ?63- ?321
4;03 $1-4 5)a9DEr(t) 4ut+ut Stat)! -*r ?31 - ?0
4;07 $1-5 5)a9DEr(t) 4ut+ut Stat)! -*r ?63 F ?321
2C53 $1-6 5)a9DEr(t) 2(r)'t(*, Stat)! -*r ?31 - ?0
2C57 $1-7 5)a9DEr(t) 2(r)'t(*, Stat)! -*r ?63 - ?321
C053 $1-8 5)a9DEr(t) C*u,t)r 3 C*,tr*$
C057 $1-9 5)a9DEr(t) C*u,t)r 7 C*,tr*$
F5G3 $1-3 5)a9DEr(t) C*u,t)r 3 Fr)Hu),'y
F5G7 $1-7 5)a9DEr(t) C*u,t)r 7 Fr)Hu),'y
?IS3 $1-C 5)a9DEr(t) C*u,t)r 3 ?ha!)
?IS7 $1-2 5)a9DEr(t) C*u,t)r 7 ?ha!)
JCF6 $1-E 5)a9DEr(t) J(9)* C*,-(%urat(*,
JSCB $1-- 5)a9DEr(t) J(9)* S'a$)
31
Boft!are Betup
ItFs time to $oad some so0t!are. 6,AR !i$$ be used 0or some e-p$anations presented here.
<ther e-amp$es !i$$ do rea$ thin+s that can be seen on runnin+ 3rope$$er hard!are.
Propeller *ard!are Betups /ifferG
7his is both a +ood and bad thin+. M$e-ibi$it" is +ood2 and the 3rope$$er rea$$" e-ce$s at
this. It is possib$e to ha(e a 3rope$$er2 up and runnin+2 on Aust a breadboard2 batter" and
sma$$ number o0 discrete components. I happen to be runnin+ a HIDRA ri+ht no!2 meanin+
the code !i$$ be setup 0or a HIDRA s"stem. <ther setups ran+e 0rom comp$e- custom
boards to the 3ara$$a- Re0erence hard!are.
7his does not mean "ou canFt !orD throu+h this stu002 on$" that "ou ma" ha(e to maDe a 0e!
edits to some e-amp$es2 in order to +et them !orDin+ proper$" on !hate(er hard!are "ou
ma" ha(e. Dependin+ on !hat ends up bein+ a part o0 this introduction to Assemb$"
5an+ua+e2 some con(ersion hints ma" be !arranted.
Con(ersions bet!een prope$$er setups $ar+e$" in(o$(e chan+es to pin speci0ications2 c$ocD
speed parameters and other simi$ar thin+s. Core code chan+es norma$$" are not reLuired2
+i(en both setups ha(e the same hard!are components attached. 7he e-amp$es in this
+uide !i$$ sticD to more or $ess standard thin+s.
7he assumption is made that "ou understand some basic e$ectronics. I0 this is not the case2
I hi+h$" recommend obtainin+ an a$read" assemb$ed 3rope$$er s"stem.
F.A$ (The Propeller /ebugger @ .mulator)
Iou ma" or ma" not ha(e a !orDin+ 3rope$$er en(ironment. 6,AR is an e-ce$$ent $earnin+
too$2 that can be used to+ether !ith the 3rope$$er ID, to run man" 3rope$$er pro+rams.
6,AR is a simu$ation that runs on "our N,7 ).0 capab$e 3C. ItFs speed is no!here near that
o0 a rea$ 3rope$$er2 but does o00er memor" (ie!s2 79 and 96A screen emu$ation2 etc...
BorDin+ en(ironment or no2 I hi+h$" recommend do!n$oadin+ this too$. &etup is rea$$" eas".
Uust unpacD 6,AR into a director" o0 "our choosin+2 and run the e-ecutab$e 0rom there. I
$iDe to put 6,AR into one o0 m" 3ara$$a- 7oo$ sub 0o$ders and !orD that !a".
6,AR can be 0ound at/
http/88so0t.Aa(a;(irtua$;machine.net8(irtua$;machines8+ear;para$$a-;prope$$er;debu++er.htm$
Iou !ant the e-ecutab$e pacDa+e2 un$ess "ou p$an on modi0ications to 6,AR itse$0/
6ear;1.11.0.0.zip
6,AR output !i$$ be used in portions o0 this +uide2 and !i$$ continue to ser(e as a debu+
32
en(ironment +oin+ 0or!ard as !e$$.
Build your Betup
It is a +ood idea to (eri0" "our hard!are setup is !orDin+ proper$". 7roub$e shootin+ this is
be"ond the scope o0 this +uide. I re0er "ou to the 0riend$" and he$p0u$ and acti(e +roup o0
3rope$$er users and enthusiasts 0ound at http/880orums.para$$a-.com Re+ister 0or an
account2 asD some Luestions2 and +et "our setup runnin+. Idea$$"2 maDe a 0e! 0riends a$on+
the !a". &hou$d "ou +et up and runnin+2 consider returnin+ the 0a(or in the 0uture.
Here is a LuicD so0t!are setup sanit" checD2 usin+ some o0 the e-amp$e code that comes
!ith the 3rope$$er ID,.
1. /o!nload the Installer pac<ages 0or both the 3rope$$er ID, and 6,AR. 7he 3rope$$er
ID, comes !ith the 3rope$$er Manua$ and it contains an e-ce$$ent tutoria$. 6,AR has
no such re0erence document2 so IF$$ +o throu+h some basics here2 as !arranted.
7he 3rope$$er ID, can be 0ound at/
http/88!!!.para$$a-.com8prope$$er8do!n$oads.asp
). Install the Propeller Tool2 taDin+ the de0au$ts. 7he on$" reason 0or not doin+ this2
!ou$d be $acD o0 disD space on "our s"stem disD (norma$$" C/2 or a pre0erence 0or
!here pro+rams are stored. 7his +uide !i$$ assume the de0au$ts2 so trans$ate
accordin+$"2 shou$d "ou maDe other choices:
3rope$$er 7oo$ De0au$t In0ormation/
7he main2 or root2 pro+ram director" is/
C/X3ro+ram Mi$esX3ara$$a- IncX3rope$$er 7oo$ (1.04.4
I $iDe to p$ace ne! proAect 0o$ders under here in the F,-amp$esF director".
...X,-amp$esXIour 3roAect Mo$der
...X,-amp$esX6ear
.. Create the F.A$ folder in the ,-amp$es director" as sho!n abo(e
4. ?pen the compressed F.A$ file. 7herein2 "ou !i$$ 0ind a re$ease 0o$der. Copy @
e+tract the contents of this folder into the F.A$ folder created in step . abo(e.
3" $un the Propeller Tool
It shou$d come up !ith a b$anD pro+ram !orD area tab acti(e2 F=ntit$ed1F that
occupies the ri+ht hand portion o0 the 3rope$$er 7oo$ !indo!.
33
#. $un F.A$ b" doub$e c$icDin+ on Fear"e+e
(7his taDes a moment to start
Iou shou$d see an empt" !indo! tit$ed2 J6,AR/ 3ara$$a- 3rope$$er ,mu$atorK
Aerify your Betup
&" Btart the Propeller Tool
). =sing the file tree on the left, naigate to the ...Xe-amp$esX5ibrar" director" and
doub$e c$icD on AFAH/emo"spin pro+ram 0i$e "ou 0ind there.
Iou should see the program load into the main !indo!"
5" *it the -1 <ey
7his !i$$ compi$e the pro+ram and present "ou !ith a stats dia$o+.

4. <n that dia$o+2 select the 'sho! he+' button to arri(e at the screen sho!n be$o!/
4. Belect 'Bae ..P$?> -ile' and na(i+ate to !here you put the F.A$ e+ecutable
files and hit the sa(e button2 modi0" the 0i$e name i0 "ou !ant2 then comp$ete the
sa(e.
," $un F.A$
7. -rom the upper most program menu, choose file, open and se$ect the 0i$e "ou Aust
sa(ed.
34
7he 0i$e shou$d open inside the 6,AR !indo!2 sho!in+ "ou some code in the main
!indo!2 the picture o0 the H=* and some menu and tab options.
%. -rom the program !indo! menu, not the main F.A$ menus or buttons2 select
6?pen Plugin8 and $oad gamonitor"+ml
9. A ne! tab !ill appear, titled 6AFA >onitor8 Belect it, then hit 6$un8 0rom the
pro+ram !indo!.
6raphics shou$d appear2 e(entua$$" renderin+ a screen that $ooDs $iDe the one sho!n
be$o!/
A$$ o0 these thin+s can happen !ith or !ithout an actua$ 3rope$$er chip bein+ connected to
"our computer2 $ea(in+ "ou 0ree to accomp$ish that ho!e(er maDes best sense 0or "ou.
I0 "ou ha(e a !orDin+ setup2 0ee$ 0ree to use it: 7his in0ormation is pro(ided in the spirit o0
speaDin+ to the (er" be+inner.
35
<nce "ou +et the 3rope$$er 7oo$ and 6,AR up and runnin+2 0ee$ 0ree to run some o0 the
demos pro(ided. Iou can start2 stop and step the prope$$er2 e-amine memor"2 see states o0
$ocDs2 !here H=* access is current$" at2 etc... &pend a $itt$e time !ith the 6,AR too$ to
understand !hat it does.
7he ,,3R<M 0i$e is a binar" that eLua$s !hat !ou$d be in the 3rope$$er RAM2 had it been
$oaded b" the *oot 5oader. 6,AR simu$ates the 3rope$$er (er" !e$$ 0rom there. 6,AR has a
0e! bu+s2 none o0 !hich shou$d si+ni0icant$" impact the materia$ presented here. Man"
ha(e used 6,AR to simu$ate comp$e- 3rope$$er pro+rams. <ne shortcomin+2 in the current
(ersion o0 6,AR2 is the inabi$it" to set breaDpoints. Bhen steppin+ throu+h instructions2
there is a $ot o0 steps to +o throu+h !hen a ne! C<6 starts. At times2 it ma" be necessar"
to Aust run somethin+ in 6,AR2 then $ooD at the end resu$t.
6ear acts $iDe a runnin+ 3rope$$er in so0t!are. ItFs speed is $ess than 1810 that o0 a rea$
3rope$$er2 runnin+ at %0Mhz.
7he s$o! bui$d o0 the +raphics is actua$$" a $ooD at !hat the monitor does2 $ine b" $ine. 7his
is !h" "ou !onFt see a 0u$$ ima+e at 0irst. It taDes some time 0or a 0e! 0rames to dra!2 thus
comp$etin+ the ima+e.
<n m" s"stem2 this taDes a Luarter o0 a minute.
36
/issecting some Assembly Language Code
7his rea$$" is a mi-ture o0 &3IN code and Assemb$" $an+ua+e code. 7he &3IN discussion !i$$
be Dept $i+ht2 but is necessar" as a$$ assemb$" $an+ua+e pro+rams !i$$ ha(e a sma$$ &3IN
component. 7his e-amp$e has the minimum amount o0 &3IN $an+ua+e reLuired to +et an
assemb$" $an+ua+e pro+ram up and runnin+.
7he e-amp$e code2 sho!n be$o!2 to++$es a speci0ic pin on and o002 end$ess$"2 !ith a speci0ic
amount o0 time bet!een to++$es. 7he parameters +i(en be$o!2 assume the reader !i$$ be
usin+ 6,AR to !atch the output. I0 rea$ hard!are is bein+ used2 $on+er de$a" times mi+ht
maDe better sense. 7r" #W000W000 0or a de$a"2 i0 "ou are usin+ a b$inDin+ 5,D2 0or e-amp$e.
7he pa+e breaD here is Aust 0or the samp$e pro+ram to appear in one2 unbroDen se+ment 0or
c$arit".
37
And no!, the codeD (finallyG)
KK 3!!)m/$y0*%%$)#!+(, LL
Fr*m ?a%) 340 *- th) ?r*+)$$)r 8a,ua$
E(th !*m) !ma$$ )9(t!< -*r th(! +ur+*!)M
*+),%))NO%ma($#'*m
C4:
&'$Nm*9) = xta$1 + +$$16x
&x(,-r)H = 5&000&000

?;7 8a(,
KBau,'h '*% t* t*%%$) ?16 ),9$)!!$yL
'*%,)1(O0*%%$)< 0) Bau,'h ,)1 '*%

230
K0*%%$) ?16L
456 0 7)%(, at C*% 538 a99r 0
0*%%$) m*> 9(ra< ?(, S)t ?(, t* *ut+ut
m*> 0(m)< ',t Ca$'u$at) 9)$ay t(m)
a99 0(m)< =$- S)t (,(t(a$ 9)$ay h)r)
P$**+ 1a(t',t 0(m)< 2)$ay Ea(t
x*r *uta< ?(, 0*%%$) ?(,
Qm+ =P$**+ B**+ ),9$)!!$y

?(, $*,% "< 16 ?(, ,um/)r
2)$ay $*,% 600 C$*'N 'y'$)! t* 9)$ay
0(m) r)! 1 Sy!t)m C*u,t)r E*rN!+a')
Fetting the code into the Propeller Tool
*e0ore an"thin+ e$se is discussed2 itFs a +ood idea to maDe sure this code is runnin+ proper$".
7o do this2 "ou can t"pe it into the 3rope$$er 7oo$. I hi+h$" recommend doin+ this2 as "ou
!i$$ +et 0ami$iar !ith both the 3rope$$er 7oo$ and 0ormattin+ at the same time. ItFs a$so
+ood 0or e-ercisin+ "our memor".
Most adu$ts $earn best !ith a mu$ti;sensor" approach. 7his means read it2 sa" it2 !rite it2
t"pe it2 !atch it. 7hatFs o$d schoo$2 but I Dno! it !orDs. M" da" Aob in(o$(es adu$t $earnin+
on a re+u$ar basis.
7he other a$ternati(e is to cut and paste the code abo(e into the prope$$er too$.
38
,ither !a" is 0ine2 Aust +et it in there. Iou can checD "our !orD2 i0 t"pin+2 b" usin+ the M%
De". 7his !i$$ cause the 3rope$$er 7oo$ to e(a$uate the code2 and tr" and bui$d a !orDin+
binar" ima+e o0 it.
I0 "ou see a screen $iDe the one sho!n at
ri+ht2 e(er"thin+ is $ooDin+ +ood 0or the
6,AR simu$ation to 0o$$o!.
I0 not2 re(ie! !hat "ou ha(e input2 and tr"
a+ain.
7his in0o screen te$$s "ou the size o0 "our
pro+ram2 re$ati(e to the 3rope$$er resources.
<n$" 1) $on+s are used 0or this pro+ram:
7hatFs a mere 4% b"tes: Assemb$" $an+ua+e
pro+rams t"pica$$" are (er" sma$$ compared
to pro+rams !ritten in other 0orms.
Remember to hit the J&ho!WHe-K button
and sa(e "our ,,3R<M 0i$e 0or use in 6,AR.
I0 "ou are runnin+ rea$ hard!are2 "ou can
edit the pin number2 maDe sure "our 5,D2 or
other de(ice is connected to that pin2 and
hit M10 to run the pro+ram direct$" 0rom the
3rope$$er RAM.
Fear Bimulation
It is necessar" to maDe sure this pro+ram is runnin+ proper$" be0ore an" 0urther discussions2
chan+es2 additions happen.
A+ain2 hit M% to arri(e at the In0o screen. 7his !i$$ catch an" +ross errors in 0ormattin+ and
s"nta-. &e$ect the J&ho! He-K button2 0o$$o!ed b" the J&a(e ,,3R<M Mi$eK button to sa(e
the memor" ima+e 6,AR needs. ItFs not a bad idea to Aust do a$$ o0 this in the 6,AR 0o$der
to a(oid ha(in+ to re;se$ect directories.
Run 6ear and open the ,,3R<M ima+e "ou Aust created.
&e$ect JRunK2 0rom the !indo! that appears2 then se$ect the $o+ic (ie! tab to see the pin
to++$e in the simu$ation. Iour screen shou$d $ooD somethin+ $iDe the one be$o!/
39

As sho!n pin 1# is co$ored red2 0or output. At the $e0t2 "ou can a$so see the pin state
re+isters and the state o0 pin 1# there too.
Hit the stop button2 to 0reeze the output2 run to continue and step instruction to +o
throu+h it one instruction at a time. 7here is a$so step c$ocD. Comparin+ these t!o is
use0u$. &tep instruction directs 6,AR to Aust simu$ate !hat it taDes to +et to the ne-t
instruction state. &tep c$ocD means Aust ad(ance the simu$ation one c$ocD at a time.
&teppin+ throu+h this !ith the c$ocD is a $ot o0 c$icDs: Be ha(e a de$a" o0 #00 c$ocDs per pin
to++$e. 7hatFs actua$$" not (er" $on+ at %0Mhz2 but seems (er" $on+2 one c$ocD at a time.
Another so$ution 0or this di$emma is a ca$$ed a breaDpoint. ItFs a pre;de0ined p$ace in the
pro+ram !here it can stop runnin+2 !hi$e bein+ e-amined. 6,AR does not ha(e this 0aci$it"2
!hich !i$$ $imit some o0 the discussion e-amp$es.
Program Bections
7he 3rope$$er ID, hi+h$i+hts the di00erent pro+ram sections in co$or. ,ach section has a
speci0ic purpose2 detai$ed be$o!/
40
0= ,D4 stands or >,ntegrated De"elopment 4n"ironment? (his is a short !a% o reerring
to the sot!are tool$ designed to !ork !ith the :ropeller.
-ormatting
7he 0irst thin+ !e see is $ots o0 0ormattin+ e$ements. 7ruth is2 the 3rope$$er ID, and the
computer itse$0 does not care about an" o0 this stu00. ItFs 0or us humans2 !ho need $ots o0
conte-t2 i0 an"thin+ is to maDe an" rea$ sense.
It is a rea$$" +ood idea to +et in the habit o0 0ormattin+ "our code "ou !rite. Mi+ht seem
$iDe an e-tra hass$e ri+ht no!2 but soon it !i$$ become habit and "ou !i$$ thanD "ourse$0
!hen +oin+ bacD to $ooD at code "ou !rote "ears a+o. <thers !i$$ thanD "ou too.
IF(e co$ored the 0irst e-amp$e in a 0ashion simi$ar to !hat the 3rope$$er 7oo$ does. 6oin+
0or!ard2 p$ain te-t !i$$ be used.
(unlabeled) Pre)program comment area
&ome peop$e re0er to this as the Program *eader" I0 "ou ha(e a particu$ar pro+ram $icense
in mind2 !ant to e-press credit 0or other contributors code2 or ma"be Aust !ant to e-p$ain
!hat the pro+ram does2 itFs inputs2 outputs2 etc... this is !here "ou do it. Comments can
taDe man" 0orms. A 0e! are detai$ed here in this section.
KK 3!!)m/$y0*%%$)#!+(, LL
Fr*m ?a%) 340 *- th) ?r*+)$$)r 8a,ua$
E(th !*m) !ma$$ )9(t!< -*r th(! +ur+*!)M
*+),%))NO%ma($#'*m
Comments
Bingle Juote comments 6 ' 8
7his one is LuicD and eas". ,ssentia$$"2 an"thin+ to the ri+ht o0 the sin+$e Luote character
6'8 is then considered a comment.
Comments are e-p$anator" in nature. 7he" !i$$ not be considered as part o0 the pro+ram at
a$$. Comments are used b" pro+rammers to communicate intent2 so that it is not 0or+otten
o(er time2 or must be re;parsed.
Comments ma" appear as part o0 pro+ram $ines. 5ooD at the DA7 section be$o! this one2
0or some e-amp$es o0 that.
Brac<et comments 6 K L 6
41
5ar+e comments ma" be de0ined !ith the open bracDet JYJ and c$ose bracDet JZK
characters thus/
K
C- a '*mm),t (! !u/!ta,t(a$< +)rha+! '*,ta(,! a ta/$)<
)t'### th) /ra'N)t m)th*9 (! a -ar /)tt)r *+t(*,# I)r)
(! a, )xam+$) ta/$)P
+aram)t)r&arrayR0S F 399r)!! *- !*m)th(,%
+aram)t)r&arrayR2S F 399r)!! *- !*m)th(,% )$!)
F(,a$$y< 1) ),9 th) $ar%) '*mm),t 1(th a '$*!) /ra'N)tM
L
*racDet comments are a$so a LuicD !a" to e-c$ude se+ments o0 code 0rom the pro+ram.
C?# (Constants)
C4:
&'$Nm*9) = xta$1 + +$$16x
&x(,-r)H = 5&000&000
Iou can de0ine constants here. A constant is a (a$ue assi+ned to a te-t $abe$. ,+/ 3I Q
..14149 7his is done to maDe $i0e eas". Instead o0 repeatin+ the (a$ue e(er" !here it is
needed2 one Aust repeats the $abe$. Additiona$$"2 chan+in+ the $abe$ de0inition2 then maDes
that chan+e +$oba$ Gmeanin+ e(er"!here that $abe$ appears. 7his is po!er0u$ in that
maDin+ $ots o0 manua$ chan+es +ets considerab$" easier !hen constants are used.
Constants do not consume pro+ram space in and o0 themse$(es. 7he" are 0or "ou to
communicate meanin+ to the 3rope$$er 7oo$2 or other assemb$er pro+ram.
Constants do taDe pro+ram space !hen speci0ied in the main pro+ram. 7he" taDe the same
amount o0 space Aust puttin+ the (a$ue there !ou$d. 7here are three pro+ram $ines in this
section.
Constants are operated on b" the 3rope$$er too$. 7he" are not run time pro+ram e$ements.
Bhi$e it is possib$e to per0orm math operations !ith constants2 the end resu$t is a static
(a$ue to be used b" the runnin+ pro+ram. Bhen used this !a"2 the idea is to maDe pre;
ca$cu$atin+ some (a$ues needed b" the pro+ram a(ai$ab$e to it as resu$ts on$".
7hese constants de0ine the c$ocD mode2 source and 0reLuenc" the s"stem c$ocD is deri(ed
0rom. More detai$ on this can be 0ound in the 3rope$$er Manua$2 startin+ on 3a+e 1%0.
P=B (Public)
42
?;7 8a(,
KBau,'h '*% t* t*%%$) ?16 ),9$)!!$yL
'*%,)1(O0*%%$)< 0) Bau,'h ,)1 '*%
&3IN pro+rams are broDen do!n into sma$$ components2 ca$$ed procedures. An" such
procedures in the 3=* area2 are e-posed to other &3IN pro+rams. (7here is a$so a 3RI2
meanin+ pri(ate2 area not sho!n in this e-amp$e. 7hose procedures !ou$d not be shared.
In this case2 the procedure name is >ain2 and is the on$" procedure. ,(en thou+h it is
sma$$2 there is a $ot to understand here.
3rocedure Main on$" has one &3IN instruction: 7hatFs the cogne! command2 sho!n in bo$d
abo(e. 7his $ine te$$s the 3rope$$er to $oad and start runnin+ another C<6 !ith the
assemb$" $an+ua+e pro+ram2 $ocated in H=* memor" b" the $abe$ 7o++$e. 7he F[K operator
means Jpointer toK2 and !i$$ be discussed in more detai$ short$". Mor no!2 J[7o++$eK
means the $ocation in H=* memor" !here the assemb$" pro+ram be+ins.
Co+ne! has t!o ar+uments bein+ supp$ied here. <ne is the H=* memor" $ocation o0 the
pro+ram to be $oaded into the C<6. 7he other is a sin+$e (a$ue2 to be passed to the
pro+ram runnin+ inside the C<6. 7hatFs the J2 0K portion. 7his particu$ar pro+ram does not
reLuire a passed (a$ue2 so an"thin+ actua$$" !orDs here2 but a 0 is t"pica$$" used !hen
nothin+ needs to be passed to the C<6.
It is !orth notin+ that C<6 0 is actua$$" runnin+ the &3IN procedure main. Another C<6 !i$$
end up runnin+ the assemb$" $an+ua+e pro+ram2 to be started b" C<6 0. 7his is a
si+ni0icant point to understand:
Bo, ho! does this all flo! together any!ay;
Bhen the 3rope$$er 7oo$ sees this pro+ram e-amp$e2 it parses the pro+ram (!ith parse
meanin+/ J7o breaD do!n to core e$ements o0 meanin+K to bui$d up a$$ the binar"
instruction code2 interpreted &3IN code2 data and other pro+ram e$ements2 such as memor"
arra"s2 initia$ized (a$ues2 etc...
It then bui$ds up a H=* memor" ima+e containin+ a$$ o0 these thin+s2 0it to+ether in an
or+anized !a". 7hat H=* memor" ima+e contains a$$ o0 the e-ecutab$e pro+ram code in
this e-amp$e. 7hatFs the 4% b"tes seen on the M% screen abo(e.
Bhen the 3rope$$er starts2 it $ooDs 0or a pro+ram ima+e to 0i$$ itFs RAM !ith. 7hatFs actua$$"
identica$ to the ,,3R<M ima+es !eF(e created to run !ith 6,AR. Ha(in+ 0i$$ed up the RAM2
it then $oads the &3IN interpreter2 0rom itFs interna$ R<M2 into C<6 02 !hich then +ets
started runnin+ the &3IN pro+ram it 0inds in H=* memor". 7hatFs ho! Main is e(entua$$"
e-ecuted.
7he &3IN interpreter sees the co+ne! instruction2 in the procedure Main2 and a$so Dno!s
43
the address associated !ith the $abe$ J7o++$eK. It taDes that address and directs the
3rope$$er to start up a ne! C<62 in this case C<6 1.
At this point2 the assemb$" $an+ua+e pro+ram +ets copied 0rom the H=*2 to C<6 1. Rea$$"2
there are t!o copies o0 the assemb$" $an+ua+e pro+ram. <ne is the H=*2 and is necessar"
to +et a C<6 up and runnin+. 7he other is actua$$" in the C<62 runnin+:
516-s need to ill their program space !ith !hate"er it is the% are going to
)e doing$ )eore the% actuall% run an%thing. (hat-s 2@) o ;0.$ A*2 9ongs$
per 516. (his happens e"en i the actual assem)l% language is "er% small.
At this point2 C<6 0 is essentia$$" done. 7he on$" tasD it !as +i(en !as to $oad up C<6 12
and thatFs it.
7he assemb$" pro+ram then e-ists in C<6 12 and be+ins to run. 7hatFs the ne-t pro+ram
section:
/AT (/ata)
7his is !here the assemb$" pro+ram $i(es. 5ots o0 thin+s can +o into this section2 but IFm
+oin+ to 0ocus on the assemb$" $an+ua+e pro+ram 0or no!.
230
7he 0irst $ine2 the DA7 $ine2 de0ines the start o0 a DA7 section. =n$iDe the Hi+h$ander2 there
can be more than one: (7hat is to sa"2 "ou ma" ha(e more than one instance o0 the
pro+ram section t"pes.
K0*%%$) ?16L
7his is a comment $ine2 detai$in+ the idea that !e are to++$in+ pin number 1#.
456 0 7)%(, at C*% 538 a99r 0
<R6 0 essentia$$" means2 J&tart o0 C<6 Memor"K2 !hich is $ocation 0. 7his he$ps the
3rope$$er 7oo$ see !e are te$$in+ it about an assemb$" $an+ua+e pro+ram and not some other
data. 7he 3rope$$er too$ then can $a" out this pro+ram in terms o0 H=* memor" 0or $oadin+
it into the 3rope$$er2 !hi$e a$so encodin+ the instructions 0or e-ecution in the C<6 memor"2
!hen that time comes.
I0 there !as another assemb$" $an+ua+e pro+ram to be run in another C<62 another <R6 0
statement and another $abe$2 $iDe J7o++$eK !ou$d be a$$ that is needed to set that pro+ram
up.
7o++$e then has t!o meanin+s:
,n the &/B$ it-s some memor% location in the &/B memor% space B0000 to
44
B7$ such as B0024. ((hat-s not the real location$ <ust an e'ample o lo!
&/B memor%.) (hat meaning is necessar% to insure the 516 is loaded !ith
the right program data residing in the &/B.
,n the 516$ it-s actuall% address 0$ or the )eginning o 516 memor%.
;emem)er$ all 516 memor% is addressed as longs. Cor assem)l% language
this makes things reall% eas%.
7he 0irst instruction2 in this case2 is $ocation 0. 7he second one !i$$ be $ocation 12 and so
on... >sho!n be$o!?
0*%%$) R0S m*> 9(ra< ?(, S)t ?(, t* *ut+ut
R1S m*> 0(m)< ',t Ca$'u$at) 9)$ay t(m)
R2S a99 0(m)< =$- S)t m(,(mum 9)$ay h)r)
P$**+ R3S 1a(t',t 0(m)< 2)$ay Ea(t
R4S x*r *uta< ?(, 0*%%$) ?(,
R5S Qm+ =P$**+ B**+ ),9$)!!$y

?(, R6S $*,% "< 16 ?(, ,um/)r
2)$ay R7S $*,% 600 C$*'N 'y'$)! t* 9)$ay
0(m) R8S r)! 1 Sy!t)m C*u,t)r E*rN!+a')
A t!o C?F e+ample
5etFs sa" !e !anted to to++$e t!o pins2 instead o0 Aust one. 7here are % C<6s in the
3rope$$er2 !h" not Aust 0ire up another C<6C Ri+ht no!2 !e Dno! a$most enou+h to do Aust
that.
HereFs the e-amp$e code 0or t!o C<6Fs. 6et it into the 3rope$$er 7oo$2 and simu$ate it in
6,AR. 7he di00erent pro+ram e$ements are sho!n in bo$d.
C4:
&'$Nm*9) = xta$1 + +$$16x
&x(,-r)H = 5&000&000

?;7 8a(,
KBau,'h '*% t* t*%%$) ?16 ),9$)!!$yL
'*%,)1(O0*%%$)< 0) Bau,'h ,)1 '*%
'*%,)1(O0*%%$)1< 0) Bau,'h a,*th)r ,)1 '*%

230
K0*%%$) ?16L
45
456 0 7)%(, at C*% 538 a99r 0
0*%%$) m*> 9(ra< ?(, S)t ?(, t* *ut+ut
m*> 0(m)< ',t Ca$'u$at) 9)$ay t(m)
a99 0(m)< =$- S)t m(,(mum 9)$ay h)r)
P$**+ 1a(t',t 0(m)< 2)$ay Ea(t
x*r *uta< ?(, 0*%%$) ?(,
Qm+ =P$**+ B**+ ),9$)!!$y

?(, $*,% "< 16 ?(, ,um/)r
2)$ay $*,% 600 C$*'N 'y'$)! t* 9)$ay
0(m) r)! 1 Sy!t)m C*u,t)r E*rN!+a')
K0*%%$) ?17 ---------------------------------------------------L
456 0 7)%(, at C*% 538 a99r 0
0*%%$)1 m*> 9(ra< ?(, S)t ?(, t* *ut+ut
m*> 0(m)< ',t Ca$'u$at) 9)$ay t(m)
a99 0(m)< =$- S)t m(,(mum 9)$ay h)r)
P$**+ 1a(t',t 0(m)1< 2)$ay1 Ea(t
x*r *uta< ?(,1 0*%%$) ?(,
Qm+ =P$**+ B**+ ),9$)!!$y

?(,1 $*,% "< 17 ?(, ,um/)r
2)$ay1 $*,% 400 C$*'N 'y'$)! t* 9)$ay
0(m)1 r)! 1 Sy!t)m C*u,t)r E*rN!+a')
7here are t!o !a"s to do this. 7he better !a" is to !rite a smart enou+h assemb$"
$an+ua+e pro+ram2 so that one cop" o0 it can be in the H=* memor". 7his is done !ith
passed parameters (ar+uments2 pointers...2 such that each time it is started up on a C<62
it Dno!s to $ooD 0or some in0ormation that di00erentiates !hat it is doin+ 0rom !hat itFs
other instances are doin+ on their C<6s.
7hen there is the eas" !a"2 sho!n here. Rea$$" a$$ that happened !as a cut Fn paste o0 the
ori+ina$ assemb$" $an+ua+e pro+ram. Mrom there2 ne! $abe$s !ere created b" addin+ a 1
onto a$$ o0 the e-istin+ ones2 0ina$$" some comments !ere chan+ed2 a$on+ !ith the pin and
de$a" (a$ues.
*acD up in Main2 another co+ne! command starts up C<6 ) !ith the second assemb$"
$an+ua+e pro+ram.
The only real ne! piece of information is that labels must be uniJueG 7hatFs !h" the
ones !ere added onto the ends o0 the $abe$s. 7he 3rope$$er 7oo$ must $ooD throu+h a$$ the
code2 parse it and or+anize it. I0 it sees t!o o0 a $abe$2 it has no !a" to di00erentiate them
$iDe !e do.
46
7here are additiona$ ru$es 0or $abe$s2 !e$$ co(ered in the 3rope$$er Manua$.
Hope0u$$"2 this e-amp$e rein0orces H=* memor" and C<6 memor"2 $abe$s2 and such...
Mrom here2 itFs onto parsin+ the assemb$" code.
Assembly Program /etail
IF(e reproduced the assemb$" pro+ram here a+ain 0or c$arit". Mor no!2 itFs bacD to the sin+$e
C<6 e-amp$e. 7he instruction addresses are in bracDets. <ne thin+ to remember is that
each instruction is a $on+2 so is a re+ister2 or memor" stora+e $ocation. ItFs a$$ $on+s2
addressed one $on+ at a time. ;eall%$ an% gi"en 516 memor% address is !hat %ou sa% it is.
0s long as %ou are clear and consistent !ith %our purpose$ or each location$ %ou !ill not
ha"e pro)lems !ith this.
)Rb"tes o0 C<6 memor" then breaDs do!n into 41) G 1# specia$ purpose re+isters2 o0
addressab$e C<6 memor". (O000 ; O1,M
7his section !i$$ 0ocus on the instructions used. Man" o0 the instructions ha(e a $ot o0
(ariations that !i$$ be i+nored2 in the hopes that Aust detai$in+ ho! the" are used in this
particu$ar pro+ram ends up bein+ he$p0u$. Ha(in+ $earned about speci0ic uses2 one can then
more easi$" e-p$ore other options: It is not a bad idea to cracD open the 3rope$$er Manua$
and read about the di00erent instruction options2 once "ou ha(e their purpose2 presented
here2 c$ear in "our mind.
/iscussion
Program
7he 0irst # C<6 memor" $ocations ho$d the pro+ram. In the conte-t o0 the2 JBhat is a
re+isterCK discussion ear$ier2 these memor" $ocations are Aust stora+e ri+ht no!. 7he" ho$d
pro+ram instructions2 to be di00erentiated 0rom the data consumed 8 manipu$ated b" the
pro+ram. 7he" are not rea$$" Fre+istersF in the most o0ten used sense o0 the !ord.
7hatFs rea$$" the on$" di00erentiator bet!een so0t!are (pro+rams and data. It can +et
some!hat bizarre !ith pro+rams consumin+ other pro+rams2 or a pro+ram chan+in+ itse$0.
Ho!e(er2 i0 "ou are there asDin+ those Luestions2 "ouF(e pro+ressed past needin+ this +uide:
Con+ratu$ations.
7he point to remember here is that an" C<6 memor" $ocation can p$a" an" o0 these ro$es.
Ho! a memor" stora+e $ocation is used rea$$" is up to the pro+rammer2 !ith the rest Aust
bein+ semantics.
/ata
47
7he $ast three then are data Fre+istersF2 in that the" ho$d (a$ues to be used b" the pro+ram2
and one o0 them >%?2 de0ines a !orDspace used b" the pro+ram.
7here are t!o di00erentiators here/
Initiali9ed data
7hese are (a$ues that are $oaded !ith the pro+ram. 7hinD o0 them as +i(ens2 0rom !hich a
+reater so$ution or tasD ma" be comp$eted.
=ninitiali9ed
,ssentia$$"2 !orDin+ memor" $ocations. 7he pro+ram !i$$ use these 0or in;process stora+e.
BorDin+ data2 in other !ords. 7heir startin+ (a$ue is not important.
A+ain2 these are not hard and 0ast ru$es. Mor e-amp$e2 one cou$d de0ine a memor" $ocation
!ith some starter data2 then decide to use it 0or temporar" stora+e $ater2 once the need 0or
the initia$ data has passed. A+ain2 semantics. 7he De" to this stu00 is to 0o$$o! the intent
o0 the pro+ram. Mrom there2 these di00erences !i$$ !ash out2 $ea(in+ "ou Dno!in+ !hat is
actua$$" happenin+ in the memor". Mrom there2 ca$$ it !hat "ou !ant to ca$$ it:
0*%%$) R0S m*> 9(ra< ?(, S)t ?(, t* *ut+ut
R1S m*> 0(m)< ',t Ca$'u$at) 9)$ay t(m)
R2S a99 0(m)< =$- S)t m(,(mum 9)$ay h)r)
P$**+ R3S 1a(t',t 0(m)< 2)$ay Ea(t
R4S x*r *uta< ?(, 0*%%$) ?(,
R5S Qm+ =P$**+ B**+ ),9$)!!$y

?(, R6S $*,% "< 16 ?(, ,um/)r
2)$ay R7S $*,% 600 C$*'N 'y'$)! t* 9)$ay
0(m) R8S r)! 1 Sy!t)m C*u,t)r E*rN!+a')
Instruction M(N mo Mdestination C?F addressN, Msource C?F addressN
0*%%$) R0S m*> 9(ra< ?(,
7he mo instruction copies the contents o0 one C<6 memor" $ocation to another one. 7he
end resu$t o0 a mo( !i$$ be t!o memor" $ocations ha(in+ the same content. 7hatFs it:
3rett" eas" o(era$$. ItFs oD to thinD o0 mo( as actua$$" bein+ cop"2 because that is e-act$"
!hat it does.
&o2 !hich $ocations are bein+ mo(ed and !h"C
48
7hatFs !hat the $abe$s are 0or. In this case2 !eF(e +ot the FPinF $abe$ re0errin+ to C<6
memor" $ocation number #. 7hat memor" $ocation is initia$ized !ith the (a$ue necessar" to
identi0" pin 1# 0or some operation. (7hat !i$$ +et detai$ed under that instruction header
A$$ !e need to Dno!2 at this sta+e2 is that !hate(er is contained in memor" $ocation >#? !i$$
do the Aob o0 identi0"in+ pin 1#.
7he pre;de0ined $abe$ dira2 re0ers to one o0 the specia$ C<6 memor" re+isters. 7hese are
acti(e memor" $ocations2 !hose contents impact the beha(ior o0 the 3rope$$er itse$0. 7he
dira re+ister de0ines the input 8 output direction 0or the 0irst .) i8o pins. In simp$e terms2
each pin needs a direction. Bhen more than one C<6 is !orDin+ !ith a pin2 it +ets more
comp$e- than that ho!e(er.
(&ee the 3rope$$er Manua$2 pa+e ). 0or more on the matter o0 pin states.
In this e-amp$e2 !eF(e +ot one C<62 !orDin+ !ith one 3IN2 so a zero means that pin is an
input pin to be read b" runnin+ pro+rams. A one means that pin is an output pin2 !hose
state is chan+ed b" runnin+ pro+rams.
7o to++$e the pin2 !e need to chan+e itFs state2 so itFs +otta be set to output.
(hat is e'actl% !hat this irst mo" instruction does. ,t sets the direction o pin num)ered
*D to )e in the output direction.
Instruction M&N mo Mdestination C?F addressN, Msource C?F addressN
R1S m*> 0(m)< ',t
Another mo( instruction2 !ith the same 0orm as the 0irst one. Be Dno! somethin+ is
+ettin+ copied2 but !hatC A+ain2 the $abe$s te$$ the stor" here.
3rede0ined $abe$ cnt re0ers to the acti(e C<6 memor" re+ister that re0ers to the +$oba$
s"stem counter2 shared b" a$$ the C<6s. 6$oba$ means $itera$$"2 Je(er"!hereK2 and shared
means Jreadab$e b" e(er"thin+K.
7he $abe$ 7ime2 is the !orDin+ re+ister. ItFs purpose is to pro(ide a p$ace 0or computations2
intermediate resu$ts2 etc... Bhate(er the counter (a$ue is2 at the time this instruction is
e-ecuted2 !i$$ be copied into the C<6 memor" $ocation the $abe$ 7ime re0ers to.
(his instruction then notes the "alue o the s%stem counter$ or use later.
Instruction M%N add M destination operand C?F addressN, MOimmediate source alue
operand to be addedN
R2S a99 0(m)< =$-
49
Here !eF(e +ot both a ne! instruction2 and a ne! addressin+ 0orm: (7hatFs the pound si+n:
J @ J 7he ne! instruction is an add instruction. It !orDs $iDe this/
7he unsi+ned sum o0 both operands !i$$ end up in the C<6 memor" re+ister speci0ied as the
destination.
7he source (a$ue is interestin+ because it is contained in the instruction itse$02 not a C<6
memor" $ocation: 7his deser(es some e-p$anation2 be0ore !e mo(e on.
Time P Time Q 7f
Immediate addressing
An immediate address is essentia$$" one !here the instruction itse$0 has the operand2 or
data to be operated on2 !ithin it2 and not contained some!here e$se in the C<6 memor".
3rope$$er instructions ha(e t!o 9 bit 0ie$ds2 ca$$ed the destination and source 0ie$ds.
Norma$$"2 these re0er to an" o0 the 41) $on+s in the C<6.
As an option2 the pro+rammer can Aust choose to put a (a$ue into the instruction2 thus
sa(in+ a C<6 memor" $ocation 0or somethin+ e$se.
7hatFs !hat is happenin+ here. &ma$$ (a$ues (0 ; O100 can be immediate (a$ues2 !here it
maDes sense. 5ar+er ones !onFt 0it into instructions and must be addressed in the usua$
!a"P name$"2 b" re0erence to their C<6 memor" address2 as in the mo( instruction abo(e.
Bhat !e ha(e here then is the contents o0 the C<6 memor" $ocation2 re0erred to b" the
$abe$ 7ime2 +ets added to the immediate (a$ue OM2 !ith the resu$t bein+ stored bacD to the
C<6 memor" the $abe$ 7ime points to. In this case2 that happens to be memor" $ocation
number >%?.
(his instruction then adds a short amount o additional time to the s%stem count. (his is
<ust enough time to get through the rest o the initial instructions$ !ithout missing the
counterE .ore instructions F more initial dela% time. =ot enough dela% time and the
program has to !ait or the counter to !raparound. 1n an 80 mh# s%stem$ this takes a
little under one ull minute.
Instruction M5N !aitcnt Mcog memory location containing counter alue to !atch forN
Mcog memory location containing delay to be added in preparation for ne+t !atchN
P$**+ R3S 1a(t',t 0(m)< 2)$ay
7his instruction a$so has a $abe$2 J/$oopK. 7he $abe$ means !e are +oin+ to be comin+ bacD
to this instruction $ater on. Rather than ca$cu$ate the C<6 memor" address to return to2
itFs a !ho$e $ot easier to Aust s$ap a $abe$ on the return point and re0er to it $ater. 7his is
50
!hat J/$oopK is a$$ about here. It is a destination $abe$ 0or a Aump instruction some!here
e$se in the pro+ram. The t!o together form a program loop"

7he !aitcnt instruction pauses the C<6 pro+ram2 unti$ the s"stem counter (a$ue matches
the C<6 memor" $ocation speci0ied in the 0irst part o0 the instruction. In this case2 thatFs
the one pointed to b" the $abe$ 7ime. &o !eF(e +ot/
Pause until cnt P Time
Additiona$$"2 e(er" instruction taDes some time to e-ecute. At %0 Mhz2 this is 40ns o0 time
actua$$". 7his is !h" a sma$$ (a$ue !as added to 7ime in the instruction abo(e. It is
necessar" to account 0or instruction e-ecution time !hen settin+ up a !aitcnt.
I0 this is not done proper$"2 the !aitcnt instruction is e-ecuted a0ter the counter and C<6
memor" (a$ues happen to match up. 7he pro+ram then !i$$ pause 0or some 4. seconds2
!ith an %0Mhz c$ocD2 !aitin+ 0or the counter to !raparound and then match up a second
time.
(his is a propeller red lag$ B(2. , %ou-"e got a program that takes a)out a minute to do
an%thing$ %ou-"e hosed up a !aitcnt or sure.
7hatFs not a$$ 0or this instruction thou+h. Rea$$"2 this is $iDe t!o instructions: 7he 0irst one
is a$$ about !aitin+ unti$ the counter matches some C<6 memor" $ocation. <nce that
match has occurred2 the second part o0 the instruction comes into p$a".
No!2 the C<6 memor" $ocation re0erred to b" the $abe$ /elta2 +ets added to the contents
o0 C<6 memor" $ocation 7ime.
Time P Time Q /elta
7!o instructions in one: 7he co+ stops and !aits 0or the s"stem counter to reach a Dno!n
(a$ue2 then adds some other (a$ue to the (a$ue bein+ !atched 0or2 then continues on.
Instruction M4N +or Mdestination C?F memory addressN Msource C?F memory addressN
R4S x*r *uta< ?(, 0*%%$) ?(,
No! itFs time to taDe a di+ression. 7o understand !hat this instruction does2 !eF(e +ot to
e-p$ore the bit operators. 7hese are $o+ica$ operations that can be app$ied to numbers the
same !a" math operations are app$ied.
7he -or instruction !orDs $iDe the add instruction does. 7he source !i$$ be operated on
to+ether !ith the destination2 !ith the resu$t o0 the operation bein+ !ritten to the
destination. In this case2 the source is C<6 memor" associated !ith the $abe$ Pin. 7he
destination is another pre;de0ined $abe$ that points to the re+ister contro$$in+ the 3in
51
states. &ince pin 1# is de0ined as an output pin2 this instruction then a00ects itFs state2 hi+h
or $o!2 dependin+ on the $o+ic contained in the bit operator2 !hich is -or (e-c$usi(e or.
I0 "ou donFt Dno! !hat bit $o+ic operators are2 consider readin+ that section be$o!P
other!ise itFs on to the $ast pro+ram instruction.
Instruction M3N Rmp MOimmediate C?F memory location to Rump toN
>4? Amp @/$oop
7his one is rea$$" simp$e. ItFs a $ot $iDe the mo( instruction in that some (a$ues +et copied
around. 7he immediate addressin+ mode is used2 so e(er"thin+ about this instruction is
contained in the instruction. Additiona$$"2 there is on$" one data 0ie$d speci0ied:
7he Amp instruction copies a number into the 3rope$$er pro+ram counter: 7hatFs it. <nce
the 3ro+ram Counter has been chan+ed2 pro+ram e-ecution continues at the ne! $ocation.
Here !e see the $abe$ $oop bein+ used in an immediate mode. Be Dno! that $oop reso$(es
to the C<6 memor" $ocation @.. &o2 a Amp @. !ou$d !orD here nice$". Ha(in+ the $abe$ is
hand" ho!e(er. I0 additiona$ instructions !ere p$aced be0ore the /$oop $abe$2 itFs abso$ute
memor" $ocation !ou$d chan+e2 and that !ou$d mean chan+in+ the Amp too.
Nobod" !ants to !orr" about that2 so the 3rope$$er 7oo$ e(a$uates $oop2 then puts that
(a$ue into the Amp2 at the time the pro+ram is prepared 0or $oadin+ onto the 3rope$$er.
Note/ I0 the pound si+n is missin+2 then the contents o0 C<6 memor" address @. !ou$d
then contain the (a$ue to be $oaded into the 3ro+ram Counter: 7hat !ou$d not ha(e the
desired e00ect at a$$2 since $ocation number . is an instruction2 itFs +oin+ to point the
3ro+ram Counter some!here other than !here !e !ant it to be.
In this case2 that !ou$d be outside the pro+ram2 meanin+ !eF(e $ost contro$ o0 the C<6: ItFs
+oin+ to Aust continue e-ecutin+ !hate(er it 0inds in itFs memor" ob$i(ious to an"thin+ !e
intend 0or it to do.
, call this >&app% Cun .emor% 9and?E
I0 !e !anted to Amp this !a"2 !e !ou$d ha(e to $oad a C<6 memor" $ocation !ith the (a$ue
o0 /$oop2 then the Amp !ithout the pound si+n !ou$d !orD as intended. 7hatFs e-tra !orD2
not reLuired in this case. 7hat is a$so ca$$ed an indirect address2 !here a memor" $ocation
contains an address. Important 0or subroutines2 and other structured Aumps.
Program /iscussion
7hatFs it. BeF(e more or $ess parsed throu+h this pro+ram. Be no! Dno! it does the
52
0o$$o!in+ thin+s o(er and o(er/
(in pseudo english form)
;&pin procedure JMainK $aunches assemb$" pro+ram in C<6 1
; set the direction state o0 pin 1# to be output
;$ooD at the s"stem counter and store it in the !orDin+ memor" $ocation
;add a sma$$ minimum2 J+et thin+s startedK de$a" time to it
Dloop ;!ait 0or the s"stem counter to catch up2 !hen it does2 add de$a" time
;to++$e the pin
;+o bacD to the tasD identi0ied b" the $abe$ Dloop
Additiona$$"2 there are thin+s !e can chan+e. 7hose thin+s are the immediate (a$ue used
0or the initia$ de$a". 7hatFs the O7f in instruction number >)?. 7he pin to be to++$ed can be
chan+ed b" rede0inin+ the initia$ized (a$ue2 identi0ied b" the $abe$ Pin2 !hich current$" is
memor" $ocation >#?. Mina$$"2 the de$a" bet!een to++$es is contained in C<6 memor"
$ocation >7?2 associated !ith $abe$ /elay.
Mina$$"2 some understandin+ about the $e(e$ and scope o0 the tasD breaDdo!n shou$d be
understood at this point. ,ach instruction has a !e$$ de0ined contribution to the tasD as a
!ho$e.
I recommend some time spent maDin+ these chan+es2 a$on+ !ith some time !atchin+ the
resu$ts in 6,AR2 or better2 on "our 3rope$$er hard!are setup. I0 "ou !ant2 use the t!o C<6
e-amp$e so "ou ha(e somethin+ to compare !ith.
7his is a$so a +reat temp$ate pro+ram2 0or tr"in+ other instructions out. &ome su++estions
are/
1. Chan+e the pattern o0 the pin to++$e. Ha(e it be a repeatab$e seLuence2 $on+2 short2
$on+2 $on+...
7his can be done !ith a $on+er $oop and some additiona$ !aitcnt instructions.
,+/
!aitcnt 7ime2 De$a"
x*r *uta< ?(,
1a(t',t 0(m)< 2)$ay1
x*r *uta< ?(,
). 7o++$e more than one pin. App$" the bit operation in0o presented to bui$d di00erent pin
masDs and operate on them.
53
Bit Logic and >anipulation ?perators
Near the be+innin+ o0 this document2 the idea that computers on$" do t!o core thin+s !as
introduced. 7hatFs some!hat o0 an o(ersimp$i0ication. 7here is one additiona$ core area o0
operation to be co(ered in this section. 7hese operators cou$d be inc$uded near the
be+innin+ !ith the math. Ma"be the" shou$d. 7he ba$ance 0or ne! in0ormation !as
!ei+hed !ith actua$$" +ettin+ throu+h an assemb$" $an+ua+e pro+ram. Ha(in+ done that2
itFs time to rea$$" e-p$ore these po!er0u$ too$s. 7he" are as essentia$ as the math is.
Computers process $o+ic in the same !a" the" process math. In 0act2 the t!o ideas are
c$ose$" re$ated as !i$$ be e-p$ained be$o!. Bhen t!o binar" numbers are combined2 or
operated on2 ru$es are app$ied. &o 0ar2 the math oriented ru$es ha(e been presented. In
this section2 $o+ic ru$es !i$$ be presented.
,ach operator !i$$ be co(ered. 7here are some ne! concepts to be $earned in this section.
7he" !i$$ be introduced in conte-t2 in a 0ashion simi$ar to ho! the math section is done.
7he operator2 J<RK is used to introduce source2 destination2 etc... <ther operators !i$$
assume that materia$ is understood. Additiona$$"2 !here the operators app$" to the
e-amp$e pro+ram2 that !i$$ be discussed in conte-t as !e$$2 !ith the +oa$ o0 a
con(ersationa$ st"$e that ties the !ho$e to+ether2 a$beit in a some!hat $ess or+anized
0ashion.
Truth Tables
A truth tab$e is simp$" a $istin+ o0 combinations and resu$ts 0or a +i(en ru$e. Mor an" set o0
binar" numbers2 the number o0 uniLue combinations is the sum o0 the po!ers o0 t!o itFs
number o0 di+its represent2 p$us one because !e count 0rom one and not zero.
E%P %1111 = $0- (+1) = 16 = ,um/)r *- '*m/(,at(*,!
%1111&1111 = $-- (+1) = 256
A sin+$e binar" di+it has t!o states2 on and o002 hi+h and $o!2 true (12 0a$se (02 etc...
<peratin+ on t!o numbers then re(ea$s the basic truth tab$e size o0 0our possib$e states2
t!o 0or each di+it in(o$(ed.
7ruth tab$es 0or the (arious $o+ic operators !i$$ be +i(en be$o!2 a$on+ !ith e-p$anator"
te-t. 7ruth tab$es breaD do!n the di+it combinations2 app$" the ru$e and summarize the
resu$t as bein+ either true (1 or 0a$se (0.
?$ (Bit!ise ?$)
7he ru$e 0or this one is simp$e. I0 either bit is set2 or true2 then the resu$t is true2
other!ise 0a$se. Here is the truth tab$e/
45
54
-----------
1 *r 1 = 1 <---5)!u$t *- 45 ru$) a++$()9
1 *r 0 = 1
0 *r 1 = 1
0 *r 0 = 0
&ome e-amp$es are in order here2 a$on+ !ith some possib$e use scenarios. Consider the
binar" numbers be$o!/
%11000011 <--- 4+)ra,9 0 %0000 %1001
45 %10011001 <--- 4+)ra,9 1 45 %1010 45 %0000
--------- ----- -----
%11011011 <--- B*%('a$ 5)!u$t %1010 %1001
7his one is prett" eas": I0 there is a one an"!here2 the resu$t is one. 7he direction o0 the
operands does not matter either. <ne is either per0ormin+ an <R operation2 or one is not.
ItFs !orth taDin+ a $ooD at ho! the prope$$er does it/
230 *r% 0
R0S *r 5)!u$t< 4+)ra,90
R1S *r 5)!u$t< =%0001&1101
?r) 45 (,!tru't(*, C46 m)m*ry '*,t),t!P
5)!u$t $*,% %00000000&00001111&00000000&00000000
4+)ra,90 $*,% %00001100&10000001&00011100&00010000
K
?*!t (,!tru't(*, '*,t),t!< !h*1, -*r )a'h (,!tru't(*,#
5)!u$t R0S $*,% %00001100&10001111&00011100&00010000
5)!u$t R1S $*,% %00001100&10001111&00011100&00011101
:*t) that 4+)ra,90 ,)>)r 'ha,%)9M
L
Mor the moment2 $etFs pretend this bit o0 code has Aust been $oaded into a C<62 and the
3ro+ram Counter is at >0?2 pointin+ to the 0irst or instruction. At this moment2 both Resu$t
and <perand0 are initia$ized data2 residin+ in the C<6 memor".
Bhen instruction >0? e-ecutes2 the contents o0 <perand0 are <RFed to+ether !ith the
contents o0 Resu$t. 7he resu$t o0 this operation is !ritten bacD to the C<6 memor"
associated !ith $abe$ Resu$t. 7his can be seen in the comment section abo(e.
No! the ne-t <R instruction runs2 but this time one o0 the operands is immediate. 7hatFs
the pound si+n J @ J. 7hat operand2 N0001W1101 +ets <RFed !ith Resu$t and the resu$t o0
that ends up bacD in the C<6 memor"2 sho!n in the comment section abo(e >1?.
55
<n the 3rope$$er2 bit operation instructions 0o$$o! this +enera$ con(ention/
45 R5)!u$t 4+)ra,9 1S< RS*ur') 4+)ra,9 0S
Remember2 the resu$t o0 the operation !i$$ end up bein+ stored !here <perand 1 ori+ina$$"
!as. 7his is a destructi(e operation2 in that !hen it is comp$eted2 <perand 1 no $on+er
e-ists in itFs ori+ina$ 0orm: Ha(in+ this beha(ior is actua$$" a +ood thin+2 Aust be sure "ou
understand it.
:hat about the Carry; (Parity)
=n$iDe the math operations2 bit!ise operations donFt carr" o(er. 7he" operate on each pair
o0 di+its in a discrete 0ashion. 7he carr" does ser(e a use0u$ purpose ho!e(er. A0ter a bit
operation2 the number o0 ones in the resu$t is computed.
This is called parity, and itFs simp$" an e-pression o0 the number o0 di+its containin+ a one2
bein+ either e(en or odd.
I0 the" are e(en2 thatFs a parit" resu$t o0 0. I0 the" are odd2 then thatFs a parit" resu$t o0 1.
)%P
%11000011 = ?ar(ty E>), = 0 %10011001 = ?ar(ty 499 = 1
7his resu$t !i$$ +o into the carr" 0$a+2 un$ess "ou instruct the 3rope$$er to i+nore the
operation parit" !ith the JNCK condition.
45 5)!u$t< =-- :C
It is a$so possib$e to compute the parit" on$"2 and not store the resu$t at a$$: Here is an
instruction to do Aust that/
45 5)!u$t< %1101&0011 :5
3arit" is used a$$ o(er the p$ace to per0orm (er" simp$e error checDin+. I0 a stream o0 bits is
mo(in+ 0rom computer to computer2 or e(en 0rom one part o0 a computer to another2 the
chance e-ists that one or more o0 those bit states !i$$ end up chan+ed in the process.
\uite simp$"2 stu00 happens.
3arit" can catch these in a hi+h number o0 cases2 because the sum tota$ o0 ones2 bein+ e(en
or odd2 !i$$ chan+e !ith a sin+$e bit chan+e. A parit" computation then ser(es as a +enera$
sanit" checD.
:hat else can I do !ith ?$;
56
7he <R instruction is +reat 0or settin+ a speci0ic bit to a set2 true2 or one state !ithout
impactin+ the other bits surroundin+ it. 7his is done !ith a bit mas<"
Bit >as<ing
A bit masD is an operand setup !ith the idea o0 it bein+ app$ied to another operand to
achie(e a speci0ic and predictab$e resu$t. ItFs o0ten used !hen the patterns o0 di+its to be
a00ected (ar" !ide$"2 but the +enera$ resu$t does not.
Bithout bit masDs2 most pro+rams !ou$d +ro! to be (er" $en+th$" as man" branches and
stora+e $ocations !ou$d be reLuired to hand$e (aried di+its and their manipu$ations.
In the e-amp$e pro+ram abo(e2 a speci0ic pin direction state !as set to the output state.
(1 Bhat i0 another pin state needed to be set2 and !e !ere ne(er sure !hat other states
ma" ha(e been set in other co+s2 or e(en b" other parts o0 the pro+ram itse$0C
In this case !e need to set a sin+$e di+it to a one2 !ithout impactin+ the other di+its. 7his
is an <R operation.
Another e-amp$e is +raphics. A t"pica$ +raphics screen has a bitmap disp$a". 7he binar"
numbers are read and trans0ormed into pi-e$s on the screen. =n$ess the number o0 bits per
pi-e$ is a b"te2 !ord or $on+2 it is necessar" to manipu$ate bits direct$" to set and reset
pi-e$s.
5etFs sa" the (ideo sub;s"stem is operatin+ in a mode !here one bit is used per pi-e$...
2ES0 %00000000 ######## S4;5CE %11111111 2ES0 TTTTTTTT
%00111100 ##TTTT## %10000001 T#TTTT#T
%00011000 ###TT### 45 %10000001 = T##TT##T
%00000000 ######## %11111111 TTTTTTTT
45 2ES0< S4;5CE
In the e-amp$e abo(e2 the (ideo screen source b"tes contain an e-istin+ ima+e on screen.
7he source b"tes are some!here e$se2 and are to be dra!n on top o0 !hate(er is a$read" on
the +raphics screen. In this case2 the <R instruction $ets us do somethin+ that !i$$ on$" add
pi-e$s to the screen.
<ther bit operations !orD $iDe <R does. ,ssentia$$"2 there are t!o operands. <ne is the
source2 and is not chan+ed b" the instruction. 7he other is the destination2 and is chan+ed
b" the instruction2 un$ess the pro+rammer has set the NR e00ect to inhibit this 0rom
happenin+.
?ther Truth Tables
57
S?$ (.+clusie ?r)
7he e-c$usi(e or operation is the to++$e2 in the pro+ram abo(e. ItFs truth tab$e $ooDs $iDe
this/
U45
-----------
1 x*r 1 = 0 <---5)!u$t *- U45 ru$) a++$()9
1 x*r 0 = 1
0 x*r 1 = 1
0 x*r 0 = 0
7his ru$e can be summed up thus/ I0 the 0irst operand or the second operand2 not both2 is
one2 the resu$t is oneP other!ise the resu$t is zero.
Consider this truth tab$e in the conte-t o0 the e-amp$e pro+ram abo(e. 7his is ho! a to++$e
is achie(ed !ith one instruction. 7he C<6 memor"2 identi0ied b" $abe$ 3in2 contains the
0o$$o!in+ binar" bit masD/
N00000000W00000001W00000000W00000000
7his number identi0ies pin 1#2 and is used to chan+e itFs state. 7he state o0 a$$ the pins can
be obtained in the outa re+ister. I0 one reads this re+ister2 one +ets the state o0 a$$ the
pins. I0 one !rites to this re+ister2 one then chan+es said states.
7he -or instruction/
R4S x*r *uta< ?(, 0*%%$) ?(,s
taDe the bit masD at 3in2 0or itFs source2 and operates on the pin states themse$(es as a
destination. App$" the truth tab$e to see the to++$e happen thus/
*uta %xxxxxxxx&xxxxxxx0&xxxxxxxx&xxxxxxxx
x*r ?(, %00000000&00000001&00000000&00000000
5)!u$t *uta %xxxxxxxx&xxxxxxx1&xxxxxxxx&xxxxxxxx
*uta %xxxxxxxx&xxxxxxx1&xxxxxxxx&xxxxxxxx
x*r ?(, %00000000&00000001&00000000&00000000 (S*ur') /(t ma!N)
5)!u$t *uta %xxxxxxxx&xxxxxxx0&xxxxxxxx&xxxxxxxx (2)!t(,at(*,)
Notice the - in the resu$ts abo(eC 7he - means2 JdoesnFt matterK. 5ooDin+ at the -or truth
tab$e2 it can be seen that app$"in+ an -or operation2 !ith a source operand di+it o0 02 does
58
not chan+e the destination operand e(er. I0 a one is speci0ied ho!e(er2 the destination
operand state !i$$ be to++$ed to the opposite state.
=sin+ a bit masD then $oca$izes the impact o0 the -or operation to the bit contro$$in+ the
state o0 pin+ 1#.
I0 it !as necessar" to to++$e pin 17 a$so2 the on$" chan+e !ou$d be to the 3in bit masD2
$ea(in+ the rest o0 the pro+ram as is.
7he J]^ 1#K is a shortcut that basica$$"2 sets a one2 then shi0ts it o(er 1# times. 7he pin
state cou$d a$so be speci0ied $iDe this/
?(, R6S $*,% %00000000&00000001&00000000&00000000
7here is no di00erence. 7he short hand method2 is computed b" the 3rope$$er ID, too$. 7he
$on+er method is De"ed in b" the pro+rammer. ,ither is 0ine2 so $on+ as the masD correct$"
identi0ies the desired pins to impact.
&u++estion/ Modi0" the bit masD and (ie! the resu$t in 6,AR.
A#/ (Bit!ise A#/)
HereFs the truth tab$e/
3:2
-----------
1 a,9 1 = 1 <---5)!u$t *- 3:2 ru$) a++$()9
1 a,9 0 = 0
0 a,9 1 = 0
0 a,9 0 = 0
7he and ru$e is (er" simp$e/ 7he resu$t is on$" a one2 !hen both operands are one.
A#/# (Bit!ise A#/ of one number, !ith #?T of another)
7his instruction in(o$(es t!o truth tab$es2 and is rea$$" t!o operations pacDed into one. A
N<7 operation is per0ormed on the source operand2 !ith the resu$t o0 that bein+ ANDed
!ith the destination. It $ooDs $iDe this/
3:2:
----------------------------------------------------
S*ur') %00000000&00000001&00000000&00000000
:40 S*ur') %11111111&11111110&11111111&11111111
3:2 2)!t(,at(*, %00101100&00111001&00010000&00000000
59
2)!t(,at(*, --. %00101100&00111000&00010000&00000000(5)!u$t 1r(tt),)
#?T (Bit!ise #?T)
=n$iDe the other operations2 the N<7 ru$e on$" accepts one operand. Here is the truth
tab$e/
:40
-----------
1 ,*t = 0
0 ,*t = 1
7he ru$e 0or N<7 is in(erse. MaDe a$$ the ones into zeros2 and a$$ the zeros into ones.
7hatFs it: Not is part o0 the 3rope$$er instruction set. 7he same resu$t can be obtained !ith
E<R2 usin+ a source masD o0 a$$ ones. (OMMMMMMMM Not is $isted here2 as part o0 the
e-p$anation 0or the ANDN instruction2 !hich per0orms a N<7 on itFs source operand.
7hese are the essentia$ bit $o+ic operators. <ther instructions2 such as mu-2 are
combinations o0 these aimed at speci0ic purposes and e00icienc". 7he" are co(ered in the
3rope$$er Manua$.
60
Assembly language programming, as seen no!, is about these core thingsD
)understanding the hard!are !ith a high degree of fidelity
can't control that !hich is not understoodG
)brea<ing the tas< do!n into small elements, that leerage said understanding
if you hae problems, you probably don't understand e+actly !hat the hard!are is
really doing
)organi9ationG
you can ma<e as big of a mess as you !ant to 'Teep It Bimple Btupid MTIBBN applies here
as much as it does any!here"
I
I hae more e+amples planned, but need to stop here for the moment" Comments,
feedbac<, successes, failures, stories, all appreciated" This is actually enough to begin
to !or< through the e+cellent intermediate tutorial deBila has !ritten"
That's e+actly !here I am at right no!G
61
Alphabetical Index
acti(e memor" $ocation..................................................................................0
Addition....................................................................................................14
address space (H=*.....................................................................................)4
AND (*it!ise AND........................................................................................49
ANDN (*it!ise AND o0 one number2 !ith N<7 o0 another.........................................49
Assemb$" 3ro+ram Detai$...............................................................................42
Assemb$"7o++$e.spin......................................................................................%
*inar"........................................................................................................7
*it............................................................................................................9
*it 5o+ic and Manipu$ation.............................................................................34
*it MasD....................................................................................................32
*racDet comment........................................................................................41
*"te..........................................................................................................9
carr"........................................................................................................1#
carr" 0$a+..................................................................................................1)
C$ocD.......................................................................................................)4
CN7.........................................................................................................)4
C<6.........................................................................................................)#
C<6 0......................................................................................................)7
C<6 memor"..............................................................................................)#
C<6 memor" addressin+................................................................................)#
C<6 memor" re+isters....................................................................................1
co+ne!.....................................................................................................4.
C<6N,B...................................................................................................)#
Comment..................................................................................................41
Common computer numerica$ units...................................................................&(
Comp$ements.............................................................................................19
Computer Math...........................................................................................&4
Computer number sizes...................................................................................0
C<N.........................................................................................................4)
Constant...................................................................................................4)
con(ertin+ binar" to decima$...........................................................................14
C3=.........................................................................................................10
DA7.........................................................................................................44
data........................................................................................................)9
Data........................................................................................................42
Data ........................................................................................................)%
Data a$i+nment...........................................................................................)#
Decima$ to *inar" con(ersion..........................................................................%&
De$imiters.................................................................................................17
destination................................................................................................47
Di(ision....................................................................................................%(
,,3R<M 0i$e................................................................................................9
62
M$a+s........................................................................................................1)
Mormattin+.................................................................................................41
6,AR.......................................................................................................5%
Happ" Mun Memor" 5and................................................................................3%
He-adecima$ ...............................................................................................%
hi+h and $o!..............................................................................................34
H=*.........................................................................................................)4
H=* memor"..............................................................................................)4
I8<..........................................................................................................1)
Immediate addressin+...................................................................................3(
Initia$ized data...........................................................................................41
instruction time to e-ecute............................................................................3&
Instructions in RAM.......................................................................................)%
interna$ re+isters..........................................................................................0
Amp.........................................................................................................3%
Ri$ob"te ...................................................................................................10
5ar+e comment...........................................................................................4)
5east &i+ni0icant *"te (5&*............................................................................)%
$east si+ni0icant di+it....................................................................................14
$itt$e endian...............................................................................................)%
5on+..........................................................................................................9
Me+ab"te..................................................................................................10
memor" mapped re+ister................................................................................0
Modu$o.....................................................................................................1%
Most &i+ni0icant *"te (M&*.............................................................................)%
most si+ni0icant di+it....................................................................................14
mo(.........................................................................................................41
Mu$tip$ication.............................................................................................%(
Ne+ati(e...................................................................................................)1
Nibb$e........................................................................................................9
N<7 (*it!ise N<7......................................................................................#0
Number Representations.................................................................................2
<6 memor" re+ister......................................................................................0
<neFs comp$iment........................................................................................))
<pcode.....................................................................................................1)
operands...................................................................................................)9
<R (*it!ise <R..........................................................................................34
3a+es.......................................................................................................10
3arit".......................................................................................................3,
parse.......................................................................................................4.
3ersoni0ication............................................................................................10
pri(ate.....................................................................................................4.
procedures................................................................................................4.
3ro+ram ...................................................................................................42
3ro+ram Counter.........................................................................................)9
3ro+ram Header..........................................................................................41
63
3ro+ram &ections.........................................................................................4(
3rope$$er 7oo$...............................................................................................
3=*.........................................................................................................4)
3ub$ic......................................................................................................4)
Luantit"....................................................................................................11
RAM.........................................................................................................11
Re+ister....................................................................................................)9
R<M.........................................................................................................11
&i+ned ma+nitude........................................................................................))
&in+$e Luote comment...................................................................................41
source......................................................................................................47
&3IN.........................................................................................................)9
startup C<6...............................................................................................)7
states......................................................................................................34
&ubtraction................................................................................................&0
&"stem Counter...........................................................................................)4
to++$e......................................................................................................4%
7ruth 7ab$es...............................................................................................34
t!o C<6 e-amp$e........................................................................................44
7!oFs comp$iment........................................................................................))
=ninitia$ized .............................................................................................41
!aitcnt....................................................................................................3(
Bord.........................................................................................................9
!raparound...............................................................................................1%
-or..........................................................................................................3&
E<R (,-c$usi(e <r......................................................................................4%
zero 0$a+...................................................................................................1)
&etup......................................................................................................55
64
Appendi+ A
Parsing Assembly Language Programs
5iDe an" pro+rammin+ $an+ua+e2 there are times !hen itFs +ood to +o throu+h other
pro+rams and understand !hat is happenin+ !hen and !h". 7here is a +ood starter process
0or this IFm +oin+ to detai$ here2 in an appendi-2 $ar+e$" because itFs a topic thatFs re$e(ant
to the be+inner2 and because it Aust doesnFt 0it !ithin the o(era$$ document 0$o! so 0ar.
Assemb$" $an+ua+e pro+rams are di00erentiated 0rom hi+her $e(e$ ones2 !here parsin+ to
understand is concerned2 b" their simp$e Jbui$din+ b$ocDK structure2 and the need to
consider the hard!are 0unctiona$it" in the conte-t o0 the pro+ram to a hi+her de+ree than
is o0ten reLuired 0or hi+her $e(e$ pro+rams. BorDin+ throu+h a pro+ram then consists o0
understandin+ !hat each instruction does2 and !h" that matters +i(en the hard!are at
hand.
7he core e$ements o0 the 3rope$$er itse$0 are !e$$ documented in the manua$ and
schematics. *ecause the scope o0 this document is a$$ about core understandin+ o0 the
so0t!are side o0 thin+s2 IFm +oin+ to $imit the discussion on the hard!are side accordin+$".
7his is a process that has a$!a"s !orDed (er" !e$$ 0or me. 7here are others approaches to
this and b" a$$ means consider and use them i0 the" !orD 0or "ou.
Assemb$" $an+ua+e instructions a$$ boi$ do!n to three core t"pes/
/ata moement
7he data mo(ement t"pes essentia$$" +et data 0rom one p$ace to another. Durin+ the
course o0 pro+ram e-ecution2 (a$ues end up in (arious p$aces as (arious times. 7hese are
the instructions that +et that done.
?perators
,ssentia$$"2 these instructions chan+e bits. &ome e-amp$es o0 operators are add2 subtract2
bit!ise <R2 AND2 shi0t $e0t2 shi0t ri+ht2 rotate $e0t2 rotate ri+ht2 etc...
Program -lo!
ItFs rare that a pro+ram is Aust a $inear pro+ression o0 instructions. <0ten thin+s need to be
done mu$tip$e times2 and the seLuence o0 e(ents necessar" to comp$ete the tasD !i$$ (ar"
dependin+ on the state o0 the data too. 3ro+ram 0$o! instructions hand$e these aspect o0
thin+s. 7he" a$so he$p !ith the o(era$$ structure o0 thin+s2 !here there is pro+ram and
data e$ements present2 pro+ram 0$o! instructions then Deep the 0$o! o0 e-ecution in the
pro+ram areas. Nobod" !ants to e-ecute their data2 un$ess the" are bui$din+ se$0;modi0"in+
code:
65
(In that case2 the data to be manipu$ated is part o0 the pro+ram itse$0. 7his happens on the
prope$$er o0ten 0or pointers.
ItFs (er" hand" to ha(e a chart o0 "our a(ai$ab$e instructions or+anized into these rou+h
cate+ories. Here is one be$o!2 "ou can start !ith toda". Iou !i$$ see some instructions end
up bein+ cate+orized in more than one !a". 7he reason 0or this is those instructions bund$e
0unctions to+ether.
,+/ DUN1 7his instruction decrements a speci0ic C<6 memor" $ocation2 and per0orms a UM3
(Aump to operation2 i0 that C<6 memor" $ocation is not "et eLua$ to zero. 7hatFs a coup$e
o0 operators (decrement and compare to zero and a pro+ram 0$o! e$ement present2 a$$ in
one instruction: (Aump to2 i0 not zero IF(e +ot those in bo$d.
Mrom the chart abo(e2 itFs c$ear that most o0 the instructions are operators. Bhere parsin+
a pro+ram is concerned2 this means startin+ !ith the pro+ram 0$o! and data mo(ement
instructions he$ps put a$$ the operators in conte-t. Not a bad p$ace to start: Iou +et a $ot
66
Data Transport Operators Operators Program Flow
CLKSET ABS NEG* CALL
COGID ABSNEG NEGC COGINIT
ADD NEGNC COGSTOP
HUBOP ADDABS NEGNZ
ADDS NEGZ DJNZ
LOCKCLR ADDSX
LOCKNEW ADDX OR JMP
LOCKRET AND JMPRET
LOCKSET ANDN RCL
RCR NOP
MOV CMP REV
MOVD CMPSUB ROL RET
MOVI CMPSX ROR
MOVS CMPX TJNZ
SAR TJZ
RDBYTE DJNZ SHL
RDLONG SHR WAITCNT
RDWORD MAX SUB WAITPEQ
MAXS SUBABS WAITPNE
WAITVID MIN SUBS WAITVID
WRBYTE MINS SUBSX
WRLONG MUXC SUBX
WRWORD MUXNC SUMC
MUXNZ SUMNC
MUXZ SUMNZ
TEST
XOR
o0 structure !ith out ha(in+ to hand$e too man" instructions at one time.
,ach instruction does a 0air$" simp$e thin+. Ma"be itFs Aust mo(in+ a number to a $ocation.
Bhat that number does is o0ten as much about the hard!are as it is an"thin+ e$se. Mo(in+
a number to set a pin state2 0or e-amp$e2 mi+ht $i+ht an 5,D2 or tri++er some other de(ice
to do somethin+.
Mo(in+ that same number to a di00erent $ocation in memor" ma" Aust pro(ide some
in0ormation necessar" to comp$ete a math operation in another part o0 the pro+ram too.
The Process
Ha(in+ +one throu+h that2 itFs time to taDe a $ooD at the process. 6enera$$" speaDin+2 itFs
+ood to +o throu+h and understand !hich instruction does !hat2 accordin+ to the +ross
characterizations abo(e. &o2 is it a data mo(e2 operation2 or pro+ram 0$o! instructionC
Mrom there2 taDin+ it a pass at a time2 "ou breaD the pro+ram do!n into pro+ram areas2
data areas2 inputs and outputs2 and other such thin+s that he$p "ou to understand the
structure o0 thin+s. ItFs not a bad idea to print thin+s out2 and maDe notes to rein0orce
!hat "ou are $earnin+.
Mina$$"2 that structure and understandin+ o0 !hat the instructions do2 !hen the" do it2 and
on !hat the" do it to2 a$$ combine to 0orm hi+her $e(e$ intent. Ha(in+ arri(ed here2 "ou
shou$d be ab$e to put to+ether a p$ain ,n+$ish description o0 !hat that pro+ram does.
ItFs !orth notin+ at this point2 chan+in+ the pro+ram2 or !ritin+ a simi$ar one is rea$$" this
process in re(erse: Iou +o 0rom hi+her $e(e$ descriptions bacD do!n to the +ranu$ar2
instruction b" instruction tasDs necessar" 0or it to happen on !hate(er hard!are "ou are
!ritin+ 0or.
7he bu$D o0 the main document is about the computin+ 0oundations necessar" to understand
!hat instructions do and !hat pro+rams $ooD $iDe. 7his appendi- is rea$$" about taDin+ that
and app$"in+ it to other pro+rams to speed the $earnin+ and use process.
Consider the 0o$$o!in+ bit o0 code/
456 0
m*> C*u,t)r&0(m)&0*&Ea(t&F*r< C:0
a99 C*u,t)r&0(m)&0*&Ea(t&F*r< =$20 C,(t(a$&Ea(t&0(m)
1a(t',t C*u,t)r&0(m)&0*&Ea(t&F*r< 399(t(*,a$&Ea(t&0(m)
In the sma$$ bit o0 code abo(e2 there is one o0 each instruction t"pe. 7he" are co$ored2 as
i0 hi+h$i+hted. I0 this !ere a $ar+er pro+ram2 some time !ou$d ha(e been spent $ocatin+
67
pro+ram 0$o! instructions2 $ooDin+ at the $abe$s to see !hat b$ocDs o0 code e-ecute !hen
and !here2 0o$$o!ed b" a 0e! more passes to $ocate mo(ement and operators.
In this case2 itFs enou+h to Aust proper$" characterize each instruction and mo(e on. ItFs Aust
a code se+ment to i$$ustrate the process in +enera$.
7he mo instruction is a transport 8 mo(ement instruction. Rea$$"2 a$$ this one does is mo(e
some bits 0rom one C<6 memor" $ocation to another. 7his does not te$$ much o0 the stor"
ho!e(er2 and thatFs !here the hard!are part o0 the picture comes into p$a". CN7 happens
to be a prede0ined constant that points to the C<6 memor" $ocation !here the current
(a$ue o0 the s"stem counter can be obtained. 7he mo(e instruction taDes !hate(er that
(a$ue is and stores it in a C<6 memor" $ocation2 so that the other parts o0 the pro+ram can
!orD !ith it.
In p$ain ,n+$ish2 this instruction Aust notes !hat the (a$ue o0 the counter is at the time it is
e-ecuted2 0or use $ater on. 7hatFs it:
In &3IN2 that mi+ht $ooD $iDe this/
CounterW7imeW7oWBaitWMor /Q CN7
=ote ho! the la)els con"e% some o the stor% too. 1ten !hat the la)els are
!ill oer up clues as to !hat is happening. 7ometimes this is the case and
sometimes it isn-t. Cor program segments !ith seemingl% cr%ptic la)els$ it-s
oten !orth it to make la)el notes$ or do some digging in the hopes to see
!hat the la)el might mean.
Be Dno! no! there is a s"stem counter2 itFs (a$ue is important to this pro+ram2 and that
(a$ue has been noted 0or use $ater on.
7he add instruction is an operator one. ,ssentia$$"2 this boi$s do!n to some bits +ettin+
chan+ed. In this case2 the immediate (a$ue O)0 is added to the counter (a$ue a$read"
noted2 !ith the resu$t o0 that addition stored in the C<6 memor" $ocation
CounterW7imeW7oWBaitWMor.
In ,n+$ish then2 !e understand the pro+ram is addin+ a (a$ue to the one noted abo(e and
storin+ it !here the ori+ina$ (a$ue used to be.
In &3IN2 thatFs (er" simi$ar to this/
CounterW7imeW7oWBaitWMor /Q CounterW7imeW7oWBaitWMor T O)0
No! !e are do!n to the $ast instruction2 !aitcnt. 7his one is tricD" because itFs rea$$" t!o
instructions in one: 7he !ait part o0 the instruction is pro+ram 0$o! re$ated. Ho!e(er2 itFs
a$so an operator in that it per0orms an addition a0ter itFs done !aitin+:
68
Baitcnt pauses unti$ the s"stem counter2 CN72 matches a (a$ue. In this case2 that (a$ue is
speci0ied b" the $abe$2 CounterW7imeW7oWBaitWMor.
In ,n+$ish this is/
3ause unti$ the s"stem counter is eLua$ to CounterW7imeW7oWBaitWMor.
7hatFs the pro+ram 0$o! part o0 the instruction. 7he operator t"pe is strai+ht0or!ard in
that the (a$ue identi0ied b" the $abe$2 Additiona$WBaitW7ime2 !i$$ be added to the (a$ue the
pro+ram !as !aitin+ 0or.
In ,n+$ish then2 this is/
Bhen the counter matches the tar+et (a$ue2 add Additiona$WBaitW7ime to the tar+et and
mo(e on.
5ooDin+ it a$$ o(er2 it then becomes c$ear the pro+ram is doin+ the 0o$$o!in+2 e-pressed in
p$ain ,n+$ish/
&" Loo< at the counter
%" #ote that alue
5" Add on some <no!n additional alue to compensate for time consumed to get things
done (e+ecute other instructions)
4" :ait on that result, allo!ing the counter to catch up"
3" *aing caught up, add in the ne+t !ait time"
(here are some hard!are considerations here$ that , glossed o"er. (he
reason some time is added to the counter "alue is )ecause instructions take
time to e'ecute. , that time is not accounted or$ the program !ill miss the
target counter "alue and hang or a)out a minute as the counter counts all
the !a% through and !raps around.
7hatFs basica$$" ho! it +oes 0rom bottom to top. Mrom here2 !e Dno! some thin+s about
the pro+ram and can maDe chan+es. 7he initia$ de$a" time is the O)02 (a$ue in the add
instruction. 7he other additiona$ !ait time can be chan+ed in the !aitcnt instruction as
!e$$. 7hese chan+es on$" maDe sense once the o(era$$ intent behind the pro+ram is !e$$
understood.
Britin+ ne! pro+rams2 or ma"be Aust chan+in+ this one then !orDs in re(erse: Mormu$ate
the hi+h $e(e$ statement that detai$s !hat needs to happen2 breaD it into discrete hard!are
re$ated pieces2 app$" the instructions that can per0orm the tasD2 and seLuence them into
the pro+ram 0$o!.
69

Potrebbero piacerti anche