You are on page 1of 107

Microprocessor Simulator V5.

0 Help
C Neil Bauers 2003

http://www.softwareforeducation.com/

General
Introduction rchitecture Installation !n"Installation # $ and %u&s '( Support Hand)oo* Use lt+Ta) to switch between the help and simulator windows.

Tutorials
Gettin& Started ll ,earnin& Tas*s 0- #irst 'ro&ram "" .ast/ 01ample 02 Traffic ,i&hts 03 4ata Mo5es 06 (ountin& 05 7e/)oard Input 08 'rocedures 09 Te1t I/: 0; 4ata Ta)les 0< 'arameters -0 S= Interrupts -- H= Interrupts

Reference
Shortcut 7e/s S(II (odes Glossar/ He1adecimal and %inar/ Instruction Set Summar/ Instruction Set 4etailed The ,ist #ile .e&ati5e .um)ers 'op"up Help ,o&ic and Truth The 0ditor 'eripheral 4e5ices

This simulator is for learners in the 1 ! a"e ran"e althou"h man# #oun"er enthusiasts ha$e used it too. %t introduces low le$el pro"rammin" and microcomputer architecture. Tutorial materials are included co$erin" the sub&ect in some depth. The tutorials ali"n closel# with the British 'C( )2 Computin" specifications and also the British BT(C National for %T *ractitioners +Computer ,#stems-. The simulator has enou"h depth and fle.ibilit# to be used with uni$ersit# under"raduate students stud#in" low le$el pro"rammin" for the first time.

Introduction
(ontents

=ho Should !se the Simulator


The simulator is intended for an# student stud#in" low le$el pro"rammin"/ control or machine architecture for the first time. The simulator can be used b# students a"ed 10 to 1 to sol$e less comple. problems such as controllin" the traffic li"hts and sna1e. 2ore ad$anced students t#picall# 1 or older can sol$e 3uite comple. low le$el pro"rammin" problems in$ol$in" conditional &umps/ procedures/ software and hardware interrupts and Boolean lo"ic. )lthou"h pro"rams will be small/ there is "ood scope for modular desi"n and separation of code and data tables. The simulator is suitable for courses such as BT(C National 4iploma for %T *ractitioners +Computer ,#stems and Control Technolo"#), and )2 Computin" +5ow 5e$el *ro"rammin"(lectronics Courses. Courses in$ol$in" microcontrollers. Courses in$ol$in" control s#stems.

4escription of the Simulator


The simulator includes more conceptuall# difficult instructions such as C)55/ 6(T/ %NT and %6(T. There is a hardware timer interrupt too. This simulator emulates an ei"ht bit C*U that is similar to the low ei"ht bits of the 70.7 famil# of chips. 28 b#tes of 6)2 are simulated. %t is surprisin" how much can be done with onl# 28 b#tes of 6)2.

#eatures
7 bit C*U 1 %nput 9utput ports. Not all are used. ,imulated peripherals on ports 0 to 8. )n assembler. 9n:line help. ,in"le step throu"h pro"rams. Continuousl# run pro"rams. %nterrupt 02 tri""ered b# a hardware timer +simulated-. C*U Cloc1 ,peed can be altered.

'eripherals
;e#board %nput Traffic 5i"hts ,e$en ,e"ment 4ispla# =eater and Thermostat ,na1e and 2a>e ,tepper 2otor 2emor# 2apped ?4U

01ample 'ro&rams
<<1e#b.asm <<tli"ht.asm <<se$se".asm <<hon.asm and <<hoff.asm <<sna1e.asm <<step.asm <<1e#b.asm

4ocumentation
9n:line h#perte.t help is stored in a @ebsite. %t is possible to cop# from the help pa"es and paste into a word processor or te.t editor pro"rams. Users ha$e permission to modif# help files for use b# students and to print and or ma1e multiple photocopies.

4isclaimer
This simulation software is not "uaranteed in an# wa#. %t ma# differ from realit#. %t mi"ht not e$en wor1 at all. Tr# it out and if #ou li1e it/ "o ahead and use it.

S/stem
(ontents

rchitecture

Simplified Simulator

rchitecture

central processin" unit +C*U28 b#tes of random access memor# +6)21 input output +%9- ports. 9nl# si. are used. ) hardware timer that tri""ers interrupt 02 at re"ular time inter$als that #ou can pre:set usin" the confi"uration tab. ) 1e#board that tri""ers interrupt 03. *eripherals connected to the *orts.

The simulator is pro"rammable in that #ou can run man# different pro"rams. %n real life/ the 6)2 would be replaced b# read onl# memor# +692- and the s#stem would onl# e$er run one pro"ram hard wired into the 692. There are hundreds of e.amples of s#stems li1e this controllin" traffic li"hts/ C4 pla#ers/ simple "ames consoles/ man# childrenAs "ames/ T? remote controls/ microwa$e o$en timers/ cloc1 radios/ car en"ine mana"ement s#stems/ central heatin" controllers/ en$ironmental control s#stems and the list "oes on.

The (entral 'rocessin& !nit


The central processin" unit is the BbrainB of the computer. )ll calculations/ decisions and data mo$es are made here. The C*U has stora"e locations called re&isters. %t has an arithmetic and lo"ic unit +)5U- where the processin" is done. 4ata is ta1en from the re"isters/ processed and results "o bac1 into the re"isters. 2o$e +29?- commands are used to transfer data between 6)2 locations and the

re"isters. There are man# instructions/ each with a specific purpose. This collection is called the instruction set.

General 'urpose Re&isters


The C*U has four "eneral:purpose re"isters called )5/ B5/ C5 and 45. These are ei"ht bits or one b#te wide. 6e"isters can hold unsi"ned numbers in the ran"e 0 to !288 and si"ned numbers in the ran"e 127 to !12C. These are used as temporar# stora"e locations. 6e"isters are used in preference to 6)2 locations because it ta1es a relati$el# lon" time to transfer data between 6)2 and the C*U. Daster computers "enerall# ha$e more C*U re"isters or memor# on the C*U chip. The re"isters are named )5/ B5/ C5 and 45 because the 1 :bit $ersion of this C*U has more re"isters called )=/ B=/ C= and 4=. The A5A means 5ow and the A=A means =i"h. These are the low and hi"h ends of the 1 :bit re"ister.

Special 'urpose Re&isters


The special purpose re"isters in the C*U are called %*/ ,6 and ,*.

I' is the Instruction pointer


This re"ister contains the address of the instruction bein" e.ecuted. @hen e.ecution is complete/ %* is increased to point to the ne.t instruction. Eump instructions alter the $alue of %* so the pro"ram flow &umps to a new position. C)55 and %NT also chan"e the $alue stored in %*. %n the 6)2 displa#s/ the instruction pointer is hi"hli"hted red with #ellow te.t.

SR is the Status Re&ister


This re"ister contains fla"s that report the C*U status. The The The The AFA >ero fla" is set to one if a calculation "a$e a >ero result. A,A si"n fla" is set to one if a calculation "a$e a ne"ati$e result. A9A o$erflow fla" is set if a result was too bi" to fit in a re"ister. A%A interrupt is set if interrupts are enabled. ,ee C5% and ,T%.

S' is the Stac* 'ointer


The stac1 is an area of memor# or"anised usin" the 5%D9 last in first out rule. The stac1 pointer points to the ne.t free stac1 location. The simulator stac1 starts at address BD &ust below the 6)2 used for the $ideo displa#. The stac1 "rows towards address >ero. 4ata is pushed onto the stac1 to sa$e it for later use. 4ata is popped off the stac1 when needed. The stac1 pointer ,* 1eeps trac1 of where to push or pop data items. %n the 6)2 displa#s/ the stac1 pointer is hi"hli"hted blue with #ellow te.t.

Random

ccess Memor/

The simulator has 28 b#tes of ram. The addresses are from 0 to 288 in decimal numbers or from G00H to GDDH in he.adecimal. 6)2 addresses are usuall# "i$en in s3uare brac1ets such as GCCH where CC is a he.adecimal number. 6ead GCCH as Bthe data stored at location CCB.

%usses
Busses are collections of wires used to carr# si"nals around the computer. The# are commonl# printed as parallel trac1s on circuit boards. ,lots are soc1ets that enable cards to be connected to the s#stem bus. )n 7:bit computer t#picall# has re"isters 7 bits wide and 7 wires in a bus. ) 1 :bit computer has 1 bit re"isters and 1 address and data wires and so on. The ori"inal %B2 *C had 7 data wires and 20 address wires enablin" one me"ab#te of 6)2 to be accessed. 32 bit re"isters and busses are now usual +1<<C:2003-.

4ata %us

The 4ata Bus is used to carr# data between the C*U/ 6)2 and %9 ports. The simulator has an 7:bit data bus.

ddress %us

The )ddress Bus is used to specif# what 6)2 address or %9 port should be used. The simulator has an 7:bit address bus.

The Control Bus This has a wire to determine whether to access 6)2 or %9 ports. %t also has a wire to determine whether data is bein" read or written. The C*U reads data when it flows into the C*U. %t writes data when it flows out of the C*U to 6)2 or the %9 ports.

(ontrol %us

The ,#stem Cloc1 wire carries re"ular pulses so that all the electronic components can function at the correct times. Cloc1 speeds between 100 and 200 million c#cles per second are t#pical +1<<C-. This is referred to as the cloc1 speed in 2=> or me"ahert>. The simulator runs in slow motion at about one instruction per second. This is ad&ustable o$er a small ran"e.

Hardware Interrupts

=ardware %nterrupts re3uire at least one wire. These enable the C*U to respond to e$ents tri""ered b# hardware such as printers runnin" out of paper. The C*U processes some machine code in response to the interrupt. @hen finished/ it continues with its ori"inal tas1. The %B2 *C has 1 interrupts controlled b# 0 wires.

To Install
(ontents

Important
D%6,T ma1e a bac1:up cop# of the distribution dis1 or downloaded file.

S/stem Re>uirements
,ms32?80.(.e re3uires @indows<8I<7INTI2000IJ*. ) mouse or other pointin" de$ice is hi"hl# recommended to access the h#perte.t help pa"es.

To Install
Kou donAt need to run setup. %n fact there is no setup pro"ram. 2a1e a folder for all the simulator files and cop# them from the distribution dis1 to this director#. %f #ou ha$e downloaded a Fip file/ un>ip it into this director#. Create a shortcut to sms32$80.e.e andIor create a shortcut on the start menu.

Su&&estion
%n a ,chool Colle"e or Uni$ersit# settin"/ the e.ercise files should be made a$ailable to students. These files are numbered as in this e.ample : 02tli"ht.asm and <<sna1e.asm. The other e.ample files demonstrate the capabilities of the simulator and are t#pical of the sorts of pro"rams students mi"ht write for assi"nments. 4(29.),2 could be made a$ailable. The other e.ample files should be 1ept b# the teacher for reference. .ote The on line help should be in the same director# as ,ms32?80.(.e

.etwor* Installation
The simulator has been desi"ned to run from a ser$er. %t does not need to be installed on wor1stations. This ma1es deplo#ment simple. 9ne da# all software will be li1e this and stressed out networ1 administrators will re$ert to bein" normal/ 1ind/ cheerful human bein"s. Create a read onl# folder on the ser$er and ma1e sure all users can access it. +Create a share or use a public dri$e-. Cop# all the files from the distribution into the folder on the ser$er. %f the# are >ipped/ un>ip them. *ro$ide a shortcut to sms32$80.e.e for users to start up the pro"ram. The shortcut should specif# a wor1in" director# where users ha$e write permission such as their home folder. Users need write access to their own home folder +a flopp# dis1 would be 9; too-. Users need to sa$e their wor1 to this folder +or flopp#-. %f the simulator starts in this director#/ it will sa$e an %N% file in the userAs director# that holds information about window positions and preferences. %f the %N% file is missin"/ the simulator will start with default settin"s and will attempt to create the %N% file. %f ,2, starts in a read onl# director#/ it will not be able to sa$e its %N% file. %f possible/ correct this non fatal problem.

To Run
%f #ou ha$e set up an icon/ double clic1 the icon or select the icon and press enter. Kou can double clic1 on ,ms32?80.e.e within @indows (.plorer or 2# Computer. Kou can dra" and drop an assembl# code file onto the pro"ram icon. )ssembl# code file names should end in .asm. Kou can drop an# t#pe of file into the simulator but the result is not li1el# to be useful.

To !n"Install
(ontents

Important
D%6,T ma1e a bac1:up cop# of the distribution dis1 or downloaded file.

To !n"install
The simulator is completel# self contained and can be deleted without upsettin" an# other applications. Usin" @indows (.plorer or 2# Computer or Networ1 Nei"hborhood +for a networ1 installation- delete all the files in the simulator director#. 6emo$e an# icons or other references to the simulator from #our s#stem. There are .: re"istr# entries or 455 files so the un:install is completel# clean.

# $ and %u&s
(ontents

%u&s and #eatures


=ere are some bu"s and features that % 1now about. %f #ou disco$er other problems/ please send me an 0 Mail. % will cure the eas# bu"s and list the hard ones here.

=h/ is m/ displa/ messed up?


The simulator was de$eloped usin" the default windows fonts/ colours and borders. ,ome combinations of colours/ fonts and window borders ha$e caused problems. The s#mptoms include in$isible te.t/ te.t that wonAt fit inside the window/ labels that donAt line up with the item the# are supposed to label and stretched bitmap ima"es that loo1 untid#. The cure is to use @indows default settin"s or compatible settin"s.

=here are m/ windows?


Be"inners and some e.perts mi"ht hit another problem. @ith the displa# in a hi"h resolution mode/ the simulator windows can be mo$ed towards the bottom ri"ht. @hen the displa# is restored to a lower resolution and the simulator is re:started/ all its windows will be off screen where tha# canAt be seen or controlled. The cure is to close the simulator and delete ,ms32?80.%N%. The simulator will restart with its windows in default $isible positions.

=h/ won@t this file sa5e? ASms32550.IniA


This is common on a networ1 installation. 2a1e sure the wor1in" director# is one that #ou ha$e permission to write to.

=h/ can@t I sa5e m/ wor*?


2a1e sure #ou are sa$in" to a folder or director# where #ou ha$e write permission. This problem usuall# occurs when #ou are runnin" the simulator from a networ1 installation.

'( Support Hand)oo*


(ontents

The '( Support Hand)oo*


This is an e.cellent boo1 for an#one learnin" about personal computer hardware. 4etails are at http://www.dum)rec*.demon.co.u*/ This boo1 co$ers *C architecture in some detail and ma1es e.cellent further readin" in con&unction with the use of this simulator. The boo1As %,BN reference is 0<801C11:1:J and it is a$ailable in the followin" wa#sL Drom all ma&or boo1 distributors. Drom the Maplin chain of stores or 2aplin mailIweb order. 4irectl# from the publishers/ 4umbrec1 *ublishin". This is often the 3uic1est method.

*lease "et up:to:date contact details from their we)site.

'( Support Hand)oo* (ontents


Computer Basics ,oftware M 4ata 9peratin" ,#stems Numberin" ,#stems Computer )rchitecture 4ispla# Technolo"# Computer 2emor# 4iscs M 4ri$es Computer *eripherals ,#stem ,election =ardware %nstallation *.C. Confi"uration @indows Confi"uration *.C. ,upport Daultfindin" Computer ,ecurit# 4ata Communications 5ocal )rea Networ1s The %nternet Creatin" @ebsites 2ultimedia

!sin& the Simulator " Gettin& Started


(ontents

:n ,ine Help
*ress the #- 1e# to "et on line help.

=ritin& a 'ro&ram
To write and run a pro"ram usin" the simulator/ select the source code editor tab b# pressin" lt+!. T#pe in #our pro"ram. %t is best to "et small parts of the pro"ram wor1in" rather than t#pin" it all in at once. =ere is a simple e.ample. )lso loo1 at the tutorial e.ample pro"rams. Kou can t#pe this into the simulator or cop# and paste it. The assembl# code has been annotated with comments that e.plain the code. These comments are i"nored b# the assembler pro"ram. Comments be"in with a semicolon and continue to the end of the line.

; ===== COUNT ================================================= MOV AL,0 ; Move 0 into the AL register REP: ; This label is se! "ith # $% &o$$an!s A'' AL,( ; A!! t"o to AL )MP REP ; ) $% ba&* to the re% label EN' ; Progra$ en!s here ; =============================================================

Runnin& a 'ro&ram
To run a pro"ram/ #ou can step throu"h it one line at a time b# pressin" clic1in" this button repeatedl#. Kou can run a pro"ram continuousl# b# pressin" #< or lt+' or b#

lt+R or b# pressin" this button lt+, or lt+T

To speed up or slow down a runnin" pro"ram use these buttons or t#pe To stop a runnin" pro"ram press

lt+: or clic1 or press 0scape or press this button. lt+. or clic1 this lt+0 or clic1 this

To restart a paused pro"ram/ continuin" from where it left off/ press button. To restart a pro"ram from the be"innin"/ reset the C*U b# pressin" button. To re:open the 6)2 displa# window/ press lt+M or clic1 this button.

ssem)l/ (ode
The code #ou t#pe is called assembl# code. This human:readable code is translated into machine code b# the ssem)ler. The machine code +binar#- is understood b# the C*U. To assemble a pro"ram/ press lt+ or clic1 this button. Kou can see an animation of the assembler process b# chec1in" this bo.. @hen #ou run or setp a pro"ram/ if necessar#/ the code is assembled.

ssem)ler 'hases
There is short dela# while the assembbler "oes throu"h all the sta"es of assemblin" the pro"ram. The steps are

1. Sa5e the source code.


2. Con$ert the source code into to*ens +this simulator uses human readable to1ens for educational $alue rather than efficienc#-.

3. 'arse the source code and +if necessar#- "enerate error messa"es. %f there are no errors/
"enerate the machine codes. This process could be coded more efficientl#. %f the to1ens representin" machine op codes li1e 29? and E2* were numerical/ the assembler could loo1 up the machine code e3ui$alents in an arra# instead of plou"hin" throu"h man# if:then:else statements. 9nce a"ain/ this has been done to demonstrate the process of assemblin" code for educational reasons.

4. (alculate Bumps/ the distances of the &umpIbranch instructions.

Viewin& Machine (ode


The machine code stored in 6)2 can be $iewed in three modes b# selectin" the appropriate radio button.

He1adecimal : This displa# corresponds e.actl# to the binar# e.ecuted b# the C*U. S(II : This displa# is con$enient if #our pro"ram is processin" te.t. The te.t is readable but the machine codes are not. Source (ode : This displa# shows how the assembl# code commands are placed in memor#.

Tutorial 01amples
The tutorial e.amples pro$ide a step b# step introduction to the commands and techni3ues of low le$el pro"rammin". (ach pro"ram has one or more learnin" tas1s associated with it. ,ome of the tas1s are simple. ,ome are real brain teasers.

,earnin& Tas*s
(ontents

The Tas*s
=ere are all the learnin" tas1s "rouped to"ether with pointers to the e.ample pro"rams and e.planator# notes.

Simple

rithmetic

(.ample : 0-first.asm : )rithmetic 1. @rite a pro"ram that subtracts usin" ,UB 2. @rite a pro"ram that multiplies usin" 2U5 3. @rite a pro"ram that di$ides usin" 4%? 0. @rite a pro"ram that di$ides b# >ero. 2a1e a note to a$oid doin" this in real life.

!sin& He1adecimal
(.ample : 02tli&ht.asm : Traffic 5i"hts 8. Use the help pa"e on =e.adecimal and Binar# numbers. @or1 out what he.adecimal numbers will acti$ate the correct traffic li"hts. 2odif# the pro"ram to step the li"hts throu"h a realistic se3uence.

S(II (odes
(.ample : 03mo5e.asm . 5oo1 up the ),C%% codes of =/ (/ 5/ 5 and 9 and cop# these $alues to memor# locations C0/ C1/ C2/ C3 and C0. This is a simple and somewhat crude wa# to displa# te.t on a memor# mapped displa#.

(ountin& and Cump (ommands


(.ample : 06incBmp.asm C. 6ewrite the e.ample pro"ram to count bac1wards usin" 4(C B5. 7. 6ewrite the e.ample pro"ram to count in threes usin" )44 B5/3. <. 6ewrite the pro"ram to count 1 2 0 7 1 usin" 2U5 B5/2 10. =ere is a more difficult tas1. Count 0 1 1 2 3 8 7 13 21 30 88 <7 o$erflow. =ere each number is the sum of the pre$ious two. Kou will need to use two re"isters and two 6)2 locations for temporar# stora"e of numbers. %f #ou ha$e ne$er pro"rammed before/ this is a real brain teaser. 6emember that the result will o$erflow when it "oes abo$e 12C. This number se3uence was first described b# 5eonardo Dibonacci of *isa +11C0N1230-

(haracter Input :utput


(.ample : 05*e/)in.asm 11. (as#O %nput characters and displa# each character at the top left position of the ?4U b# cop#in" them all to address GC0H. 12. =arder Use B5 to point to address GC0H and increment B5 after each 1e# press in order to see the te.t as #ou t#pe it. 13. =arderO ,tore all the te.t #ou t#pe in 6)2 when #ou t#pe it in. @hen #ou press (nter/ displa# the stored te.t on the ?4U displa#. 10. 4ifficult T#pe in te.t and store it. @hen (nter is pressed/ displa# it on the ?4U screen in re$erse order. Usin" the stac1 ma1es this tas1 easier

'rocedures
(.ample : 08proc.asm 18. 6e:do the traffic li"hts pro"ram and use this procedure to set up realistic time dela#s. 02tli"ht.asm 1 . 6e:do the te.t input and displa# pro"ram with procedures. Use one procedure to input the te.t and one to displa# it.

Te1t I: and 'rocedures


(.ample : 09te1tio.asm 1C. @rite a pro"ram usin" three procedures. The first should read te.t from the 1e#board and store it in 6)2. The second should con$ert an# upper case characters in the stored te.t to lower case. The third should displa# the te.t on the ?4U screen.

4ata Ta)les
(.ample : 0;ta)le.asm 17. %mpro$e the traffic li"hts data table so there is an o$erlap with both sets of li"hts on red. 1<. Use a data table to na$i"ate the sna1e throu"h the ma>e. This is on port 00. ,end DD to the sna1e to reset it. Up/ down left and ri"ht are controlled b# the left four bits. The ri"ht four bits control the distance mo$ed. 20. @rite a pro"ram to spin the stepper motor. )cti$ate bits 1/ 2/ 0 and 7 in se3uence to ener"ise the electroma"nets in turn. The motor can be half stepped b# turnin" on pairs of ma"nets followed b# a sin"le ma"net followed b# a pair and so on. 21. Use a data table to ma1e the motor perform a comple. se3uence of forward and re$erse mo$es. This is the t#pe of control needed in robotic s#stems/ printers and plotters. Dor this e.ercise/ it does not matter e.actl# what the motor does.

'arameters
(.ample : 0<param.asm 22. @rite a procedure that doubles a number. *ass the sin"le parameter into the procedure usin" a re"ister. Use the same re"ister to return the result. 23. @rite a procedure to in$ert all the bits in a b#te. )ll the >eros should become ones. )ll the ones should become >eros. *ass the $alue to be processed into the procedure usin" a 6)2 location. 6eturn the result in the same 6)2 location. 20. @rite a procedure that wor1s out Dactorial N. This e.ample shows one method for wor1in" out factorial N. Dactorial 8 is 8 P 0 P 3 P 2 P 1 Q 120. Kour procedure should wor1 properl# for factorial 1/ 2/ 3/ 0 or 8. Dactorial would cause an o$erflow. Use the stac1 to pass parameters and return the result. Calculate the result. Usin" a loo1 up table is cheatin"O 28. @rite a procedure that wor1s out Dactorial N. Use the stac1 for parameter passin". @rite a recursi$e procedure. Use this definition of Dactorial. Dactorial + 0 - is defined as 1. Dactorial + N - is defined as N P Dactorial +N : 1-. To wor1 out Dactorial +N-/ the procedure first tests to see if N is >ero and if not then re:uses itself to wor1 out N P Dactorial +N : 1-. This problem is hard to understand in an# pro"rammin" lan"ua"e. %n assembl# code it is harder still.

Software Interrupts
(.ample : -0swint.asm 2 . The simulated 1e#board "enerates %NT 03 e$er# time a 1e# is pressed. @rite an interrupt 03 handler to process the 1e# presses. Use %N 0C to fetch the pressed 1e# into the )5 re"ister. The ori"inal %B2 *C allocated 1 b#tes for 1e# press stora"e. The 1 locations are used in a circular buffer fashion. Tr# to implement this. 2C. Build on tas1 2 b# putin" characters onto the ne.t free screen location. ,ee if #ou can "et correct beha$iour in response to the (nter 1e# bein" pressed +fairl# eas#- and if the Bac1 ,pace 1e# bein" pressed +harder-.

Hardware Interrupts
(.ample : --hwint.asm 27. @rite a pro"ram that controls the heater and thermostat whilst at the same time countin" from 0 to < repeatedl#/ displa#in" the result on one of the se$en se"ment displa#s. %f #ou want a harder challen"e/ count from 0 to << repeatedl# usin" both displa#s. Use the simulated hardware interrupt to control the heater and thermostat.

2<. ) fiendish problem. ,ol$e the Tower of =anoi problem whilst steerin" the sna1e throu"h the ma>e. Use the te.t characters )/ B/ C (tc. to represent the dis1s. Use three of the four rows on the simulated screen to represent the pillars. % am not aware of an#one ha$in" sol$ed the tower of =anoi +includin" me-/ let alone controllin" the sna1e at the same time.

30. Use the 1e#board on *ort 0C. @rite an interrupt handler +%NT 03- to process the 1e# presses. Kou must also process %NT 02 +the hardware timer- but it need not perform an# tas1. Dor a more ad$anced tas1/ implement a 1 b#te circular buffer. @rite code to place the buffered te.t on the ?4U screen when #ou press (nter. Dor an e$en harder tas1/ implement code to process the Bac1space 1e# to delete te.t characters in the buffer.

01ample " 0-first.asm "


(ontents

rithmetic

2ost of these e.amples include a learnin" tas1. ,tud# the e.ample and if #ou can complete the tas1Is/ it is li1el# that #our understandin" is "ood.

01ample " 0-first.asm


; ===== +OR, OUT ( PLU- ( ====================================== CLO ; Close n"ante! "in!o"s. MOV AL,( ; Co%/ a ( into the AL register. MOV 0L,( ; Co%/ a ( into the 0L register. A'' AL,0L ; A!! AL to 0L. Ans"er goes into AL. EN' ; Progra$ en!s ; ===== Progra$ En!s =========================================== 1OUR TA-, ========= Use -U0, '2V an! MUL to s btra&t, !ivi!e an! $ lti%l/. +hat ha%%ens i3 /o !ivi!e b/ 4ero5 Ma*e se o3 CL an! 'L as "ell as AL an! 0L. T#pe this code into the simulator editor :R cop# and paste the code :R load the e.ample from dis1. ,tep throu"h the pro"ram b# pressin" lt+' repeatedl#.

@hile #ou are steppin"/ watch the C*U re"isters. Kou should see a A2A appear in the )5 re"ister followed b# a A2A in the B5 re"ister. )5 should be added to B5 and a A0A should appear in )5. The altered re"isters are hi"hli"hted #ellow. @atch the re"ister labelled %* +%nstruction *ointer-. This re"ister 1eeps trac1 of where the processor has "ot to in the pro"ram. %f #ou loo1 at the 6)2 displa#/ one 6)2 location is labelled with a red blob. This corresponds to the %nstruction *ointer. Note how the red blob +%*- mo$es when #ou step the pro"ram. @hen doin" the learnin" e.ercises/ add to and modif# #our own cop# of the e.ample.

=hat /ou need to *now


)n# te.t after a semicolon is not part of the pro"ram and is i"nored b# the simulator. These comments are used for e.planations of what the pro"ram is doin". 'ood pro"rammers ma1e e.tensi$e use of comments. 'ood comments should not &ust repeat the code. 'ood comments should e.plain wh/ thin"s are be"in done.

(omments

(,:

The C59 command is uni3ue to this simulator. %t closes an# window that is not needed while a pro"ram is runnin". This command ma1es it easier to write nice demonstration pro"rams. %t also a$oids ha$in" to close se$eral windows manuall#.

M:V

The 29? command is short for 2o$e. %n this e.ample numbers are bein" copied into re"isters where arithmetic can be done. 29? copies data from one location to another. The data in the ori"inal location is left intact b# the 29? command. 2o$e was shortened to 2o$ because/ in olden times/ computer

memor# was fiendishl# e.pensi$e. ($er# command was shortened as much as possible/ much li1e the mobile phone te.tin" lan"ua"e used toda#.

rithmetic
The add command comes in two $ersions. =ere are two e.amples 44 44 ,D%, : )dd B5 to )5 and store the result into )5 ,D5 : )dd 8 to )5 and store the result into )5

44

5oo1 at the on:line help to find out about ,UB/ 2U5 and 4%?. 6emeber that #ou can access on:line help b# pressin" the #- 1e#.

6e"isters are stora"e locations where 7 bit binar# numbers are stored. The central processin" unit in this simulator has four "eneral purpose re"isters called )5/ B5/ C5 and 45. These re"isters are interchan"eable and can/ with a few e.ceptions/ be used for an# purpose.

Re&isters

Newer central processin" unit +C*U- chips ha$e 1 / 32 or e$en 0 bit re"isters. These wor1 in the same wa# but more data can be mo$ed in one step so there is a speed ad$anta"e. @ider re"isters can store lar"er inte"er +whole- numbers. This simplifies man# pro"rammin" tas1s. The other three re"isters ,*/ %* and ,6 are described later.

He1adecimal .um)er s 0.4

%n the command 29? )5/2 the 2 is a he.adecimal number. The he.adecimal number s#stem is used in low le$el pro"rammin" because there is a $er# con$enient con$ersion between binar# and he.. ,tud# the He1adecimal and %inar/ number s#stems.

The last command in all pro"rams should be (N4. )n# te.t after the (N4 1e#word is i"nored.

Eour Tas*s
Use all the re"isters )5/ B5/ C5 and 45 and e.periment with )44/ ,UB/ 2U5 and 4%?. Dind out what happens if #ou tr# to di$ide b# >ero.

01ample " <<nast/.asm " .ast/


(ontents

This e.ample shows how #ou can create totall# unreadable code. Tr# not to do this. This pro"ram actuall# wor1s. Cop# it and paste it into the simulator and tr# itO Clic1 the 5ist:Dile tab to see the code laid out better and to see the addresses where the code is stored. To "et bac1 to the editor window clic1 the ,ource:Code tab.

01ample " <<nast/.asm


; 66666 7ere is ho" NOT to "rite a %rogra$ 66666 8: Mov 0L,C0 Mov AL,9C :: Mov ;0L<,AL CMP AL,=0 )> > 2NC AL 2NC 0L )MP : >: MOV CL,?0 MOV AL,(0 MOV 0L,C0 1: MOV ;0L<,AL 2NC 0L 'EC CL )N> 1 )MP 8 EN' ; Loo* at the list 3ile. 2t &o$es o t O,@ ; Press Es&a%e to sto% the %rogra$ r nning. ; 6666666666666666666666666666666666666666666666

Here it is tidied up
; ; ; ; 66666 66666 66666 66666 A Progra$ to !is%la/ A-C22 7ere it is ti!ie! %. This This $a*es it %ossible to The labels have been given Mov 0L,C0 Mov AL,9C &hara&ters 66666666666666666 version is annotate!. 666666 n!erstan!. 66666666666666666 $ore rea!able na$es too. 666

-tart: 7ere:

; Ma*e 0L %oint to vi!eo RAM ; 9C is the A-C22 &o!e o3 the Aless thanA s/$bol ; Co%/ the A-C22 &o!e in AL to the RAM lo&ation that 0L is ; ; ; ; ; Co$%are AL "ith ABA 23 AL &ontaine! ABA # $% to Clear: P t the neCt A-C22 &o!e into AL Ma*e 0L %oint to the neCt vi!eo RAM lo&ation ) $% ba&* to 7ere

Mov ;0L<,AL %ointing to. CMP AL,=0 )> Clear 2NC AL 2NC 0L )MP 7ere Clear: MOV CL,?0 MOV AL,(0 MOV 0L,C0 Loo%: MOV ;0L<,AL %ointing to. 2NC 0L 'EC CL )N> Loo% )MP -tart

; +e are going to re%eat ?0 DheCE ti$es ; The A-C22 &o!e o3 the s%a&e &hara&ter ; The a!!ress o3 the start o3 vi!eo RAM ; Co%/ the A-C22 s%a&e in AL to the vi!eo RAM that 0L is ; ; ; ; Ma*e 0L %oint to the neCt vi!eo RAM lo&ation CL is &o nting !o"n to"ar!s 4ero 23 CL is not 4ero # $% ba&* to Loo% CL "as 4ero so # $% ba&* to the -tart an! !o it all again.

EN' ; 6666666666666666666666666666666666666666666666666666666666666

Eour Tas*
@rite all #our future pro"rams ... with "ood la#out with meanin"ful label names with useful comments that 0F', I. the code a$oidin" comments that state the totall# ob$oius and &ust repeat the code

%ad (omment " Bust repeats the code


%NC B5 R )dd one to B5

!seful (omment " e1plains wh/ the step is needed


%NC B5 R 2a1e B5 point to the ne.t $ideo 6)2 location

01ample " 02tli&ht.asm " Traffic ,i&hts


(ontents

01ample " 02tli&ht.asm


; ===== CONTROL T7E TRAFF2C L2G7T- ============================= -tart: CLO MOV AL,0 OUT 0H MOV AL,FC OUT 0H )MP -tart EN' ; Close ; ; ; ; ; ; ; ; n"ante! "in!o"s.

T rn o33 all the tra33i& lights. Co%/ 00000000 into the AL register. -en! AL to Port One DThe tra33i& lightsE. T rn on all the tra33i& lights. Co%/ HHHHHH00 into the AL register. -en! AL to Port One DThe tra33i& lightsE. ) $% ba&* to the start. Progra$ en!s.

; ===== Progra$ En!s ========================================== 1OUR TA-, ========= Use the hel% %age on 7eCa!e&i$al an! A-C22 &o!es. +or* o t "hat heCa!e&i$al n $bers "ill a&tivate the &orre&t tra33i& lights. Mo!i3/ the %rogra$ to ste% the lights thro gh a realisti& seI en&e.

To run the pro"ram press the ,tep button repeatedl# or press the 6un button. To stop the pro"ram/ press ,top. @hen the pro"ram is runnin"/ clic1 the 6)2:,ource or 6)2:=e. or 6)2:),C%% tabs. These "i$e alternati$e $iews of the contents of random access memor# +6)2-. )lso clic1 the 5ist Dile tab to see the machine code "enerated b# the simulator and the addresses where the codes are stored.

'orts
The traffic li"hts are connected to port one. %ma"ine this as a soc1et on the bac1 of the processor bo.. 4ata sent to port one "oes to the traffic li"hts and controls them. There are si. lamps to control. 6ed/ )mber and 'reen for a pair of li"hts. This can be achie$ed with a sin"le b#te of data where two bits are unused. B# settin" the correct bits to 9ne/ the correct lamps come on.

Dill in the rest of this table to wor1 out the =e.adecimal $alues #ou need. 9f course #ou need to 1now the se3uence of li"hts in #our countr#. Red 1 m)er Green Red 0 0 0 m)er Green 0 1 .ot .ot He1 used used 0 0 70

=hat /ou need to *now


5abels mar1 positions that are used b# Eump commands. )ll the commands in this pro"ram are repeated for e$er or until ,top is pressed. 5abel names must start with a letter or N character. 5abel names must not start with a di"it. The line

,a)els and Cumps

E2* ,tart causes the pro"ram to &ump bac1 and re:do the earlier commands. 4estination labels end in a colon. Dor e.ample ,tartL

(ontrollin& the ,i&hts

%f #ou loo1 carefull# at the traffic li"hts displa#/ #ou can see which bit controls each li"ht bulb. @or1 out the pattern of nou"hts and ones needed to turn on a sensible set of bulbs. Use the =e.adecimal and Binar# numbers table to wor1 out the he.adecimal e3ui$alent. 2o$e this he.adecimal number into )5. 9UT 01 This command copies the contents of the )5 re"ister to 9utput *ort 9ne. The traffic li"hts are connected to port one. ) binar# one causes a bulb to switch on. ) nou"ht causes it to turn off.

01ample " 03mo5e.asm " 4ata Mo5es


(ontents

01ample " 03mo5e.asm


; 666666666666666666666666666666666666666666666666666666666666666 ; A %rogra$ to !e$onstrate MOV &o$$an!s. Mov is short 3or $ove. ; 666666666666666666666666666666666666666666666666666666666666666 CLO ; Close n"ante! "in!o"s. ; ===== 2MME'2ATE MOVE- ===== MOV AL,HJ ; Co%/ HJ 7EK into the AL register MOV 0L,?0 ; Co%/ ?0 7EK into the 0L register MOV CL,J0 ; Co%/ J0 7EK into the CL register MOV 'L,L0 ; Co%/ L0 7EK into the 'L register Foo: 2NC AL ; 2n&re$ent AL 3or no %arti& lar reason. ; ===== 2N'2RECT MOVE- ===== MOV ;A0<,AL ; Co%/ val e in AL to RAM lo&ation ;?0< MOV 0L,;?0< ; Co%/ val e in RAM lo&ation ;A0< into 0L ; ===== REG2-TER 2N'2RECT MOVE- ===== MOV ;CL<,AL ; Co%/ the val e in AL to the RAM ; lo&ation that CL %oints to. MOV 0L,;CL< ; Co%/ the RAM lo&ation that CL %oints ; to into the 0L register. )MP Foo ; PRE-- E-CAPE TO -TOP T7E PROGRAM

EN' ; 666666666666666666666666666666666666666666666666666666666666666 TA-, ==== Loo* % the A-C22 &o!es o3 the letters in 7,E,L,L,O an! $ove these A-C22 &o!es to RAM a!!resses ;C0<, ;CH<, ;C(<, ;C9< an! ;C?<. R n the %rogra$ an! "at&h ho" the teCt a%%ears on the si$ late! V'U !is%la/. This is ver/ $ &h the sa$e as "hat ha%%ens in the 20M PC r nning M- 'O-. The %rogra$ /o "rite sho l! "or* b t i3 /o &ontin e to st !/ lo" level %rogra$$ing, /o "ill 3in! $ &h $ore e33i&ient an! 3leCible "a/s o3 solving this %roble$. ,tep throu"h the pro"ram and watch the re"ister $alues chan"in". %n particular/ loo1 at the 6)2: =e. displa# and note the wa# that $alues in 6)2 chan"e. )ddresses G80H and G)0H are altered. Kou can cop# the e.ample pro"ram from the help pa"e and paste it into the source code editor.

ddresin& Modes
There are se$eral 44R0SSI.G M:40S a$ailable with mo$e commands.

Immediate
29? 29? 29? 29?

ddressin&

) he.adecimal number is copied into a re"ister. 01amples... )5/18 R Cop# 18 =(J into the )5 re"ister B5/00 R Cop# 00 =(J into the B5 re"ister C5/80 R Cop# 80 =(J into the C5 re"ister 45/ 0 R Cop# 0 =(J into the 45 re"ister

Indirect

ddressin&

) $alue is mo$ed to or from 6)2. The ram address is "i$en as a number li1e G22H in s3uare brac1ets. 01amples... 29? G)0H/)5 R Cop# $alue in )5 to 6)2 location G00H 29? B5/G00H R Cop# $alue in 6)2 location G)0H into B5

Re&ister Indirect

ddressin&

Cop# a $alue from 6)2 to a re"ister or cop# a $alue from a re"ister to 6)2. The 6)2 address is contained in a second re"ister enclosed in s3uare brac1ets li1e this GC5H. 01amples ... 29? GC5H/)5 R Cop# the $alue in )5 to the 6)2 location that C5 points to. 29? B5/GC5H R Cop# the 6)2 location that C5 points to into the B5 re"ister.

Re&ister Mo5es
Not a$ailable in this simulation. ) re"ister mo$e loo1s li1e this 29? )5/B5 To do this usin" simulator commands/ use *U,= B5 *9* )5 *ush and *op are e.plained later.

(alculated

ddresses

Not a$ailable in this simulator. Cop# a $alue from 6)2 to a re"ister or cop# a $alue from a re"ister to 6)2. The 6)2 address is contained in s3uare brac1ets and is calculated. This is done to simplif# access to record structures. Dor e.ample a surname mi"ht be stored 12 b#tes from the start of the record. This techni3ue is shown in the e.amples below. 29? GC5 ! 8H/)5 R Cop# the $alue in )5 to the 6)2 location that C5 ! 8 points to. 29? B5/GC5 ! 12H R Cop# the 6)2 location that C5 ! 12 points to into the B5 re"ister.

Implied

ddresses

Not a$ailable in this simulator. %n this case/ memor# locations are named. )ddress G80H mi"ht be called Apupp#A. This means that mo$es can be pro"rammed li1e this. 29? )5/pupp# R Cop# the $alue in 6)2 at position pupp# into the )5 re"ister. 29? pupp#/B5 R Cop# B5 into the 6)2 location that pupp# refers to.

01ample " 06IncCmp.asm " (ountin&


(ontents

01ample " 06IncCmp.asm


; ===== Co nting =================================== MOV Re%: 2NC )MP EN' 0L,?0 0L Re% ; 2nitial val e store! in 0L ; ) $% ba&* to this label ; A!! ONE to 0L ; ) $% ba&* to Re% ; Progra$ En!s

; ===== Progra$ En!s =============================== TA-, ===== Re"rite the %rogra$ to &o nt ba&*"ar!s Re"rite the %rogra$ to &o nt in threes Re"rite the %rogra$ to &o nt H ( ? M HL sing 'EC 0L. sing A'' 0L,9. sing MUL 0L,(

7ere is a $ore !i33i& lt tas*. Co nt 0 H H ( 9 J M H9 (H 9? JJ NM over3lo". 7ere ea&h n $ber is the s $ o3 the %revio s t"o. 1o "ill nee! to se registers or RAM lo&ations 3or te$%orar/ storage o3 the n $bers. 23 /o have never %rogra$$e! be3ore, this is a real brain teaser. Re$e$ber that the res lt "ill over3lo" "hen it goes above H(=. This n $ber seI en&e "as 3irst !es&ribe! b/ Leonar!o Fibona&&i o3 Pisa DHH=08H(90E The pro"ram counts up in steps of one until the total is too bi" to be stored in a sin"le b#te. )t this point the calculation o$erflows. @atch the $alues in the re"isters. %n particular/ watch %* and ,6. These are e.plained below. )lthou"h this pro"ram is $er# simple/ some new ideas are introduced.

M:V %,D60
This line initialises B5 to 00.

Rep:
6epL is a label. 5abels are used with Eump commands. %t is possible for pro"rams to &ump bac1wards or forwards. Because of the wa# numbers are stored/ the lar"est &umps are :127 bac1wards and ! 12C forwards. 5abels must be"in with a letter or the N character. 5abels ma# contain letters/ di"its and the N character. 4estination labels must end with a ColonL

I.( %,
This command adds one to B5. @atch the B5 re"ister. %t will count up from 00 in he.adecimal so after 0< comes 0)/ 0B/ 0C/ 04/ 0(/ 0D/ 80/ 81 and so on.

:5erflow
@hen B5 reaches CD he. or 12C in decimal numbers the ne.t number ou"ht to be 127 but because of the wa# numbers are stored in binar#/ the ne.t number is minus 127. This effect is called an :V0R#,:=.

Status Re&ister GSRH


The status re"ister labelled ,6 contains four fla" bits that "i$e information about the state of the C*U. There are three fla"s that indicate whether a calculation o$erflowed/ "a$e a ne"ati$e result or "a$e a >ero result. Calculations set these fla"s , The si"n fla" indicates a ne"ati$e result. 9 The o$erflow fla" indicates o$erflows. F The >ero fla" indicates a >ero result. % %nterrupts enabled. ,T% turns this on. C5% turns this off.

These fla"s are described in more detail later.

CM' Rep
This command causes the central processin" unit +C*U- to &ump bac1 and repeat earlier commands or &ump forward and s1ip some commands.

Instruction 'ointer GI'H


The instruction pointer labelled %* contains the address of the instruction bein" e.ecuted. This is indicated b# a red hi"hli"hted 6)2 position in the simulator. (ach C*U command causes the %* to be increased b# 1/ 2 or 3 dependin" on the si>e of the command. %n the 6)2 displa#s/ the instruction pointer is hi"hli"hted red with #ellow te.t. NOP 2NC 0L A'' AL,0L )MP Re% ; ; ; ; ; 2n&rease 2P b/ H 2n&rease 2P b/ ( 2n&rease 2P b/ 9 A!! or s btra&t a val e 3ro$ 2P to # $% to a ne" %art o3 the %rogra$.

#etch 01ecute (/cle


#etch the instruction. %* points to it. This is called the operator. %f necessar#/ fetch data. %* ! 1 points to it. This is the first operand. %f necessar#/ fetch data. %* ! 2 points to it. This is the second operand. 01ecute the command. This ma# in$ol$e more fetchin" or puttin" of data. Increase I' to point to the ne.t command or calculate I' for Eump commands. Repeat this c#cle. ($er# machine c#cle has one operator or instruction. There could be >ero/ one or two operands dependin" on the instruction. 9* Codes are the machine codes that correspond to the operators and operands.

01ample " 05*e/)"in.asm " 7e/)oard Input


(ontents

01ample " 05*e/)"in.asm

; 66666666666666666666666666666666666666666666666666666666666666 ; 2n% t *e/ %resses 3ro$ the *e/boar! ntil Enter is %resse!. ; 66666666666666666666666666666666666666666666666666666666666666 CLO ; Close n"ante! "in!o"s. Re%: 2N 00 ; +ait 3or *e/ %ress 6 -tore it in AL. CMP AL,0' ; +as it the Enter *e/5 DA-C22 0'E )N> Re% ; No 6 # $% ba&*. 1es 6 en!. EN' ; 66666666666666666666666666666666666666666666666666666666666666 TA-, HHE H(E H9E H?E Eas/@ 'is%la/ ea&h &hara&ter at the to% le3t %osition o3 the V'U b/ &o%/ing the$ all to a!!ress ;C0<. 7ar!er Use 0L to %oint to a!!ress ;C0< an! in&re$ent 0L a3ter ea&h *e/ %ress in or!er to see the teCt as /o t/%e it. 7ar!er@ +hen /o -tore all the teCt /o t/%e in RAM "hen /o t/%e it in. %ress Enter, !is%la/ the store! teCt on the V'U !is%la/.

'i33i& lt T/%e in teCt an! store it. +hen Enter is %resse!, !is%la/ it on the V'U s&reen in reverse or!er. Using the sta&* $a*es this tas* easier.

Kou can cop# this e.ample pro"ram from the help pa"e and paste it into the source code editor.

I. 00
%nput from port >ero. %n this simulator/ port >ero is wired to the 1e#board hardware. The simulator waits for a 1e# press and copies the ),C%% code of the 1e# press into the )5 re"ister. This is not $er# realistic but is eas# to pro"ram. There is a more realistic 1e#board on port 0C and interrupt 03 but this is for more ad$anced pro"rammers.

(M'

,D04

Compare the )5 re"ister with the ),C%% code of the (nter 1e#. The ),C%% code of the (nter 1e# is 04he.. C2* )5/B5 wor1s as follows. The processor calculates )5 : B5. %f the result is >ero/ the AFA fla" in the status re"ister ,6 is set. %f the result is ne"ati$e/ the A,A fla" is set. %f the result is positi$e/ no fla"s are set. The AFA fla" is set if )5 and B5 are e3ual. The A,A fla" is set if B5 is "reater then )5. No fla" is set if )5 is "reater than B5.

C.I Rep
ENF stands for Eump Not Fero. Eump if the AFA fla" is not set. The pro"ram will &ump forwards or bac1 to the address that 6ep mar1s. ) related command is EF. This stands for Eump Fero. Eump if the >ero fla" is set. %n this pro"ram/ the C2* command sets the fla"s. )rithmetic commands also set the status fla"s.

M:V J(0KD ,
This will cop# )5 to address GC0H. The $isual displa# unit wor1s with addresses GC0H to GDDH. This "i$es a displa# with 0 rows and 1 columns. )ddress GC0H is the top left corner of the screen.

M:V J%,KD ,
This copies )5 to the address that B5 points to. B5 can be made to point to the ?4U screen at GC0H b# usin" 29? B5/C0. B5 can be made to point to each screen position in turn b# usin" %NC B5. This is needed for tas1 2.

01ample " 08proc.asm " 'rocedures


(ontents

01ample " 08proc.asm


; 666666666666666666666666666666666666666666666666666666666666666 ; ; ; ; ; ; ; ; A general % r%ose ti$e !ela/ %ro&e! re. The !ela/ is &ontrolle! b/ the val e in AL. +hen the %ro&e! re ter$inates, the CPU registers are restore! to the sa$e val es that "ere %resent be3ore the %ro&e! re "as &alle!. P sh, Po%, P sh3 an! Po%3 are se! to a&hieve this. 2n this eCa$%le one %ro&e! re is re6 se! three ti$es. This re6 se is one o3 the $ain a!vantages o3 sing %ro&e! res.

;666666 The Main Progra$ 6666666666666666666666666666666666666666 -tart: MOV AL,M ; A short !ela/. CALL 90 ; Call the %ro&e! re at a!!ress ;90< MOV CALL MOV CALL )MP -tart AL,H0 90 AL,(0 90 ; A $i!!le si4e! !ela/. ; Call the %ro&e! re at a!!ress ;90< ; A Longer !ela/. ; Call the %ro&e! re at a!!ress ;90<

; ) $% ba&* to the start.

; 66666 Ti$e 'ela/ Pro&e! re -tore! At A!!ress ;90< 6666666666666 ORG 90 ; Generate $a&hine &o!e 3ro$ a!!ress ;90< PU-7 PU-7F Re%: 'EC )N> POPF POP AL AL REP AL ; -ave AL on the sta&*. ; -ave the CPU 3lags on the sta&*. ; - btra&t one 3ro$ AL. ; ) $% ba&* to Re% i3 AL "as not >ero. ; Restore the CPU 3lags 3ro$ the sta&*. ; Restore AL 3ro$ the sta&*.

RET ; Ret rn 3ro$ the %ro&e! re. ; 666666666666666666666666666666666666666666666666666666666666666 EN' ; 666666666666666666666666666666666666666666666666666666666666666 TA-, HJE HLE Re6!o the tra33i& lights %rogra$ an! se this %ro&e! re to set % realisti& ti$e !ela/s. 0(tlight.as$ Re6!o the teCt in% t an! !is%la/ %rogra$ "ith %ro&e! res. Use one %ro&e! re to in% t the teCt an! one to !is%la/ it.

; 666666666666666666666666666666666666666666666666666666666666666 Kou can cop# this e.ample pro"ram from the help pa"e and paste it into the source code editor.

M:V

,D;

) $alue is placed into the )5 re"ister before callin" the time dela# procedure. This $alue determines the len"th of the dela#.

( ,, 30
Call the procedure at address G30H. This alters the instruction pointer %* to G30H and the pro"ram continues to run from that address. @hen the C*U reaches the 6(T command it returns to the address that it came from. This return address is sa$ed on the stac1.

Stac*
This is a re"ion in memor# where $alues are sa$ed and restored. The stac1 uses the 5ast %n Dirst 9ut rule. 5%D9. The C)55 command sa$es the return address on the stac1. The 6(T command "ets the sa$ed $alue from the stac1 and &umps to that address b# settin" %*.

:RG 30
9ri"in at address G30H. 96' specifies at what 6)2 address machine code should be "enerated. The time dela# procedure is stored at address G30H.

'!SH

,a$e the $alue of )5 onto the stac1. The C*U stac1 pointer ,* points to the ne.t free stac1 location. The push command sa$es a $alue at this position. ,* is then mo$ed bac1 one place to the ne.t free position. %n this simulator/ the stac1 "rows towards address Fero. ) stac1 o$erflow occurs if the stac1 tries to fill more than the a$ailable memor#. ) stac1 underflow occurs if #ou tr# to pop an empt# stac1.

'!SH#
,a$e the C*U fla"s in the status re"ister ,6 onto the stac1. This ensures that the fla"s can be put bac1 as the# were when the procedure completes. The stac1 pointer is mo$ed bac1 one place. ,ee the *ush command. .:T0: %tems must be popped in the re$erse order the# were pushed.

40(

,ubtract one from )5. This command sets the F fla" if the answer was Fero or the , fla" if the answer was ne"ati$e.

C.I R0'
Eump Not Fero to the address that 6ep mar1s. Eump if the F fla" is not set.

':'#
6estore the C*U fla"s from the stac1. %ncrease the stac1 pointer b# one.

':'

6estore the )5 re"ister from the stac1. This is done b# first mo$in" the stac1 pointer ,* forward one place and cop#in" the $alue at that stac1 position into the )5 re"ister. ) stac1 underflow occurs when an attempt is made to pop more items off the stac1 than were present. .:T0: %tems must be popped in the re$erse order the# were pushed.

R0T
6eturn from the procedure to the address that was sa$ed on the stac1 b# the C)55 command. *rocedures can re:use themsel$es. This is called recursion. %t is a powerful techni3ue and dan"erous if #ou donAt understand what is happenin"O )ccidental or uncontrolled recursion causes the stac1 to "row until it o$erwrites the pro"ram or o$erflows.

01ample " 09te1tio.asm " Te1t I/: 'rocedures


(ontents

01ample " 09te1tio.asm


; ; ; ; ; 66666666666666666666666666666666666666666666666666666666666666 A %rogra$ to rea! in a string o3 teCt an! store it in RAM. The en! o3 teCt "ill be labelle! "ith A-C22 &o!e 4eroOn ll. 66666666666666666666666666666666666666666666666666666666666666 T7E MA2N PROGRAM MOV 0L,=0 ; ;=0< is the a!!ress "here the teCt "ill ; be store!. The %ro&e! re ses this. CALL H0 ; The %ro&e! re at ;H0< rea!s in teCt an! ; %la&es it starting 3ro$ the a!!ress ; in 0L. ; 0L sho l! still &ontain ;=0< here. CALL ?0 ; This %ro&e! re !oes nothing ntil /o ; "rite it. 2t sho l! !is%la/ the teCt.

7ALT ; 'ONAT U-E EN' 7ERE 01 M2-TA,E. ; 66666666666666666666666666666666666666666666666666666666666666 ; A PROCE'URE TO REA' 2N T7E TEKT ORG H0 ; Co!e starts 3ro$ a!!ress ;H0< PU-7 PU-7 PU-7F Re%: 2N CMP )> MOV 2NC )MP -to%: MOV MOV POPF POP POP AL,0 ; This is the NULL en! $ar*er ;0L<,AL ; Co%/ NULL &hara&ter to this %osition. 0L AL ; Restore 3lags 3ro$ the sta&* ; Restore 0L 3ro$ the sta&* ; Restore AL 3ro$ the sta&* 00 AL,0' -to% ;0L<,AL 0L Re% ; ; ; ; ; ; 2n% t 3ro$ %ort 00 D*e/boar!E +as *e/ %ress the Enter *e/5 23 /es then # $% to -to% Co%/ *e/%ress to RAM at %osition ;0L< 0L %oints to the neCt lo&ation. ) $% ba&* to get the neCt &hara&ter AL 0L ; -ave AL onto the sta&* ; -ave 0L onto the sta&* ; -ave the CPU 3lags onto the sta&*

RET ; Ret rn 3ro$ the %ro&e! re. ; 66666666666666666666666666666666666666666666666666666666666666 ; A PROCE'URE TO '2-PLA1 TEKT ON T7E -2MULATE' -CREEN ORG ?0 ; Co!e starts 3ro$ a!!ress ;H0< ; PPPP 1OU MU-T F2LL T72- GAP PPPP RET ; At %resent this %ro&e! re !oes ; nothing other than ret rn. ; 66666666666666666666666666666666666666666666666666666666666666

EN' ; 2t is &orre&t to se EN' at the en!. ; 66666666666666666666666666666666666666666666666666666666666666 TA-, H=E +rite a %rogra$ sing three %ro&e! res. rea! teCt 3ro$ the *e/boar! an! store it sho l! &onvert an/ %%er &ase &hara&ters to lo"er &ase. The thir! sho l! !is%la/ V'U s&reen. The 3irst sho l! in RAM. The se&on! in the store! teCt the teCt on the

; 66666666666666666666666666666666666666666666666666666666666666 Kou can cop# this e.ample pro"ram from the help pa"e and paste it into the source code editor.

'assin& 'arameters M:V %,D90


The B5 re"ister contains C0. This $alue is needed b# the te.t input procedure. %t is the address where the te.t will be stored in 6)2. This is an e.ample of passin" a parameter usin" a re"ister. )ll #ou are doin" is "ettin" a number from one part of a pro"ram to another.

I.( %,
This command adds one to B5. The effect is to ma1e B5 point to the ne.t memor# location read# for the ne.t te.t character to be stored.

( ,, -0
Call the procedure at address G10H. This is achie$ed in practice b# settin" the C*U instruction pointer %* to G10H.

R0T
)t the end of the procedure/ the 6(T command resets the C*U instruction pointer %* bac1 to the instruction after the C)55 instruction to the procedure. This address was stored on the stac1 b# the C)55 instruction.

H ,T
4onAt confuse =)5T and (N4. The (N4 command causes the assembler to stop scannin" for more instructions in the pro"ram. The =)5T command "enerates machine code 00 which causes the C*U to halt. There can be se$eral =)5T commands in a pro"ram but onl# one (N4 command.

:RG -0
9ri"in G10H. The assembler pro"ram starts "eneratin" machine code from address G10H.

'!SH

, and ':'

,a$e the $alue of )5 onto the stac1. This is an area of 6)2 startin" at address BD. The stac1 "rows towards >ero. The 6)2 displa#s show the stac1 pointer as a blue hi"hli"ht with #ellow te.t. *ush and *op are used so that procedures and interrupts can tid# up after themsel$es. The procedure or interrupt can alter C*U re"isters but it restores them to their old $alues before returnin".

'!SH# and ':'#


*U,=D sa$es the C*U fla"s onto the stac1. *9*D restores the C*U fla"s to their ori"inal $alue. This enables procedures and interrupts to do useful wor1 without une.pected side affects on the rest of the pro"ram.

I. 00

%nput from port >ero. This port is connected to the 1e#board. The 1e# press is stored into the )5 re"ister.

(M'

,D04

Compare the )5 re"ister with the he.adecimal number 04. 04 is the ),C%% code of the (nter 1e#. This line is as1in" B@as the enter 1e# pressedSB C2* wor1s b# subtractin" 04 from )5. %f the# were e3ual then the subtraction "i$es an answer of >ero. This causes the C*U >ero or AFA fla" to be set.

CI Stop
Eump to the ,top label if the C*U AFA fla" was set. This is a conditional &ump.

M:V J%,KD ,
2o$e the 1e# press stored in )5 into the 6)2 location that GB5H points to. %NC B5 is then used to ma1e B5 point to the ne.t 6)2 location.

CM' Rep
Eump bac1 to the 6ep label. This is an unconditional &ump. %t alwa#s &umps and the C*U fla"s are i"nored.

R0T
6eturn from the procedure to the address stored on the stac1. This is done b# settin" the instruction pointer %* in the C*U.

01ample " 0;ta)le.asm " 4ata Ta)les


(ontents

01ample " 0;ta)le.asm


; 66666 EKAMPLE M 6666666 'ATA TA0LE- 66666666666666666666666666 )MP '0 '0 '0 '0 -tart: Re%: MOV MOV OUT CMP )> 2NC )MP -tart M? CM 9H JM 0L,0( ; -*i% %ast the !ata table. ; ; ; ; 'ata table begins. These val es &ontrol the tra33i& lights This seI en&e is si$%li3ie!. Last entr/ is also se! as en! $ar*er

; 0( is start a!!ress o3 !ata table

AL,;0L< ; Co%/ !ata 3ro$ table to AL 0H ; O t% t 3ro$ AL register to %ort 0H AL,JM -tart 0L Re% ; ; ; ; Last ite$ in !ata table 555 23 /es then # $% to -tart 2n no then %oint 0L to the neCt entr/ ) $% ba&* to !o neCt table entr/

EN' ; 66666666666666666666666666666666666666666666666666666666666666 TA-, HME HNE 2$%rove the tra33i& lights !ata table so there is an overla% "ith both sets o3 lights on re!. Use a !ata table to navigate the sna*e thro gh the $a4e. This is on %ort 0?. -en! FF to the sna*e to reset it. U%, !o"n le3t an! right are &ontrolle! b/ the le3t 3o r bits. The right 3o r bits &ontrol the !istan&e $ove!. +rite a %rogra$ to s%in the ste%%er $otor. A&tivate bits H, (, ? an! M in seI en&e to energise the ele&tro$agnets in t rn. The $otor &an be hal3 ste%%e! b/ t rning on %airs o3 $agnets 3ollo"e! b/ a single $agnet 3ollo"e! b/ a %air an! so on. Use a !ata table to $a*e the $otor %er3or$ a &o$%leC seI en&e o3 3or"ar! an! reverse $oves. This is the t/%e o3 &ontrol nee!e! in roboti& s/ste$s, %rinters an! %lotters. For this eCer&ise, it !oes not $atter eCa&tl/ "hat the $otor !oes.

(0E

(HE

; 66666666666666666666666666666666666666666666666666666666666666 Kou can cop# this e.ample pro"ram from the help pa"e and paste it into the source code editor.

4% ;6
4B stands for 4efine B#teIs. %n this case 70he. is stored into 6)2 at address G02H. )ddresses G00H and G01H are occupied b# the E2* ,tart machine codes.

70 he. is 1000 0100 in binar#. This is the pattern or nou"hts and ones needed to turn on the left red li"ht and the ri"ht "reen li"ht.

M:V %,D02
2o$e 02 into the B5 re"ister. G92H is the 6)2 address of the start of the data table. B5 is used as a pointer to the data table.

M:V

,DJ%,K

GB5H points to the data table. This line copies a $alue from the data table into the )5 re"ister.

:!T 0,end the contents of the )5 re"ister to port 01. *ort 01 is connected to the traffic li"hts.

(M'

,D5;

87 is the last entr# in the data table. %f )5 contains 87/ it is necessar# to reset B5 to point bac1 to the start of the table read# to repeat the se3uence. %f )5 is e3ual to 87/ the AFA fla" in the C*U will be set.

CI Start
Eump bac1 to start if the AFA fla" in the C*U is set.

I.( %,
)dd one to B5 to ma1e it point to the ne.t entr# in the data table.

01ample " 0<param.asm " 'arameters


(ontents

01ample " 0<param.asm


; 66666 EKAMPLE N 6666666 Passing Para$eters 6666666666666666666 ; 66666 Use Registers to %ass %ara$eters into a %ro&e! re 666666 )MP '0 '0 MOV MOV CALL -tart 0 0 AL,J 0L,? 90 ; -*i% over b/tes se! 3or !ata storage

-tart:

; Reserve a b/te o3 RAM at a!!ress ;0(< ; Reserve a b/te o3 RAM at a!!ress ;09<

; A %ro&e! re to a!! AL to 0L ; Res lt ret rne! in AL.

; 66666 Use RAM lo&ations to %ass %ara$eters into a %ro&e! re 66 MOV MOV MOV MOV CALL AL,9 ;0(<,AL ; -tore 9 into a!!ress ;0(< 0L,H ;09<,0L ; -tore H into a!!ress ;09< ?0

; 66666 Use the -ta&* to %ass %ara$eters into a %ro&e! re 666666 MOV AL,= PU-7 AL MOV 0L,( PU-7 0L CALL L0 POP 0L POP AL ; This one &ontains the ans"er )MP -tart ; Go ba&* an! !o it again.

; 66666 A %ro&e! re to a!! t"o n $bers 6666666666666666666666666 ; ; ; ; Para$eters %asse! into %ro&e! re sing AL an! 0L Res lt ret rne! in AL This $etho! is si$%le b t is no goo! i3 there are a lot o3 %ara$eters to be %asse!. ORG A'' RET 90 AL,0L ; Co!e starts at a!!ress ;90< ; 'o the a!!ition. Res lt goes into AL

; Ret rn 3ro$ the %ro&e! re

; 66666 A %ro&e! re to a!! t"o n $bers 6666666666666666666666666 ; Para$eters %asse! into %ro&e! re sing RAM lo&ations. ; Res lt ret rne! in RAM lo&ation ; ; This $etho! is $ore &o$%leC an! there is no li$it on the n $ber o3 %ara$eters %asse! nless RAM r ns o t.

ORG PU-7 PU-7 PU-7F MOV MOV A'' MOV POPF POP POP RET

?0 CL 'L CL,;0(< 'L,;09< CL,'L ;0(<,CL

; Co!e starts at a!!ress ;?0< ; -ave registers an! 3lags on the sta&*

; ; ; ;

Fet&h a %ara$eter 3ro$ RAM Fet&h a %ara$eter 3ro$ RAM 'o the a!!ition -tore the res lt in RAM

; Restore original register 'L CL ; an! 3lag val es

; 66666 A %ro&e! re to a!! t"o n $bers 6666666666666666666666666 ; The n $bers to be a!!e! are on the sta&*. ; POP %ara$eters o33 the sta&* ; 'o the a!!ition ; P sh ans"er ba&* onto the sta&* ; The $a#orit/ o3 %ro&e! re &alls in real li3e $a*e se ; o3 the sta&* 3or %ara$eter %assing. 2t is ver/ &o$$on ; 3or the a!!ress o3 a &o$%leC !ata str &t re in RAM to ; be %asse! to a %ro&e! re sing the sta&*. ORG POP POP POP A'' PU-7 PU-7 PU-7 L0 'L 0L AL AL,0L AL AL 'L ; Ans"er ; The n $ber o3 % shes $ st ; Ans"er ; $at&h the n $ber o3 %o%s. ; P t the sta&* ba&* as it "as be3ore ; Co!e starts at a!!ress ;L0< ; Ret rn a!!ress ; A %ara$eter ; A %ara$eter

RET ; 66666666666666666666666666666666666666666666666666666666666666 EN' Tas* ((E +rite a %ro&e! re that !o bles a n $ber. Pass the single %ara$eter into the %ro&e! re sing a register. Use the sa$e register to ret rn the res lt. +rite a %ro&e! re to invert all the bits in the 4eros sho l! be&o$e ones. All the ones 4eros. Pass the val e to be %ro&esse! into sing a RAM lo&ation. Ret rn the res lt in lo&ation. a b/te. All sho l! be&o$e the %ro&e! re the sa$e RAM

(9E

(?E

+rite a %ro&e! re that "or*s o t Fa&torial N. This eCa$%le sho"s one $etho! 3or "or*ing o t 3a&torial N. Fa&torial J is J P ? P 9 P ( P H = H(0. 1o r %ro&e! re sho l! "or* %ro%erl/ 3or 3a&torial H, (, 9, ? or J. Fa&torial L "o l! &a se an over3lo". Use the sta&* to %ass %ara$eters an! ret rn the res lt. Cal& late the res lt. Using a loo* % table is &heating@ +rite a %ro&e! re that "or*s o t Fa&torial N. Use the

(JE

sta&* 3or %ara$eter %assing. +rite a re& rsive %ro&e! re. Use this !e3inition o3 Fa&torial. Fa&torial D 0 E is !e3ine! as H. Fa&torial D N E is !e3ine! as N P Fa&torial DN 6 HE. To "or* o t Fa&torial DNE, the %ro&e! re 3irst tests to see i3 N is 4ero an! i3 not then re6 ses itsel3 to "or* o t N P Fa&torial DN 6 HE. This %roble$ is har! to n!erstan! in an/ %rogra$$ing lang age. 2n asse$bl/ &o!e it is har!er still. Kou can cop# this e.ample pro"ram from the help pa"e and paste it into the source code editor.

'assin& 'arameters
*arameters can be passed in three wa#s.

1. ('! re&isters can )e used : Dast but little data can be passed. %n some pro"rammin"
lan"ua"es the B6e"isterB 1e#word is used to achie$e this.

2. R M locations can )e used : ,lower and recursion ma# not be possible. %n some

pro"rammin" lan"ua"es the B,taticB 1e#word is used to achie$e this. This techni3ue is useful if $er# lar"e amounts of data are help in 6)2. *assin" a pointer to the data is more efficient than ma1in" a cop# of the data on the stac1. and recursion is possible. Compilers "enerall# use this method b# default unless otherwise directed.

3. The stac* can )e used : =arder to understand and code but a lot of data can be passed

The e.ample pro"ram uses all three methods to add two numbers to"ether. The e.ample tas1s in$ol$e all three methods.

01ample " -0swint.asm Software Interrupts


(ontents

01ample " -0swint.asm


; 66666666666666666666666666666666666666666666666666666666666666 ; An eCa$%le o3 so3t"are interr %ts. ; 66666666666666666666666666666666666666666666666666666666666666 )MP -tart ; ) $% %ast table o3 interr %t ve&tors '0 JH ; Ve&tor at 0( %ointing to a!!ress JH '0 =H ; Ve&tor at 09 %ointing to a!!ress =H -tart: 2NT 0( ; 'o interr %t 0( 2NT 09 ; 'o interr %t 09 )MP -tart ; 66666666666666666666666666666666666666666666666666666666666666 ORG '0 J0 E0 ; ; AL,;J0< ; AL ; ;J0<,AL ; 0H ; 'ata b/te 6 &o l! be a "hole table here 2nterr %t &o!e starts here Co%/ bits 3ro$ RAM into AL 2nvert the bits in AL Co%/ inverte! bits ba&* to RAM -en! !ata to tra33i& lights

MOV NOT MOV OUT 2RET ; 66666666666666666666666666666666666666666666666666666666666666 ORG =0 '0 0 ; 'ata b/te 6 &o l! be a table here ; 2nterr %t &o!e starts here MOV AL,;=0< ; Co%/ bits 3ro$ RAM into AL NOT AL ; 2nvert the bits in AL AN' AL,FE ; For&e right $ost bit to 4ero MOV ;=0<,AL ; Co%/ inverte! bits ba&* to RAM OUT 0( ; -en! !ata to seven seg$ent !is%la/ 2RET ; 66666666666666666666666666666666666666666666666666666666666666 EN' ; 66666666666666666666666666666666666666666666666666666666666666 TA-, (LE +rite a ne" interr %t 0( that 3et&hes a *e/ %ress 3ro$ the *e/boar! an! stores it into RAM. The 20M PC allo&ates HL b/tes 3or *e/ %ress storage. The HL lo&ations are se! in a &ir& lar 3ashion. Create a ne" interr %t that % ts &hara&ters onto the neCt 3ree s&reen lo&ation. -ee i3 /o &an get &orre&t behavio r in res%onse to the Enter *e/ being %resse! D3airl/ eas/E an! i3 the 0a&* -%a&e *e/ is %resse! Dhar!erE.

(=E

Kou can cop# this e.ample pro"ram from the help pa"e and paste it into the source code editor.

Interrupts and 'rocedures


%nterrupts are short code fra"ments that pro$ide useful ser$ices that can be used b# other pro"rams. T#pical routines handle 1e# presses/ mouse mo$ements and button presses/ screen writin"/ dis1 readin" and writin" and so on. )n interrupt is li1e a procedure but it is called in a different wa#. *rocedures are called b# &umpin" to the start address of the procedure. This address is 1nown onl# to the pro"ram that owns the procedure. %nterrupts are called b# loo1in" up the address of the interrupt code in a table of interrupt $ectors. The contents of this table is published and widel# 1nown. 2, 49, ma1es hea$# use of interrupts for all its dis1/ screen/ mouse/ networ1/ 1e#board and other ser$ices. B# writin" #our own code and ma1in" the interrupt $ector point to the code #ou wrote/ the beha$iour of interrupts can be completel# altered. Kour interrupt code mi"ht add some useful beha$iour and then &ump bac1 to the ori"inal code to complete the wor1. This is called T6)**%N' the interrupt. Software interrupts are tri""ered/ on demand/ b# pro"rams. Hardware interrupts are tri""ered b# electronic si"nals to the C*U from hardware de$ices.

Interrupt Vector Ta)le


%n the %B2 compatible computer/ addresses 0 to 1020 decimal are used for storin" interrupt $ectors. The entries in this table of $ectors point to all the code fra"ments that control 2, 49, screen/ dis1/ mouse/ 1e#board and other ser$ices. The simulator $ectors sit between addresses 0 and 18 decimal. %t is con$enient to start a simulator pro"ram with a &ump command that occupies two b#tes. This means that the first free address for an interrupt $ector is G02H. This is used b# the hardware timer if the interrupt fla" is set. =a$e another loo1 at the e.ample pro"ram. 10swint.asm

(allin& an Interrupt
This is 3uite comple.. The command %NT 02 causes the C*U to retrie$e the contents of 6)2 location 02. )fter sa$in" the return address onto the stac1/ the instruction pointer %* is set to this address. The interrupt code is then e.ecuted. @hen complete the %6(T command causes the return from the interrupt. The C*U instruction pointer %* is set to the address that was sa$ed onto the stac1 earlier.

Trappin& an Interrupt
%f #ou wan to trap interrupt 02/ chan"e the address stored at address 02 to point to code that #ou ha$e written. Kour code will then handle the interrupt. @hen complete/ #our code can use %6(T to return from the interrupt or it can &ump to the address that was ori"inall# in address 02. This causes the ori"inal interrupt code to be e.ecuted as well. %n this wa#/ #ou can replace or modif# the beha$iour of an interrupt.

01ample " --hwint.asm Hardware Interrupts


(ontents

01ample " --hwint.asm


; ; ; ; 66666666666666666666666666666666666666666666666666666666666666 An eCa$%le o3 sing har!"are interr %ts. This %rogra$ s%ins the ste%%er $otor &ontin o sl/ an! ste%s the tra33i& lights on ea&h har!"are interr %t.

; Un&he&* the Q-ho" onl/ one %eri%heral at a ti$eQ boC ; to enable both !is%la/s to a%%ear si$ ltaneo sl/. ; 66666666666666666666666666666666666666666666666666666666666666 )MP -tart ; ) $% %ast table o3 interr %t ve&tors '0 J0 ; Ve&tor at 0( %ointing to a!!ress J0 -tart: -T2 MOV Re%: AL,HH ; -et 2 3lag. Enable har!"are interr %ts ;

OUT 0J ; -te%%er $otor ROR AL ; Rotate bits in AL right )MP Re% )MP -tart ; 66666666666666666666666666666666666666666666666666666666666666 ORG J0 PU-7 PU-7 PU-7F )MP '0 '0 '0 '0 '0 Past'ata: MOV MOV OUT CMP )> 2NC MOV )MP MOV MOV -to%: POPF POP POP al bl ; -ave AL onto the sta&*. ; -ave 0L onto the sta&*. ; -ave 3lags onto the sta&*.

Past'ata M? &M 90 JM J= 0L,;J0< AL,;0L< 0H AL,JM Reset ; ; ; ; ; ; ; ; ; ; Re! Green Re!RA$ber A$ber Green Re! A$ber Re!RA$ber Use! to tra&* %rogress thro gh table 0L no" %oints to the !ata table 'ata 3ro$ table goes into AL -en! AL !ata to tra33i& lights Last entr/ in the table 23 last entr/ then reset %ointer

Reset:

0L ; 0L %oints to neCt table entr/ ;J0<,0L ; -ave %ointer in RAM -to% 0L,J= ; Pointer to !ata table start a!!ress ;J0<,0L ; -ave %ointer into RAM lo&ation J? bl al ; Restore 3lags to their %revio s val e ; Restore 0L to its %revio s val e ; Restore AL to its %revio s val e

2RET ; 66666666666666666666666666666666666666666666666666666666666666 EN' ; 66666666666666666666666666666666666666666666666666666666666666 TA-, (ME +rite a %rogra$ that &ontrols the heater an! ther$ostat "hilst at the sa$e ti$e &o nting 3ro$ 0 to N re%eate!l/, !is%la/ing the res lt on one o3 the seven seg$ent !is%la/s. 23 /o "ant a har!er &hallenge, &o nt 3ro$ 0 to NN re%eate!l/ sing both !is%la/s. Use the si$ late! har!"are interr %t to &ontrol the heater an! ther$ostat. A 3ien!ish %roble$. -olve the To"er o3 7anoi %roble$ "hilst steering the sna*e thro gh the $a4e. Use the teCt &hara&ters A, 0, C Et&. to re%resent the !is*s. Use three o3 the 3o r ro"s on the si$ late! s&reen to re%resent the %illars. Use the *e/boar! on Port 0=. +rite an interr %t han!ler D2NT 09E to %ro&ess the *e/ %resses. 1o $ st also %ro&ess 2NT 0( Dthe har!"are ti$erE b t it nee! not %er3or$ an/ tas*. For a $ore a!van&e! tas*, i$%le$ent a HL b/te &ir& lar b 33er. +rite &o!e to %la&e the b 33ere! teCt on the V'U s&reen "hen /o %ress Enter. For an even har!er tas*, i$%le$ent &o!e to %ro&ess the 0a&*s%a&e *e/ to !elete teCt &hara&ters in the b 33er.

(NE

90E

Kou can cop# this e.ample pro"ram from the help pa"e and paste it into the source code editor.

Hardware Interrupts
=ardware %nterrupts are short code fra"ments that pro$ide useful ser$ices that can be tri""ered b# items of hardware. @hen a printer runs out of paper/ it sends a si"nal to the C*U. The C*U interrupts normal processin" and processes the interrupt. %n this case code would run to displa# a B*aper 9utB messa"e on the screen. @hen this processin" is complete/ normal processin" resumes. This simulator has a timer that tri""ers %NT 02 at re"ular time inter$als that #ou can pre:set in the Confi"uration Tab. Kou must put an interrupt $ector at address 02 that points to #our interrupt code. 5oo1 at the e.ample.

STI and (,I


=ardware interrupts are i"nored unless the A%A fla" in the status re"ister is set. To set the A%A fla"/ use the set A%A command/ ,T%. To clear the A%A fla"/ use the clear A%A command C5%. =ardware interrupts can be trapped in the same wa# that software interrupts can. =ardware interrupts are tri""ered/ as needed b# dis1 dri$es/ printers/ 1e# presses/ mouse mo$ements and other hardware e$ents. This scheme ma1es processin" more efficient. @ithout interrupts/ the C*U would ha$e to poll the hardware de$ices at re"ular time inter$als to see if an# processin" was needed. This would happen whether or not processin" was necessar#. %nterrupts can be assi"ned priorities such that a dis1 dri$e mi"ht ta1e priorit# o$er a printer. %t is up to the pro"rammer to optimise all this for efficient processin". %n the %B2 compatible *C/ low number interrupts ha$e a hi"her priorit# than the hi"her numbers.

(allin& an Interrupt
This is 3uite comple.. The command %NT 02 whether tri""ered b# hardware or software/ causes the C*U to retrie$e the contents of 6)2 location 02. )fter sa$in" the return address onto the stac1/ the instruction pointer %* is set to the address that came from 6)2. The interrupt code is then e.ecuted. @hen complete the %6(T command causes the return from the interrupt. The C*U instruction pointer %* is set to the address that was sa$ed onto the stac1 earlier. =ardware interrupts differ sli"htl# from software interrupts. ) software interrupt is called with a command li1e %NT 02 and the return address is the ne.t instruction after this. %* ! 2 is pushed onto the stac1. =ardware interrupts are not tri""ered b# an instruction in a pro"ram so the return address does not ha$e to be set past the callin" instruction. %* is pushed onto the stac1.

Trappin& an Interrupt
This is the same as trappin" software interrupts described on the pre5ious pa"e.

Shortcut 7e/s
(ontents

lt 7e/s
)ssemble Button

(ontrol 7e/s #unction 7e/s


(dit ,elect )ll ( (dit Cop# 4 0 # (dit Dind G H I C 7 , M . : Dile 9pen ' $ R (dit 6eplace S Dile ,a$e T ! V (dit *aste = F (dit Cut E I D1 D2 D3 D0 D8 D DC D7 D< D10 D11 D12 6un =elp

% 5o" )ssembler )cti$it# % ( Confi"uration Tab 4 0 (dit 2enu # Dile 2enu G 5o" Dile Tab H =elp 2enu I C 5ist Dile Tab 7 To1ens Tab , ,lower Button M ,how 6am Button . Continue Button : ,top Button ' ,tep Button $ R 6un Button S 6eset Button T Daster Button ! ,ource Code Tab V ?iew 2enu = @rite 6un 5o" F (.amples 2enu E I

S(II (odes
(ontents

merican Standard (ode for Information Interchan&e


The ),C%% code has 127 standard characters and a further 127 characters that $ar# from machine to machine and countr# to countr#. The first 127 ),C%% characters are shown here. 4ec He1 00 -8 32 6; 86 ;0 <8 --2 00 -0 20 30 60 50 80 90 Spa 0 V ' Z p $ a > L A 2 % R ) r M 3 ( S c s N 6 4 T d t O 5 0 ! e u P 8 # V f 5 @ 9 G = & w G ; H F h 1 H < I E i / 00 00 .ul 0002 02 03 03 06 06 05 05 08 08 09 09 0; 0; 0< 0< -0 0 ,# 0:# 0S( Q : C I B [ + R 7 J * \ D S , W l ] " T M K m ^ . U . X n _ / ? : Y o .ul -0% -2 0( -3 04 (R -6 00 -5 0#

%el %a* Ta)

The codes from 127 to 288 are not shown here. Codes with special meanin"s to 49,/ *rinters/ Telet#pe 2achines and )N,% screens. 4ecimal 0 9 ; < -0 -3 28 29 32 255 .ul NU55 character. + (nd of te.t strin" mar1er. %el Bell or beep character. %a* Bac1space character. Ta) T)B character. ,# 5ine Deed +start a new line-. (R Carria"e 6eturn code. 0:# 49, (nd of te.t file code. 0sc (scape code. %t has special effects on older printers and )N,% screens. )N,% Q )merican National ,tandards %nstitute. Spa ,pace Character. .ul NU55 character.

!nicode
),C%% is bein" replaced b# the 1 bit Unicode with 883 characters that represent e$er# te.t character in e$er# countr# in the world includin" those used historicall#. 2ost new operatin" s#stems software pac1a"es support Unicode.

Glossar/
(ontents

Glossar/
3;8 C*U chips in %B2 compatible computers are t#picall# numbered 07 / 17 / 27 / 37 / 07 / 87 / *entium (tc. 07 chips are now re"arded as old fashioned and slow. To run @indows/ a 32 bit 37 chip was the minimum recommended. The C*U has re"isters and connections to the outside world that are 7 bits wide. 1 bit and 32 bit C*Us are now more common/ more powerful and more e.pensi$e. 0 bit C*Us e.ist but are not common +2003The famil# of %ntel chips numbered 707 / 7017 / 7027 / 7037 / 7007 and *entium. %mpro$es #our producti$it#. Dor e.ample )lt!D0 closes the current window and is 3uic1er to press than the e3ui$alent mouse or menu actions. (lectronic s#stems that deal with continuousl# $ar#in" si"nals. 6adio/ T? and =iDi s#stems are all analo"ue. C4 *la#ers are di"ital but the di"ital si"nals must be con$erted to analo"ue before bein" sent to the =iDi s#stem. )merican National ,tandards %nstitute C*U desi"ns are more comple. than t#pical buildin" desi"ns. Computer architecture is e3ui$alent to buildin" architecture. To ma1e best use of a computer/ it is useful to 1now somethin" about the computer desi"n or architecture. The )merican ,tandard Code for %nformation %nterchan"e. This is an ei"ht bit code. There are 127 characters which are standard. There are a further 127 characters that $ar# dependin" on the countr# and the "raphics s#mbols re3uired b# printers. )merican ),C%% is bein" replaced b# %nternational Unicode. The usual file e.tension for assembl# code pro"rams =uman readable commands li1e 29? )5/33 correspond closel# to C*U machine codes. The assembler pro"ram translates the human readable codes into machine codes readable b# the C*U C Neil Bauers can be ( 2ailed on the internet at nbauersTsamphire.demon.co.u1 Copies of files 1ept in case of disaster. These should be 1ept in a secure place awa# from the computer s#stem the# belon" to. %mportant files should be bac1ed up in more than one place. ,odAs law applies to bac1 up files. The file #ou reall# need is the one #ou ha$e failed to bac1 up. The start address of an ob&ect stored in memor#. Dor e.ample L The ori"inal %B2 *C ?') screen base address is B700L0000 followed b# 0000 more b#tes. Base two numbers used b# di"ital s#stems. Count with two s#mbols G 0 1 H Binar# numbers are composed of nou"hts and ones. (lectronicall# this is achie$ed b# circuits that are switched off or on. *atterns of nou"hts and ones used with )N4/ 96 and J96 to e.tract or inserts bits into b#tes. Binar# di"its. ,in"le di"its that are nou"ht or one. (i"ht Bits. The data in a b#te can ha$e man# different meanin"s dependin" on the conte.t. ) b#te can represent a C*U command/ an ),C%% character/ a decimal number/ a "raphics pattern or an#thin" #ou ha$e pro"rammed it to represent. ),C%% code 13 used to mo$e the printer carria"e or head to the left of the pa"e.

; %it ('!

;01;8 ccelerator 7e/ nalo&ue

.SI rchitecture

S(II

SM ssem)ler ssem)l/ code uthor %ac*up cop/

%ase %inar/

ddress

%it Mas*s %its %/te

(arria&e Return

The screen cursor performs in the e3ui$alent wa#. ,ee also : 5ine Deed (ase Sensiti5e (hip (lic* (loc* Upper and lower case are ta1en to be different. This simulator is not case sensiti$e. ,horthand for microchip or inte"rated circuit. The C*U is often referred to as the C*U Chip. Usuall# the left mouse button bein" pressed when the mouse is pointin" at a screen ob&ect. The C*U cloc1 steps the computer and C*U at re"ular time inter$als 1eepin" all parts of the computer in step. T#pical cloc1 speeds ran"e between 1 to 800 2e"ahert>. 200 2=> was t#pical for a *C in 1<<C. These be"in with ARA and are used to e.plain what the pro"ram is doin". 'ood comments e.plain wh# thin"s are bein" done. Bad comments simpl# repeat what is ob$ious b# loo1in" at the code. These &umps either ta1e place or not dependin" on the fla"s in the status re"ister. ,ee E,/ EN,/ E9/ EN9/ EF/ ENF/ E2*. This is used to "i$e 1e#s special meanin"s. Dor e.ample the combination of the control 1e# with the D0 function 1e# will close a window in some software pac1a"es.

(omments

(onditional Cumps (ontrol 7e/

(ontrol S/stems %ndustrial and domestic e3uipment is fre3uentl# controlled b# a small microcomputer called a microcontroller. The control s#stem is pro"rammed once for life so a T? remote controller can not be re:pro"rammed as a washin" machine controller. ('! (ursor Central *rocessin" Unit. The part of the computer that does the computations. Usuall# this is a sin"le microchip. ) flashin" s#mbol that indicates position within te.t. )lternati$el# the mouse cursor indicates the mouse position. ,pecial purpose cursors are used in some software. These store numbers/ te.t or pointers to other data ob&ects. %t is easier to loo1 after data in a table than data scattered throu"hout a pro"ram. %t is "ood st#le to use data tables. C7<H

4ata ta)les

4ecimal %ase -0 Count and do arithmetic with ten s#mbols. G 0 1 2 3 0 8 num)ers.

4i&ital 0lectronic ,#stems that use binar#. Computers use binar# numbers and are di"ital. =iDi s#stems do not use binar# and are not di"ital. +) =iDi remote control s#stem is di"ital- ,ee analo"ue. 4irector/ or #older Dile s#stems are or"anised into directories in much the same wa# that filin" cabinets are or"anised into draws and folders. Kour files should be stored in a director# that #ou ha$e created. This 1eeps #our files from "ettin" muddled up with all the other files on the computer. This will cause an error. )n# number di$ided b# >ero is infinitel# bi". This can not be calculated. ),C%% code 2 is used to indicate the end of 2, 49, te.t files. This character is often interpreted in a special wa# b# pro"rams/ ?4Us and printers. ,ee Dile 2ana"er The 2, 49, file e.tension is >ero or more characters after the dot in the file name. @ord processor files often ha$e .49C on the end. )ssembl# code files end in .),2 Commonl# this accesses the on line help.

4i5ide )/ [ero 0nd :f #ile 0scape S(II code 29 01plorer 01tension

01ecuta)le (ode Non human readable pro"ram code e.ecuted b# the C*U.

#- 7e/

#ile #ile Mana&er or 01plorer #la&s #lopp/ dis*

4ata stored on dis1 or tape. @hen the data is loaded from the file into 6)2/ it could consist of a pro"ram or data used b# the pro"ram. ) windows pro"ram that enables #ou to mana"e #our files. Cop#in"/ renamin" and deletin" files and directories are t#pical file mana"ement tas1s. The %nterrupt/ ,i"n/ Fero and 9$erflow fla"s in the status re"ister indicate the outcome of the pre$ious calculation. ,ee , Dla"/ 9 fla" and F fla". Used to store files. 3.8 inch dis1s ha$e a hard rectan"ular plastic casin" to protect the thin flopp# dis1 inside. 9lder dis1s are actuall# flopp#. The case is bend# cardboard. ,ee 4irector# D1/ D2 ... D10. These 1e#s ha$e special purposes dependin" on the software in use. D1 usuall# acti$ates help. D10 usuall# acti$ates the menu.

#older #unction *e/s

General 'urpose )5/ B5/ C5 and 45 are used to store data and perform calculations. Re&isters Gi&ahert[ Graphics 1000 2e"ahert>. C*U Cloc1 speeds are now measured in "i"ahert>. %ma"es/ pictures and "eometrical shapes are e.amples of "raphics. @indows displa#s e$er#thin" as "raphics. This "i$es "ood loo1in" displa#s but a lot of processin" is needed to achie$e it. ) dis1 that can not normall# be remo$ed from the computer. 2ost computer files are stored on the hard dis1. There should also be bac1up copies stored elsewhere in case the hard dis1 fails. Base 1 numbers. Count and do arithmetic with 1 s#mbols. G 0 1 2 3 0 8 < ) B C 4 ( D H =e.adecimal and Binar# are easil# con$erted which is wh# he.adecimal is used. C7

Hard dis*

He1adecimal

Hot 7e/s

Ctrl!, and Ctrl!9 are e.amples of hot 1e#s. These "i$e 3uic1 access to menu options. Ctrl!, "i$es the Dile ,a$e command. Ctrl!9 "i$es the Dile 9pen command. The % or interrupt fla" in the status re"ister indicates if the C*U will accept or i"nore hardware interrupts. The commands C5% and ,T% clear and set this fla". =ardware interrupts are used to si"nal e$ents li1e B;e# pressedB/ B4is1 6ead#B.or B*rinter out of paper.B ) hardware timer can "enerate an interrupt at re"ular time inter$als. The instruction 29? )5/28 is an e.ample of an immediate instruction. ,ee also L 6e"ister/ %ndirect/ 6e"ister indirect and 29?. This is where data in 6)2 is referred to with a pointer. Dor e.ample 29? )5/G20H mo$es the data from 6)2 location 20 into the )5 re"ister. G20H is a pointer to the 6)2 location. The techni3ue is called indirection. ,ee 29?/ %mmediate/ 6e"ister/ 6e"ister indirect %* points to the instruction bein" e.ecuted. @hen the instruction is complete/ the %* is mo$ed onto the ne.t instruction. %n the 6)2 displa#s/ the instruction pointer is hi"hli"hted red with #ellow te.t. The set of instructions that are reco"nised b# a C*U. T#pical instructions are 2o$e/ )dd and ,ubtract.

I #la&

Immediate Indirect Indirection

Instruction 'ointer Instruction Set

interrupt code ) pro"ram fra"ment desi"ned to be acti$ated at an# time that an interrupt interrupt handler occurs. The fra"ment is stored at an address pointed to b# an interrupt $ector. Interrupt routine %nterrupts can be tri""ered b# hardware. Dor e.ample a 1e# press or the printer runnin" out of paper cause a hardware interrupt. The C*U switches to the code that handles the interrupt. @hen finished/ the C*U continues with its earlier tas1. Interrupt Vector ) pointer stored in a table. The pointer points at the interrupt handler. ,ee %NT. I: ,hort for %nput 9utput. ,ee %N and 9UT ,east si&nificant 5,B. The ri"ht hand bit in a b#te which is worth 0 or 1.

)it ,I#: ,ine #eed ,ist #ile

2,B. The left hand bit in a b#te which is worth 0 or 127. 5east and 2ost si"nificant bits. ,ee ,tac1. ),C%% code 10 used to start a new line on the printed pa"e or screen. ,ee also : Carria"e 6eturn. This is "enerated b# the simulator assembler. %t contains the pro"ram written b# the pro"rammer. %t also contains the machine codes "enerated b# the assembler. 5ow le$el pro"rammin" is done usin" the C*U machine code or mnemonics the are close to the machine codes. 5,B. The ri"ht hand bit in a b#te which is worth 0 or 1. 2,B. The left hand bit in a b#te which is worth 0 or 127. 5east and 2ost si"nificant bits. 2achine codes are e.ecuted b# the C*U ,ee )ssembl# codes. =uman readable commands loo1 li1e this 29? )5/88 The he.adecimal e3ui$alent loo1s li1e this 40 00 88 The binar# machine code loo1s li1e this 110100000000000001010101 220 b#tes to be precise or a million b#tes appro.imatel# 2=>. 2illion cloc1 c#cles per second. ) 33 2=> cloc1 means that the C*U performs 33 million steps per second. These sorts of speeds are needed to fill screens with hi"h resolution "raphics 3uic1l#.

,ow le5el ,S%

Machine codes

Me&a)/te Me&ahert[

Memor/ Mapped 2emor# mapped hardware is controlled b# writin" data into memor# locations occupied b# that hardware de$ice. This simulator has a memor# mapped screen so each screen position corresponds to a memor# location. Microchip Microcontroller Comple. electronic circuits miniaturised onto a sin"le wafer or chip of silicon Usuall# a sin"le chip microcircuit containin" a C*U/ 6)2 and 692. 2icrocontrollers are used in T? remote controllers/ washin" machines/ di"ital cloc1s/ microwa$e o$ens/ industrial plant controllers/ car en"ine mana"ement s#stems and computer "ames. ) sin"le chip C*U. ) memorable and human readable item li1e 29? that corresponds to a non memorable item li1e 11010000 that means the same thin". 5,B. The ri"ht hand bit in a b#te which is worth 0 or 1. 2,B. The left hand bit in a b#te which is worth 0 or 127. 2ost ,i"nificant Bit. The left hand bit in a b#te. %t has a $alue of 127 decimal or 70=e. if the b#te is unsi"ned +positi$e numbers onl#-. %t has a $alue of :127 if the b#te is si"ned +positi$e and ne"ati$e numbers-. The 2,B has a $alue that depends on how wide in bits the data stora"e location is. Multiple1in& Combinin" two or more data flows onto a sin"le carr#in" medium. Dor e.ample a thousand telephone calls can be carried on a sin"le cable. 4e:multiple.in" separates the channels and routes them to their correct destinations. ),C%% code >ero used to mar1 the end of te.t strin"s. The 9 or o$erflow Dla" in the status re"ister indicates if the pre$ious calculation o$erflowed its re"ister. The networ1 is disconected. =owe$er resources/ can be made a$ailable locall# +off:line- e$en when the networ1 is not a$ailable. @hen the networ1 is re: connected/ the data files are s#nchronised so e$er#one "ets the most up:to:date information. The networ1 is connected. Computer resources are connected and a$ailable and can be accessed with a ne"li"ible or short time dela#. 9n line resources usuall#

Microprocessor Mnemonic Most si&nificant )it MS%

.!,, : #la& :ff ,ine

:n ,ine

in$ol$e interaction with the user. :' (ode :perand ) binar# code that the C*U can interpret as a command. These correspond to commands li1e 29? and )44. (ssential data that comes after the op code. 29? )5/ 88 9p:Code 9perand 9perand :5erflow #la& 'arameters This is set if the result of the pre$ious calculation was too bi" to fit the re"ister. 4ata passed into procedures of functions. *arameters can be passed usin" re"isters +$er# fast-/ 6)2 locations +"ood for bi" data items- or the ,tac1 +useful if recursion is needed-. =ardware plu""ed into the computer. )n#thin" from a 1e#board or mouse to a power station or chemical wor1s. %n the command 29? )5/G28H the 28 is a pointer to the 6)2 location with address 28. ,ee indirection. %nput 9utput *orts. *eripherals are connected to ports. The %N and 9UT machine instructions are used to communicate with the peripherals. ,mall/ modular/ self contained/ easil# tested/ code fra"ments that can be used man# times durin" the e.ecution of a pro"ram. ,ee C)55 and 6(T in the instruction set. ) pro"ram that is runnin" or loaded read# to run. *rocesses can be runnin"/ read# to run or waitin". @aitin" processes are usuall# 3ueuein" up for dis1 or printer access. ) waitin" process mi"ht be waitin" for its share of C*U time. %nstructions e.ecuted b# a computer to perform tas1s. 6andom access memor#. (lectronic memor# that stores b#tes. Normal 6)2 for"ets what it was storin" when switched off. ) powerful techni3ue where a procedure or function re:uses itself to achie$e a tas1. ) location in the C*U where data is stored. This simulator has four "eneral purpose re"isters called )5/ B5/ C5 and 45. %t has special purpose re"isters called %*/ ,6 and ,*. %n the instruction C2* )5/B5 re"isters are bein" compared. ,ee also L %mmediate/ %ndirect/ 6e"ister indirect.

'eripherals 'ointers 'orts 'rocedures

'rocess

'ro&rams R M Recursion Re&ister

Re&ister

Re&ister indirect %n the instruction 29? )5/GB5H the B5 re"ister contains a pointer to a 6)2 location. The data in this 6)2 location is mo$ed into )5. This is a re"ister indirect mo$e. ,ee also L %mmediate/ %ndirect and 6e"ister. Repetition Reset ('! This is achie$ed b# usin" &ump commands to ma1e the pro"ram &ump bac1 and repeat instructions. 6eset the C*U to its switch on state. Clear the "eneral purpose re"isters to >ero. ,et %* to >ero. ,et the fla"s to Fero. ,et the stac1 pointer to BD. The stac1 "rows downwards from address DB. The address stored on the stac1 that the pro"ram returns to when a procedure or interrupt is complete. 6un a pro"ram. *ro"rams are collections of stored instructions that are usuall# inacti$e. To run a pro"ram/ it must be copied from dis1 into 6)2 and the C*U is "i$en the address of the first instruction in the pro"ram so it can run it. ) runnin" pro"ram is often called a process. The , or si"n fla" in the status re"ister indicates if the pre$ious calculation "a$e a ne"ati$e result. Cop# processed data from 6)2 onto dis1.

Return address Run

S #la& Sa5e a file

Se5en se&ment Scheduler

displa#s are used in di"ital cloc1s/ watches/ calculators and so on. Numbers are built up b# illuminatin" combinations of the se$en se"ments. The scheduler is a process that mana"es all the other processes in a computer. %t aims to ma1e best use of the hardware resources and to minimise dela#s to processes and users. The leftmost bit in a binar# number that is used to indicate if the number is positi$e or ne"ati$e. This is set if a calculation "i$es a ne"ati$e result. Computer software that models realit# in some wa#. ?irtual realit# aims to ma1e the simulation so realistic that it seems real. 2ost simulations are desi"ned to be useful rather than realistic. The human readable pro"ram code t#ped into the computer. ,ee e.ecutable code. The stac1 pointer re"ister. %n the 6)2 displa#s/ the stac1 pointer is hi"hli"hted blue with #ellow te.t. The status re"ister. This contains fla"s that are set as a result of the most recent calculation. ) >ero result will set the F +>ero- fla". ) ne"ati$e result will set the , +si"n- fla". ) result too bi" to fit in a re"ister will set the 9 fla" +o$erflow-. %f the % fla" is clear +not set- interrupts will be i"nored. )n area of memor# used for temporar# stora"e accordin" to the 5%D9 rule. 5ast in Dirst out. The stac1 is used to sa$e re"ister contents for later restoration/ pass parameters into procedures and return results/ re$erse the order in which data is stored/ sa$e addresses so procedures can return to the ri"ht place and there are other uses includin" doin" postfi. arithmetic. *oints to the ne.t free location on the stac1. %n the 6)2 displa#s/ the stac1 pointer is hi"hli"hted blue with #ellow te.t. The stac1 is memor# or"anised as 5%D9 last in first out. %t is used to store return addresses/ the C*U state/ parameters passed to procedures/ results returned from procedures/ arithmetic data bein" processed and data whose order is to be re$ersed. The status 6e"ister contains status fla"s that indicate the outcome of the pre$ious calculation. The fla"s are ,i"n/ Fero and 9$erflow. ,ee ,6. ) special motor that rotates in small controlled an"ular mo$ements. %t is used commonl# in printers/ plotters and medical instruments and dis1 dri$es. Use )lt Tab to tas1 switch manuall#. 9peratin" s#stems also tas1 switch automaticall#. Dor e.ample when word processin"/ the cloc1 displa# continues to wor1 because from time to time the operatin" s#stem switches tas1s to 1eep both "oin". ) temperature controlled switch. 9n when too cold. 9ff when too hot. @hen pro"rams are translated into machine code/ one of the first steps is to con$ert the source code of the pro"ram into to1ens. These are not usuall# human readable. The to1ens are desi"ned to occup# minimal memor#. This simulator con$erts source code to to1ens but does not bother to code them to sa$e memor#. This is because the pro"rams are too small use much memor#. Binar# numbers where the left most bit determines whether the number is positi$e or ne"ati$e. ) 1 bit character code with 883 te.t characters for all the lan"ua"es in the world includin" most dead +disused- lan"ua"es. This code is replacin" ),C%%. Numbers without a si"n bit. These are alwa#s positi$e. ?isual displa# unit. Computer output is commonl# displa#ed on the ?4U. There

Si&n )it Si&n #la& Simulator

Si&ned .um)ers Numbers where the left most bit is the si"n bit.

Source (ode S' SR

Stac*

Stac* 'ointer

Status #la&s Status Re&ister Stepper motor Tas* Switchin&

Thermostat To*en ,ist

Twos complement !nicode !nsi&ned num)ers V4!

are se$eral ?4U displa# technolo"ies. =rite I #la& ) simple @indows word processor. 4ata is sa$ed to dis1 in a format uni3ue to the @rite pro"ram. The F or >ero fla" is set it the pre$ious calculation result was >ero.

%inar/ and He1adecimal


(ontents

(on5ertin& %etween %inar/ and He1


The C*U wor1s usin" binar#. (lectronicall# this is done with electronic switches that are either on or off. This is represented on paper b# nou"hts and ones. ) sin"le B%T or binar# di"it re3uires one wire or switch within the C*U. Usuall# data is handled in BKT(, or multiples of b#tes. ) B#te is a "roup of ei"ht bits. ) b#te loo1s li1e this

0-00-0-This is incon$enient to read/ sa# and write down so pro"rammers use he.adecimal to represent b#tes. Con$ertin" between binar# and he.adecimal is not difficult. Dirst split the b#te into two n#bbles +half a b#te- as follows

0-00
%I. RE 0000 00000-0 00-0-00 0-00--0 0---000 -00-0-0 -0---00 --0---0 ----

-0-H0F 40(IM , 0 2 3 6 5 8 9 ; < % ( 4 0 # 40(IM , 0 2 3 6 5 8 9 ; < -0 --2 -3 -6 -5

Then use the followin" table

0F M',0
,plit the b#te into two hal$es 0-00-0-- becomes 0-00 Usin" the table abo$e 0-00 is 0 -0-- is B The answer ... 0-00 -0-- is 6% in =e.adecimal. -0--

To con$ert the other wa# ta1e a he.adecimal such as (C. 5oo1 up 0 in the table. %t is ---0. 5oo1 up 9 in the table. %t is 0---. 09 is ---0 0---.

Instruction Set Summar/


(ontents

)5/ B5/ C5 and 45 are ei"ht bit/ "eneral purpose re"isters where data is stored. ,3uare brac1ets indicate 6)2 locations. Dor e.ample G18H means 6)2 location 18. 4ata can be mo$ed from a re"ister into into 6)2 and also from 6)2 into a re"ister. 6e"isters can be used as pointers to 6)2. GB5H is the 6)2 location that B5 points to. )ll numbers are in base 1 +=e.adecimal-.

Mo5e Instructions. #la&s .:T set.


ssem)ler 29? )5/18 29? B5/G18H 29? 29? 29? G18H/C5 45/G)5H GC5H/)5 Machine (ode 40 00 -5 4- 0- -5 42 -5 02 43 03 00 46 02 00 Machine (ode 0 00 0- 0- 02 2 02 03 3 03 00 6 03 5 00 00 0% 03 02 ( 00 04 0< 00 <% 0<( 02 <4 03 Machine (ode %0 00 -2 %- 0- -5 %2 02 03 %8 03 02 % 00 -0 %% 02 #0 %( 00 )5 B5 G18 H 45 GC5H Q 18 Q G18H Q C5 Q G)5H Q )5 01planation (op/ -5 into , (op/ R MJ-5K into , (op/ (, into R MJ-5K (op/ R MJ ,K into 4, (op/ , into R MJ(,K

4irect

rithmetic and ,o&ic. #la&s are set.


)5 Q )5 ! B5 B5 Q B5 : C5 C5 Q C5 P 45 45 Q 45 I )5 45 Q 45 ! 1 )5 Q )5 : 1 )5 Q )5 )N4 B5 C5 Q C5 96 B5 )5 Q )5 J96 B5 B5 Q N9T B5 6otate bits left. 5,B Q 2,B 6otate bits ri"ht. 2,B Q 5,B ,hift bits left. 4iscard 2,B. ,hift bits ri"ht. 4iscaed 5,B.

ssem)ler )44 )5/B5 ,UB B5/C5 2U5 C5/45 4%? 45/)5 %NC 45 4(C )5 )N4 )5/B5 96 C5/B5 J96 )5/B5 N9T B5 695 )5 696 B5 ,=5 C5 ,=6 45

Immediate
ssem)ler )44 )5/12 ,UB B5/18 2U5 C5/03 4%? 45/02 )N4 )5/10 96 C5/D0 J96 )5/))

rithmetic and ,o&ic. #la&s are set.


)5 B5 C5 45 )5 C5 )5 ! 12 B5 : 18 C5 P 3 45 I 2 )5 )N4 10 C5 96 D0 )5 J96 )5 Q )) Q Q Q Q Q Q

(ompare Instructions. #la&s are set.


ssem)ler C2* )5/B5 Machine (ode 01planation 4 00 0,et AFA fla" if )5 Q B5. ,et A,A fla" if )5 U B5.

C2*

B5/13

4% 0- -3

,et AFA fla" if B5 Q 13. ,et A,A fla" if B5 U 13. ,et AFA fla" if C5 Q G20H. ,et A,A fla" if C5 U G20H.

C2*

C5/G20H

4( 02 20

%ranch Instructions. #la&s .:T set.


4ependin" on the t#pe of &ump/ different machine codes can be "enerated. Eump instructions cause the instruction pointer +%*- to be altered. The lar"est possible &umps are !12C b#tes and :127 b#tes. The C*U fla"s control these &umps. The AFA fla" is set if the most recent calculation "a$e a Fero result. The A,A fla" is set if the most recent calculation "a$e a ne"ati$e result. The A9A fla" is set if the most recent calculation "a$e a result too bi" to fit in the re"ister. ssem)ler E2* =(6( Machine (ode 01planation (0 -2 %ncrease %* b# 12 (0 #0 4ecrease %* b# 2 +twos complement(- 0< (- <( (2 06 (2 #0 (3 0< (3 0(6 06 (6 00 (5 0< (5 4# (8 06 (8 #% %ncrease %* b# < if the AFA fla" is set. 4ecrease %* b# 100 if the AFA fla" is set. %ncrease %* b# 0 if the AFA fla" is N9T set. 4ecrease %* b# 1 if the AFA fla" is N9T set. %ncrease %* b# < if the A,A fla" is set. 4ecrease %* b# 31 if the A,A fla" is set. %ncrease %* b# 0 if the A,A fla" is N9T set. 4ecrease %* b# 32 if the A,A fla" is N9T set. %ncrease %* b# < if the A9A fla" is set. 4ecrease %* b# 33 if the A9A fla" is set. %ncrease %* b# 0 if the A9A fla" is N9T set. 4ecrease %* b# 8 if the A9A fla" is N9T set.

EF

T=(6(

ENF

)N*lace

E,

,T9*

EN,

,T)6T

E9

6(*()T

EN9

)')%N

'rocedures and Interrupts. #la&s .:T set.


C)55/ 6(T/ %NT and %6(T are a$ailable onl# in the re"istered $ersion. ssem)ler C)55 30 6(T %NT %6(T ssem)ler *U,= B5 *9* C5 *U,=D *9*D ssem)ler Machine (ode 01planation ( 30 ,a$e %* on the stac1 and &ump to the procedure at address 30. (% 6estore %* from the stac1 and &ump to it. (( 02 ,a$e %* on the stac1 and &ump to the address +interrupt $ector- retrie$ed from 6)2G02H. (4 6estore %* from the stac1 and &ump to it. Machine (ode 01planation 00 0B5 is sa$ed onto the stac1. 0- 02 C5 is restored from the stac1. 0 ,6 fla"s are sa$ed onto the stac1. 0% ,6 fla"s are restored from the stac1. Machine (ode 01planation

02

Stac* Manipulation Instructions. #la&s .:T set.

Input :utput Instructions. #la&s .:T set.

%N 9UT

0C 01

D0 0C D1 01 Machine (ode D( 00 DD DC D4 Code ori"in 4efine b#te 4efine b#te

4ata input from %I9 port 0C to )5. 4ata output to %I9 port 0C from )5. 01planation Close $isible peripheral windows. =alt the processor. 4o nothin" for one cloc1 c#cle. ,et the interrupt fla" in the ,tatus 6e"ister. Clear the interrupt fla" in the ,tatus 6e"ister. )ssembler directi$eL 'enerate code startin" from address 00. )ssembler directi$eL ,tore the ),C%% codes of A=elloA into 6)2. )ssembler directi$eL ,tore 70 into 6)2.

Miscellaneous Instructions. (,I and STI set I fla&.


ssem)ler C59 =)5T N9* ,T% C5% 96' 00 4B 4B B=elloB 70

4etailed Instruction Set


(ontents

The #ull Instruction Set


rithmetic ,o&ic Cump Instructions Mo5e Instructions (ompare Instructions Stac* Instructions 'rocedures nd Interrupts Inputs and :utputs :ther Instructions

General Information ('! Re&isters


There are four "eneral purpose re"isters called )5/ B5/ C5 and 45. There are three special purpose re"isters. These are %* is the instruction pointer. ,* is the stac1 pointer. ,6 is the status re"ister. This contains the %/ ,/ 9 and F fla"s.

#la&s
Dla"s "i$e information about the outcome of computations performed b# the C*U. ,in"le bits in the status re"ister are used as fla"s. This simulator has fla"s to indicate the followin". , The si"n fla" is set if a calculation "i$es a ne"ati$e result. 9 The o$erflow fla" is set if a result is too bi" to fit in 7 bits. F The >ero fla" is set if a calculation "i$es a >ero result. % is the hardware interrupts enabled fla".

2ost real life C*Us ha$e more than four fla"s.

Re&isters and Machine (odes


The re"isters and their e3ui$alent machine code numbers are shown below. Register na$es AL Ma&hine &o!es 00 0L 0H CL 0( 'L 09

01ample : To add one to the C5 re"ister use the instruction Asse$bl/ Co!e Ma&hine Co!e 7eC Ma&hine &o!e 0inar/ 2NC A? H0H00H00 CL 0( 000000H0

)0 is the machine instruction for the %NC command. 02 refers to the C5 re"ister. The assembler is not case sensiti$e. mo$ is the same as 29? and 2o$. @ithin the simulator/ he.adecimal numbers ma# not ha$e more than two he.adecimal di"its.

He1adecimal num)ers
18/ 3C and DD are e.amples of he.adecimal numbers. @hen usin" the assembler/ all numbers should be entered in he.adecimal. The C*U window displa#s the re"isters in binar#/ he.adecimal and decimal. 5oo1 at the He1adecimal and %inar/ pa"e for more detail.

.e&ati5e num)ers
D( is a ne"ati$e number. 5oo1 at the .e&ati5e .um)ers table for details of twos complement numbers. %n a b#te/ the left most bit is used as a si"n bit. This has a $alue of minus 127 decimal. B#tes can hold si"ned numbers in the ran"e :127 to !12C. B#tes can hold unsi"ned numbers in the ran"e 0 to 288.

Indirection
@hen referrin" to data in 6)2/ s3uare brac1ets are used. Dor e.ample G18H refers to the data at address 18he. in 6)2. The same applies to re"isters. GB5H refers to the data in 6)2 at the address held in B5. This is important and fre3uentl# causes confusion. These are indirect references. %nstead of usin" the number or the $alue in the re"ister directl#/ these $alues refer to 6)2 locations. These are also called pointers.

(omparin& with ;01;8 (hips


)t the mnemonic le$el/ the simulator instructions loo1 $er# li1e 70.7 assembl# code mnemonics. ,ufficient instructions are implemented to permit realistic pro"rammin" but the full instruction set has not been implemented. )ll the simulated instructions appl# to the low ei"ht bits of the 70.7 C*U. The rest of the C*U has not been simulated. %n the re"istered $ersion/ C)55/ 6(T/ %NT/ %6(T and simulated hardware interrupts are a$ailable so procedures and interrupts can be written. 2ost of the instructions beha$e as an 70.7 pro"rammer would e.pect. The 2U5 and 4%? +multiplication and di$ision- commands are simpler than the 70.7 e3ui$alents. The disad$anta"e of the simulator approach is that o$erflow is much more probable. The simulator $ersions of )44 and ,UB are realistic. The 707 4%? instruction calculates both 4%? and 294 in the same instruction. The simulator has 294 as a separate instruction. The machine codes are 3uite unli1e the 70.7 machine codes. The# are simpler/ less compact but desi"ned to ma1e the machine code as simple as possible.

@ith 70.7 machine code/ a mnemonic li1e 29? )5/18 is encoded in two b#tes. 29? )5/ is encoded into one b#te and the 18 "oes into another. This means that a lot of different machine 9* C94(, are needed for all the different combinations of 29? commands and re"isters. This simulator needs three b#tes. 29? is encoded as a b#te si>ed 9* C94(. )5 is encoded as a b#te containin" 00. The 18 "oes into a b#te as before. This is not $er# efficient but is $er# simple.

rithmetic and ,o&ic


4etailed Instruction Set

rithmetic Instructions " #la&s are set.


The (ommands
rithmetic dd : )ddition Su) : ,ubtraction Mul : 2ultiplication 4i5 : 4i$ision Mod : 6emainder after di$ision Inc : %ncrement +add one4ec : 4ecrement +subtract one,o&ic .4 : 5o"ical )N4 : 1 )N4 1 "i$es 1. )n# other input "i$es 0. :R : 5o"ical 96 : 0 96 0 "i$es 0. )n# other input "i$es 1. %itwise R:, : 6otate bits left. Bit at left end mo$ed to ri"ht end. R:R : 6otate bits ri"ht. Bit at ri"ht end mo$ed to left end.

F:R : 5o"ical e.clusi$e 96 : (3ual inputs SH, : ,hift bits left and discard "i$e 0. Non e3ual inputs "i$e 1. leftmost bit. .:T : 5o"ical N9T : %n$ert the input. 0 "i$es 1. 1 "i$es 0. SHR : ,hift bits ri"ht and discard ri"htmost bit.

(:MM .4S 4IR0(T 0F M',0S :' 44 S!% M!, 4IV M:4 I.( 40( .4 :R F:R .:T R:, R:R SH, SHR ssem)ler Machine (ode 01planation )44 ,UB 2U5 4%? 294 %NC 4(C )N4 96 J96 N9T 695 696 ,=5 ,=6 )5/B5 )0 00 01 C5/45 )1 02 03 )5/C5 )2 00 02 B5/45 )3 01 03 45/B5 ) 03 01 )5 B5 C5/45 C5 45 )5 B5 C5 )0 00 )8 01 )B 02 03 )4 02 <) 03 <B 00 <C 01 <4 02 )dd B5 to )5 ,ubtract 45 from C5 2ultipl# )5 b# C5 4i$ide B5 b# 45 6emainder after di$idin" 45 b# B5 )dd one to )5 4educt one from B5 C5 becomes C5 )N4 )5 C5 becomes C5 96 45 B5 becomes B5 J96 )5 %n$ert the bits in C5 Bits in 45 rotated one place left Bits in )5 rotated one place ri"ht Bits in B5 shifted one place left Bits in C5 shifted one place ri"ht

C5/)5 )) 02 00 B5/)5 )C 01 00

(:MM .4S IMM04I T0 0F M',0S :' 44 S!% M!, 4IV M:4 .4 :R F:R ssem)ler Machine (ode 01planation )44 ,UB 2U5 4%? 294 )N4 96 J96 )5/18 B0 00 18 B5/08 B1 01 08 )5/10 B2 00 10 B5/00 B3 01 00 45/20 B 03 20 C5/88 B) 02 88 C5/)) BB 02 )) B5/D0 BC 01 D0 )dd 18 to )5 ,ubtract 8 from B5 2ultipl# )5 b# 10 4i$ide B5 b# 0 6emainder after di$idin" 45 b# 20 C5 becomes C5 )N4 88 +01010101C5 becomes C5 96 )) +10101010B5 becomes B5 J96 D0

01amples
)44 )44 C5/)5 : )dd C5 to )5 and put the answer into C5. )5/22 : )dd 22 to )5 and put the answer into )5.

The answer alwa#s "oes into the first re"ister in the command.

4(C

B5 : ,ubtract one from B5 and put the answer into B5.

The other commands all wor1 in the same wa#.

#la&s
%f a calculation "i$es a >ero answer/ set the F >ero fla". %f a calculation "i$es a ne"ati$e answer/ set the , si"n fla". %f a calculation o$erflows/ set the 9 o$erflow fla". )n o$erflow happens if the result of a calculation has more bits than will fit into the a$ailable re"ister. @ith 7 bit re"isters/ the lar"est numbers that fit are :127 to ! 12C.

Cump Instructions
4etailed Instruction Set

Cump Instructions " #la&s are .:T set.


These instructions do N9T set the F/ , or 9 fla"s but conditional &umps use the fla"s to determine whether or not to &ump. The C*U contains a status re&ister " SR. This contains fla"s that are set or cleared dependin" on the most recent calculation performed b# the processor. The (M' compare instruction performs a subtraction li1e the S!% command. %t sets the fla"s but the result is not stored.

The #la&s " IS:I


1. I0R: : The F fla" is set if the most recent calculation "a$e a >ero result. 2. SIG. : The , fla" is set if the most recent calculation "a$e a ne"ati$e result. 3. :V0R#,:= : The 9 fla" is set if the most recent calculation "a$e a result too bi" to fit a
re"ister.

4. I.T0RR!'T : The % fla" is set in software usin" the STI command. %f this fla" is set/ the
C*U will respond to hardware interrupts. The (,I command clears the % fla" and hardware interrupts are i"nored. The % fla" is off b# default. The pro"rammer enters a command li1e CM' H0R0. The assembler con$erts this into machine code b# calculatin" how far to &ump. This tedious and error prone ta1s +for humans- is automated. %n an 7 bit re"ister/ the lar"est numbers that can be stored are :127 and !12C. This limits the ma.imum distance a &ump can "o. Ne"ati$e numbers cause the processor to &ump bac1wards towards >ero. *ositi$e numbers cause the processor to &ump forward towards 288. The &ump distance is added to I'/ the instruction pointer. To understand &umps properl#/ #ou also need to understand ne&ati5e num)ers. (:MM .4S 0F M',0S :' E2* E2* EF EF ENF ENF E, E, ssem)ler E2* =(6( E2* B)C; EF ,T9* EF ,T)6T Machine (ode C0 28 C0 D( C1 02 C1 D2 01planation Unconditional &ump. Dla"s are i"nored. Eump forward 25h 6)2 locations. Eump Unconditional &ump. Dla"s are i"nored. Eump bac1 "2d 6)2 locations. Eump Fero. Eump if the >ero fla" +F- is set. Eump forward +62h places if the +F- fla" is set. Eump Fero. Eump if the >ero fla" +F- is set. Eump bac1 "-6d places if the +F- fla" is set. Eump Not Fero. Eump if the >ero fla" +F- is .:T set. Eump forward 22h places if the +F- fla" is .:T set. Eump Not Fero. Eump if the >ero fla" +F- is .:T set. Eump bac1 "-;d places if the +F- fla" is .:T set. Eump ,i"n. Eump if the si"n fla" +,- is set. Eump forward -6h places if the si"n fla" +,- is set. Eump ,i"n. Eump if the si"n fla" +,- is set. Eump bac1 "6d places if the si"n fla" +,- is set.

ENF D96@)64 C2 22 ENF 6(* E, 2inus E, 2inus2 C2 (( C3 10 C3 DC

EN,

EN, *lus

C0 33

Eump Not ,i"n. Eump if the si"n fla" +,- is .:T set. Eump forward 33h places if the si"n fla" +,- is .:T set. Eump Not ,i"n. Eump if the si"n fla" +,- is .:T set. Eump bac1 "30d places if the si"n fla" +,- is .:T set. Eump 9$erflow. Eump if the o$erflow fla" +9- is set. Eump forward -2h places if the o$erflow fla" +9- is set. Eump 9$erflow. Eump if the o$erflow fla" +9- is set. Eump bac1 "33d places if the o$erflow fla" +9- is set. Eump Not 9$erflow. Eump if the o$erflow fla" +9- is .:T set. Eump forward 33h places if the o$erflow fla" +9- is .:T set. Eump Not 9$erflow. Eump if the o$erflow fla" +9- is .:T set. Eump bac1 "32d places if the o$erflow fla" +9- is .:T set.

EN, E9 E9 EN9

EN, *lus2 E9 TooBi" E9 6e4o EN9 9;

C0 (2 C8 12 C8 4D C 33

EN9

EN9 Bac1

C (0

The full 707 instruction set has man# other &umps. There are more fla"s in the 707 as wellO

,e&al 4estination ,a)els


hereL ) nice correct label. hereLL Not allowed 9nl# one colon is permitted. 1230L Not allowed. 5abels must be"in with a letter or ANA. NL here 9; but not human friendl#. 4estination labels must end in a colon.

,ome of these rules are not strictl# enforced in the simulator.

Mo5e Instructions
4etailed Instruction Set

Mo5e Instructions " #la&s are .:T set.


2o$e instructions are used to cop# data between re"isters and between 6)2 and re"isters. ddressin& Mode %mmediate 4irect +re"ister4irect +memor#%ndirect ssem)ler 01ample Supported 01planation mo$ al/10 mo$ al/bl mo$ al/G80H mo$ G00H/cl mo$ al/GblH mo$ GclH/dl %nde.ed mo$ al/G20 ! blH K(, N9 K(, K(, K(, K(, N9 Cop# 10 into )5 Cop# B5 into )5 Cop# data from 6)2 at address 80 into )5. Cop# data from C5 into 6)2 at address 00. B5 is a pointer to a 6)2 location. Cop# data from that 6)2 location into )5. C5 is a pointer to a 6)2 location. Cop# data from 45 into that 6)2 location. ) data table is held in 6)2 at address 20. B5 inde.es a data item within the data table. Cop# from the data table at address 20!B5 into )5. ) data table is held in 6)2 at address 20. B5 inde.es a data item within the data table. Cop# from )5 into the data table at address 20!B5. B5 points to a data table in memor#. ,% inde.es to a record inside the data table. B5 is called the Bbase re"isterB. ,% is called the Boffset or inde.B. Cop# from 6)2 at address B5!,% into )5. B5 points to a data table in memor#. ,% inde.es to a record inside the data table. B5 is called the Bbase re"isterB. ,% is called the BoffsetB. Cop# from )5 into 6)2 at address B5!,%.

mo$ G20 ! blH/al

N9

Base 6e"ister

mo$ al/Gbl!siH

N9

mo$ Gbl!siH/al

N9

Ri&ht to ,eft (on5ention

44R0SSI.G M:40S
Immediate
29? )5/10 Cop# a number into a re"ister. This is the simplest mo$e command and eas# to understand.

4irect Gre&isterH
29? )5/B5 Cop# one re"ister into another. This is eas# to understand. The simulator does not support this command. %f #ou ha$e to cop# from one re"ister to another/ use a 6)2 location or the stac1 to achie$e the mo$e.

4irect Gmemor/H
29? )5/G80H 29? G80H/)5 R Cop# from 6)2 into )5. Cop# the data from address 80. R Cop# from )5 into 6)2. Cop# the data to address 80.

The s3uare brac1ets indicate data in 6)2. The number in the s3uare brac1ets indicates the 6)2 addressIlocation of the data.

Indirect
29? )5/GB5H 29? GB5H/)5 R Cop# from 6)2 into )5. Cop# from the address that B5 points to. R Cop# from )5 into 6)2. Cop# to the address that B5 points to.

Cop# between a specified 6)2 location and a re"ister. The s3uare brac1ets indicate data in 6)2. %n this e.ample B5 points to 6)2.

Inde1ed
29? )5/G20 ! B5H 29? G20 ! B5H/)5 R Cop# from 6)2 into )5. The 6)2 address is located at 20!B5. R Cop# from )5 into 6)2. The 6)2 address is located at 20!B5.

=ere the B5 re"ister is used to Binde.B data held in a table. The table data starts at address 20.

%ase Re&ister
29? )5/GB5!,%H 29? GB5!,%H/)5 R Cop# from 6)2 into )5. The 6)2 address is located at B5!,%. R Cop# from )5 into 6)2. The 6)2 address is located at B5!,%.

B5 is the Bbase re"isterB. %t holds the start address of a data table. ,% is the Bsource inde.B. %t is used to inde. a record in the data table.

(ompare Instructions
4etailed Instruction Set

The (ompare (M' (ommand " #la&s are Set.


@hen the simulator does a comparison usin" C2*/ it does a subtraction of the two $alues it is comparin". The status re"ister fla"s are set dependin" on the result of the subtraction. The fla"s are set but the answer is discarded. +F+,%f the $alues are e3ual/ the subtraction "i$es a >ero result and the +F- >ero fla" is set. %f the number bein" subtracted was "reater than the other than a ne"ati$e answer results so the +,- si"n fla" is set. %f the number bein" subtracted is smaller than the other/ no fla"s are set. Use EF and E, or ENF and EN, to test the result of a C2* command.

4irect Memor/ (omparison


ssem)ler Machine (ode 01planation C2* C5/G20H 4C 02 20 =ere the C5 re"ister is compared with 6)2 location 20. @or1 out C5 : 6)2G20H. 4C is the machine instruction for direct memor# comparison. 02 refers to the )5 re"ister. 20 points to 6)2 address 20.

4irect Re&ister (omparison


ssem)ler Machine (ode 01planation C2* )5/B5 4) 00 01 =ere two re"isters are compared. @or1 out )5 : B5 4) is the machine instruction for re"ister comparison. 00 refers to the )5 re"ister. 01 refers to the B5 re"ister.

Immediate (omparison
ssem)ler Machine (ode 01planation C2* )5/04 4B 00 04 =ere the )5 re"ister is compared with 04/ +the ),C%% code of the (nter 1e#-. @or1 out )5 : 04. 4B is the machine instruction for re"ister comparison. 00 refers to the )5 re"ister. 04 is the ),C%% code of the (nter 1e#.

Stac* Instructions
4etailed Instruction Set

Stac* Instructions " #la&s are .:T set.


)fter pushin" items onto the stac1/ alwa#s pop them off in re$erse order. This is because the stac1 wor1s b# the ,ast In #irst :ut G,I#:H rule. The stac1 is an area of 6)2 used in this particular wa#. )n# part of 6)2 could be used. %n the simulator/ the stac1 is located &ust below the ?ideo 6)2 at address GBDH. The stac1 "rows towards >ero. %t is easil# possible to implement a stac1 that "rows the other wa#.

Stac* 01amples
ssem)ler Machine (ode 01planation *U,= B5 (0 01 *ush B5 onto the stac1 and subtract one from the stac1 pointer. (0 is the machine instruction for *U,=. 01 refers to the B5 re"ister. *9* B5 (1 01 )dd one to the stac1 pointer and pop B5 from the stac1. (1 is the machine instruction for *9*. 01 refers to the B5 re"ister. *U,=D *9*D () (B ,a$e the C*U status re"ister +,6- onto the stac1. This sa$es the C*U fla"s. 6estore the C*U status re"ister +,6- from the stac1. This restores the C*U fla"s.

The stac* is used to ...


sa$e re"ister contents for later restoration. pass parameters into procedures and return results. re$erse the order in which data is stored. sa$e addresses so procedures and interrupts can return to the ri"ht place. perform postfi. arithmetic. ma1e recursion possible.

Stac* 'ointer
) C*U re"ister +,*- that 1eeps trac1 of +is a pointer to- the data on the stac1. %t is colour coded with a blue hi"hli"ht in the simulator 6)2 displa#.

'ush and 'op


'ush : )dd data to the stac1 at the stac1 pointer position and subtract one from the stac1 pointer. 'op : )dd one to the stac1 pointer and remo$e data from the stac1 at the stac1 pointer position.

,I#:
5ast in Dirst out. The stac1 operates strictl# to this rule. @hen data is pushed onto the stac1/ it must later be popped in re$erse order.

Stac* :5erflow
The stac1 is repeatedl# pushed until it is full. The simulator does not detect this condition and the stac1 can o$erwite pro"ram code or data. 6eal life pro"rams can fail in the same wa#.

Stac* !nderflow
The stac1 is repeatedl# popped until it is empt#. The ne.t pop causes an underflow.

'rocedures and Interrupts


4etailed Instruction Set

'rocedures and Interrupts " #la&s are .:T set.


These are a$ailable in the re"istered $ersion. *lease re"ister. %t is essential to sa$e the re"isters and fla"s used b# an# procedure or interrupt and restore them after the procedure or interrupt has finished its wor1. Use push and pushf to sa$e. Use pop and popf to restore $alues.

ssem)ler Machine (ode 01planation C)55 30 C) 30 Call the procedure at address 30. The return address is pushed onto the stac1 and the %nstruction *ointer +%*- is set to 30. C) is the machine instruction for C)55. 30 is the address of the start of the procedure bein" called. 6eturn from the procedure. ,et the %nstruction *ointer +%*- to the return address popped off the stac1. CB is the machine instruction for 6eturn. The %nstruction *ointer +%*- is set to the address of the interrupt $ector retrie$ed from 6)2 address 03. The return address is pushed onto the stac1. CC is the machine instruction for %NT. 03 is the address of the interrupt $ector used b# the %NT command. 6eturn from the interrupt. ,et the %nstruction *ointer +%*- to the return address popped off the stac1. C4 is the machine instruction for %6(T.

6(T

CB

%NT 03

CC 03

%6(T

C4

Input :utput Instructions


4etailed Instruction Set

Input and :utput Instructions " #la&s are .:T set.


The simulator has 1 ports numbered from 00 to 0D. These are connected to simulated/ outside: world peripherals.

ssem)ler Machine (ode 01planation %N 0C D0 0C %nput from *ort 0C. D0 is the machine instruction for %nput. 0C is the port number. 9utput to *ort 01. D1 is the machine instruction for 9utput. 01 is the port number.

9UT 01

D1 01

'eripherals
'ort 4escription 00 002 03 06 05 08 09 0; %nput from port 00 for simulated 1e#board input. 9utput to port 01 to control the traffic li"hts. 9utput to port 02 to control the se$en se"ment displa#s. 9utput to port 03 to control the heater. %nput from port 03 to sense the thermostat state. 9utput to port 00 to control the sna1e in the ma>e. 9utput to port 08 to control the stepper motor. 9utput to port 0 to control the lift. 9utput to port 0C to ma1e the 1e#board $isible. %nput from port 0C to read the 1e#board ),C%% code. 9utput to port 07 to ma1e the numeric 1e#pad $isible. %nput from port 07 to read from the numeric 1e#pad.

0<"0# Unused

:ther Instructions
4etailed Instruction Set

Miscellaneous Instructions " (,I and STI control the GIH #la&
ssem)ler Machine (ode =)5T 00 01planation ,top the pro"ram. 00 is the machine instruction for =)5T. The pro"ram will cease to run if it encounters a =)5T instruction. Continuous runnin" is cancelled b# this command. Kou can ha$e se$eral halt commands in one pro"ram. There should be onl# one (N4 and code after (N4 is i"nored. 4o nothin" for one cloc1 c#cle. DD is the machine instruction for N9*. The pro"ram will do nothin" for one cloc1 c#cle. The pro"ram then continues as normal. N9* is used to introduce time dela#s to allow slow electronics to 1eep up with the C*U. These are also called @)%T ,T)T(,. Close all the peripheral windows. D( is the machine code for C59. %t applies to this simulator onl#/ and is used to close peripheral windows. This ma1es it easier to write demonstration pro"rams without the screen "ettin" too cluttered. Code 9ri"in. 'enerate code startin" from this address. To "enerate code from a startin" address other than >ero use 96'. This is useful to place procedures/ interrupts or data tables at particular addresses in memor#. 96' is an assembler directi$e and no code is "enerated. 4efine a b#te. ,tore the b#te +70- in the ne.t free 6)2 location. Use 4B to create data tables containin" b#tes of data. Use B4 to define an %nterrupt ?ector.

N9*

DD

C59

D(

96' 30

N9N(

4B 70

70

4B B=elloB

07/ 8/ C/ C/ D 4efine a strin". ,tore the ),C%% codes of the te.t in 3uotes in the ne.t free 6)2 locations. Use 4B to store te.t strin"s. The stored ),C%% codes do not include an end:of:strin" mar1er. Use 4% 00 for this. D4 Clear the % fla" %f the % fla" is cleared/ hardware interrupts are i"nored. This is the default state for the simulator. 6esettin" the C*U will also clear the % fla". The timer that "enerates hardware interrupts will do nothin". ,et the % fla" %f the % fla" is set/ the simulator will "enerate %NT 02 at re"ular time inter$als. %t is necessar# to ha$e an interrupt $ector stored at address 02 that points to interrupt handler code stored elsewhere. The inter$al between timer interrupts can be set usin" the slider in the Confi"uration Tab. %f interrupts occur faster than the processor can handle them/ a

C5%

,T%

DC

simulated s#stem crash will follow. )d&ust the C*U cloc1 speed and the timer inter$al to pre$ent this or cause it if #ou want to see what happens. %t is possible to pro"ram the simulator usin" pure machine codes. =ere is a simple e.ample. ; ===== NORMAL CO'E ===== MOV AL,0 2NC AL EN' ; ===== NORMAL CO'E ===== =ere is the same pro"ram in pure machine code apart from the re3uired (N4 1e#word. This should run e.actl# as the pro"ram abo$e. ; ===== PURE MAC72NE CO'E ===== '0 '0 ; MOV '0 00 ; AL '0 00 ; 0 '0 A? ; 2NC '0 00 ; AL EN' ; ===== PURE MAC72NE CO'E ===== This is an interestin" e.ercise but rather defeats the whole point of usin" an assembler. %f #ou ha$e a do"/ wh# bar1 #ourselfS 2anuall# calculatin" &ump distances mi"ht be a useful learnin" e.ercise/ especiall# for ne"ati$e &umps.

,ist #ile
(ontents

The ,ist #ile

%n the list file/ #our ori"inal pro"ram is shown. Numbers in s3uare blac1ets such as G1CH are the addresses at which the machine codes were "enerated. The machine codes are shown.

Here is a t/pical line.


MOV CL,C0 The The The The ; ;H0< '0 0( C0 ; Vi!eo ra$ base a!!ress command is to mo$e C0 into the )5 re"ister. machine code was "enerated at address G10H. machine codes are 40 00 C0. pro"rammerAs comment is reproduced.

.e&ati5e .um)ers
(ontents

.e&ati5e .um)ers
4ec He1 :127 :120 :112 :100 :0< :077 :070 :0C2 :0 0 :08 :007 :000 :032 :020 :01 :007 70 77 <0 <7 )0 )7 B0 B7 C0 C7 40 47 (0 (7 D0 D7 4ec He1 :12C :11< :111 :103 :0<8 :07C :0C< :0C1 :0 3 :088 :00C :03< :031 :023 :018 :00C 71 7< <1 << )1 )< B1 B< C1 C< 41 4< (1 (< D1 D< 4ec He1 :12 :117 :110 :102 :0<0 :07 :0C7 :0C0 :0 2 :080 :00 :030 :022 :010 :00 72 7) <2 <) )2 )) B2 B) C2 C) 42 (2 () D2 D) 4ec He1 :128 :11C :10< :101 :0<3 :078 :0CC :0 < :0 1 :083 :008 :02< :021 :013 :008 73 7B <3 <B )3 )B B3 BB C3 CB 43 (3 (B D3 DB 4ec He1 :120 :11 :107 :100 :0<2 :070 :0C :0 7 :0 0 :082 :000 :03 :027 :020 :012 :000 70 7C <0 <C )0 )C B0 BC C0 CC 40 4C (0 (C D0 DC 4ec He1 :123 :118 :10C :0<< :0<1 :0C8 :08< :003 :02C :01< :011 :003 78 74 <8 <4 )8 B8 C8 48 (8 (4 D8 D4 4ec He1 :122 :110 :10 :0<7 :0<0 :072 :0C0 :0 :087 :080 :002 :030 :02 :017 :010 :002 7 7( < <( ) )( B B( C C( 4 4( ( (( D D( 4ec He1 :121 :113 :108 :0<C :07< :071 :0C3 :0 8 :08C :00< :001 :033 :028 :01C :00< :001 7C 7D <C <D )C )D BC BD CC CD 4C 4D (C (D DC DD

:073 )4 :0 C B4 :081 C4 :038 44

:037 4)

:03C 4B

'ositi5e .um)ers
4ec He1 !000 00 !007 07 !01 10 !020 17 !032 20 !000 27 !007 30 !08 37 !0 0 00 !0C2 07 !070 80 !077 87 !0< !100 0 7 4ec He1 !001 01 !00< 0< !01C 11 !028 17 !033 21 !001 2< !00< 31 !08C 3< !0 8 01 !0C3 0< !071 81 !07< 8< !0<C !108 1 < 4ec He1 !002 02 !010 0) !017 12 !02 1) !030 22 !002 2) !080 32 !087 3) !0 02 !0C0 0) !072 82 !0<0 8) !0<7 !10 3 ) 4ec He1 !003 03 !011 0B !01< 13 !02C 1B !038 23 !003 2B !081 33 !08< 3B !0 C 03 !0C8 0B !073 83 !0<1 8B !0<< !10C 3 B 4ec He1 !000 00 !012 0C !020 10 !027 1C !03 20 !000 2C !082 30 !0 0 3C !0 7 00 !0C 0C !070 80 !0<2 8C !100 !107 0 C 4ec He1 !008 08 !013 04 !021 18 !02< 14 !03C 28 !008 24 !083 38 !0 1 34 !0 < 08 !0CC 04 !078 88 !0<3 84 !101 !10< 8 4 4ec He1 !00 0 !010 0( !022 1 !030 1( !037 2 !00 2( !080 3 !0 2 3( !0C0 0 !0C7 0( !07 !102 !110 ( 8 !0<0 8( 4ec He1 !00C 0C !018 0D !023 1C !031 1D !03< 2C !00C 2D !088 3C !0 3 3D !0C1 0C !0C< 0D !07C 8C !0<8 8D !103 !111 C D

!112 C0 !120 C7

!113 C1 !121 C<

!110 C2 !122 C)

!118 C3 !123 CB

!11

C0

!11C C8 !128 C4

!117 C !12 C(

!11< CC !12C CD

!120 CC

Two@s complement
The numbers wor1 as follows. The leftmost bit in an ei"ht bit b#te is the si"n bit. H 0 H 0 H 0 H 0 S S The sign bit has a val e o3 6H(M !e&i$al or 6M0 heCa!e&i$al. The other se$en bits are treated as a normal positi$e number between 0 and 12C. This is true whether the o$erall number is positi$e or ne"ati$e. Dor e.ample to store :1 the binar# is H H H H H H H H S S 6H(M! 6 H(M! R H(=! = 6H!

To store 12C decimal/ the binar# is 0 H H H H H H H 0 R H(=! = H(=! S S The sign bit is 4ero. 1 and 32 bit machines also use the leftmost bit as the si"n bit. The ne"ati$e numbers wor1 in e.actl# the same wa# but much bi""er niumbers can be stored. %n a 1 bit machine/ the si"n bit is worth :32C 7. %n a 32 bit machine/ the si"n bit is worth :210C073 07 +2000 million appro.imatel#-.

'op"up Help
(ontents

44

.4 ( ,, (,I (,: (M' 4% 40( 4IV 0.4 H ,T I. I.( I.T

IR0T CM' C.: C.S C.I C: CS CI M:4 M:V M!, .:' .:T :R :RG :!T ':' ':'# '!SH '!SH# R0T R:, R:R SH, SHR STI S!% F:R

('! General 'urpose Re&isters


The C*U is where all the arithmetic and lo"ic +decision ma1in"- ta1es place. The C*U has stora"e locations called re"isters. The C*U has fla"s which indicate >ero/ ne"ati$e or o$erflowed calculations. 2ore information is included in the description of the s#stem architecture. The C*U re"isters are called )5/ B5/ C5 and 45. The machine code names are 00/ 01/ 02 and 03. 6e"isters are used for storin" binar# numbers. 9nce the numbers are in the re"isters/ it is possible to perform arithmetic or lo"ic. ,endin" the correct binar# patterns to peripherals li1e the traffic li"hts/ ma1es it possible to control them. R semicolon be"ins a pro"ram comment. Comments are used to document pro"rams. The# are helpful to new pro"rammers &oinin" a team and to e.istin" people returnin" to a pro&ect ha$in" for"otten what it is about. 'ood comments e.plain =HE thin"s are bein" done. *oor comments simpl# repeat the code or state the totall# ob$ious.

Ram

ddresses

(.amples GCDH G22H G)5H GC5H GCDH the contents of 6)2 at location CD GC5H the contents of the 6)2 location that C5 points to. C5 contains a number that is used as the address.

The Instruction Set


'op"up Help

44 "
)ssembler )44 B5/C5 )44 )5/12

dd two 5alues to&ether


2achine Code )0 01 02 B0 00 12 (.planation )dd C5 to B5. )nswer "oes into B5 )dd 12 to )5. )nswer "oes into )5

('! fla&s are set

'op"up Help

.4 " ,o&ical
)ssembler )N4 B5/C5 )N4 )5/12

.4 two 5alues to&ether


(.planation )N4 C5 with B5. )nswer "oes into B5 )N4 12 with )5. )nswer "oes into )5

('! fla&s are set


2achine Code )) 01 02 B) 00 12

The )N4 rule is that two ones "i$e a one. )ll other inputs "i$e nou"ht. 5oo1 at this e.ample... H0H0H0H0 0000HHHH 66666666 AN-+ER 0000H0H0 The le3t 3o r bits are $as*e! to 0. 'op"up Help

( ,, and R0T
('! fla&s are .:T set
)ssembler 2achine Code (.planation C)55 80 C) 80 Call the procedure at address 80. The C*U pushes the instruction pointer $alue %* ! 2 onto the stac1. 5ater the C*U returns to this address. %* is then set to 80. The C*U instruction pointer is set to 80. The C*U e.ecutes instructions from this address until it reaches the 6(T command. %t then pops the $alue of %* off the stac1 and &umps to this address where e.ecution resumes.

6(T

CB

'op"up Help

(,I and STI


('! GIH fla& is set/cleared
)ssembler 2achine Code (.planation ,T% C5% DC D4 ,T% sets the %nterrupt fla". C5% clears the %nterrupt fla" A%A in the status re"ister. ,T% sets the interrupt fla" A%A in the status re"ister. The machine code for C5% is D4. The machine code for ,T% is DC. %f +%- is set/ the C*U will respond to interrupts. The simulator "enerates a hardware interrupt at re"ular time inter$als that #ou can ad&ust. %f A%A is set/ there should be an interrupt $ector at address G02H. The C*U will &ump to the code that this $ector points to whene$er there is an interrupt.

'op"up Help

(,:

('! fla&s are .:T set


)ssembler 2achine Code (.planation C59 D( Close unwanted peripheral windows. C59 is not an .7 command. %t closes all unnecessar# simulator windows which would otherwise ha$e to be closed manuall# one b# one.

'op"up Help

(M'
('! fla&s are set
)ssembler C2* )5/04 2achine Code 4B 00 04 (.planation Compare )5 with 04 %f the $alues bein" compared are ... (VU)5 set the AFA fla". )5 less than 04 set the A,A fla". )5 "reater than 04 set no fla"s. Compare )5 with B5 %f the $alues bein" compared are ... (VU)5 set the AFA fla". )5 less than B5 set the A,A fla". )5 "reater than B5 set no fla"s. Compare C5 with 20 %f the $alues bein" compared are ... (VU)5 set the AFA fla". C5 less than 6)2G20H set the A,A fla". C5 "reater than 6)2G20H set no fla"s.

C2* )5/B5

4) 00 01

C2* C5/G20H

4C 02 20

'op"up Help

4%
('! fla&s are .:T set
)ssembler 2achine Code 4B 4B 4B 4B 22 33 00 0 22 33 00 00 (.planation 4efine B#te 4B "i$es a method for loadin" $alues directl# into 6)2. 4B does not ha$e a machine code. The numbers or te.t after 4B are loaded into 6)2. Use 4B to set up data tables. ),C%% codes are loaded into 6)2.

4B B=elloB 07 8 C C D 4B 0 00 'op"up Help

(nd of te.t is mar1ed b# NU55

40( and I.(


('! fla&s are set
)ssembler %NC B5 2achine Code )0 01 (.planation )dd one to B5.

4(C )5

)8 00

,ubtract one from )5.

'op"up Help

4IV and M:4


('! fla&s are set
)ssembler 4%? )5/8 4%? )5/B5 294 )5/8 2achine Code B3 00 08 )3 00 01 B 00 08 (.planation 4i$ide )5 b# 8. )nswer "oes into )5. 4%? differs from the .7 4%?. 4i$ide )5 b# B5. )nswer "oes into )5. 4%? differs from the .7 4%?. 294 )5 b# 8. 6emainder after di$ision "oes into )5. 294 is not an .7 command. 294 )5 b# B5. 6emainder after di$ision "oes into )5. 294 is not an .7 command.

294 )5/B5

) 00 01

The .7 4%? calculates di$ and mod in one command. The answers are put into different re"isters. This is not possible with the 7 bit simulator so di$ and mod are separated and simplified. 7 4%? 3 is 3 +with remainder 2-. 7 294 3 is 2 'op"up Help

0.4
('! fla&s are .:T set
)ssembler 2achine Code (N4 00 (.planation (N4 stops further pro"ram e.ecution. The simulator achie$es this b# stoppin" the C*U cloc1. (N4 is also an assembler directi$e. )ll code after (N4 is i"nored b# the assembler. There should be onl# one (N4 in a pro"ram.

'op"up Help

H ,T
('! fla&s are .:T set
)ssembler 2achine Code (.planation =)5T 00 =)5T stops further pro"ram e.ecution. The simulator achie$es this b# stoppin" the C*U cloc1. =)5T is not an assembler directi$e. +,ee (N4There can be an# number of =)5T commands in a pro"ram.

'op"up Help

I. and :!T
('! fla&s are .:T set
)ssembler 2achine Code (.planation %N 0C D0 0C %nput from port 0C. The data is stored in the )5 re"ister.

9UT 03

D1 03

9utput to port 03. The data comes from the )5 re"ister.

'op"up Help

I.( and 40(


('! fla&s are set
)ssembler %NC B5 4(C )5 'op"up Help 2achine Code )0 01 )8 00 (.planation )dd one to B5. ,ubtract one from )5.

I.T and IR0T


('! fla&s are .:T set
)ssembler 2achine Code (.planation %NT 02 CC 02 The return address +%* ! 2- is pushed onto the stac1. The stac1 pointer +,*- is reduced b# one. 6)2 location 02 contains the address of the %nterrupt =andler. This address is BfetchedB and %* is set to it. The return address is popped off the stac1. The stac1 pointer +,*- is increased b# one. %* is set to the return address popped off the stac1.

%6(T

C4

'op"up Help

CM'
('! fla&s are .:T set and the fla&s are i&nored
)ssembler E2* Dorward 2achine Code C0 12 (.planation ,et %* to a new $alue )dd 12 to %* The assembler calculates the &ump distance. The bi""est possible forward &ump is !12C. ,et %* to a new $alue )dd :2 to %* D( is :2. This is e1plained here. The assembler calculates the &ump distance. The bi""est possible bac1ward &ump is :127.

E2* Bac1

D(

'op"up Help

C.:
('! fla&s are .:T set. C.: uses the G:H fla&.
The +9- fla" is set if a calculation "i$es a result too bi" to fit in an 7 but re"ister. )ssembler EN9 Dorward 2achine Code C 12 (.planation Eump if the +9- fla" is N9T set. %f the +9- fla" is N9T set/ &ump forward 12 places. %f the +9- fla" is N9T set/ add 12 to +%*-. %f the +9- fla" is set/ add 2 to +%*-. The assembler calculates the &ump distance. The bi""est possible forward &ump is !12C.

EN9 Bac1

C D(

Eump if the +9- fla" is N9T set. %f the +9- fla" is N9T set/ &ump bac1 2 places. %f the +9- fla" is N9T set/ add :2 to +%*-. %f the +9- fla" is set/ add 2 to +%*-. The assembler calculates the &ump distance. The bi""est possible bac1ward &ump is :127. D( is :2. This is e1plained here.

'op"up Help

C.S
('! fla&s are .:T set. C.S uses the GSH fla&.
The +,- fla" is set if a calculation "i$es a ne"ati$e result. )ssembler EN, Dorward 2achine Code C0 12 (.planation Eump if the +,- fla" is N9T set. %f the +,- fla" is N9T set/ &ump forward 12 places. %f the +,- fla" is N9T set/ add 12 to +%*-. %f the +,- fla" is set/ add 2 to +%*-. The assembler calculates the &ump distance. The bi""est possible forward &ump is !12C. Eump if the +,- fla" is N9T set. %f the +,- fla" is N9T set/ &ump bac1 2 places. %f the +,- fla" is N9T set/ add :2 to +%*-. %f the +,- fla" is set/ add 2 to +%*-. The assembler calculates the &ump distance. The bi""est possible bac1ward &ump is :127. D( is :2. This is e1plained here.

EN, Bac1

C0 D(

'op"up Help

C.I
('! fla&s are .:T set. C.I uses the GIH fla&.
The +F- fla" is set if a calculation "i$es a >ero result. )ssembler ENF Dorward 2achine Code C2 12 (.planation Eump if the +F- fla" is N9T set. %f the +F- fla" is N9T set/ &ump forward 12 places. %f the +F- fla" is N9T set/ add 12 to +%*-. %f the +F- fla" is set/ add 2 to +%*-. The assembler calculates the &ump distance. The bi""est possible forward &ump is !12C. Eump if the +F- fla" is N9T set. %f the +F- fla" is N9T set/ &ump bac1 2 places. %f the +F- fla" is N9T set/ add :2 to +%*-. %f the +F- fla" is set/ add 2 to +%*-. The assembler calculates the &ump distance. The bi""est possible bac1ward &ump is :127. D( is :2. This is e1plained here.

ENF Bac1

C2 D(

'op"up Help

C:
('! fla&s are .:T set. C: uses the G:H fla&.

The +9- fla" is set if a calculation "i$es a result too bi" to fit in an 7 but re"ister. )ssembler E9 Dorward 2achine Code C8 12 (.planation Eump if the +9- fla" is set. %f the +9- fla" is set/ &ump forward 12 places. %f the +9- fla" is set/ add 12 to +%*-. %f the +9- fla" is N9T set/ add 2 to +%*-. The assembler calculates the &ump distance. The bi""est possible forward &ump is !12C. Eump if the +9- fla" is set. %f the +9- fla" is set/ &ump bac1 2 places. %f the +9- fla" is set/ add :2 to +%*-. %f the +9- fla" is N9T set/ add 2 to +%*-. The assembler calculates the &ump distance. The bi""est possible bac1ward &ump is :127. D( is :2. This is e1plained here.

E9 Bac1

C8 D(

'op"up Help

CS
('! fla&s are .:T set. CS uses the GSH fla&.
The +,- fla" is set if a calculation "i$es a ne"ati$e result. )ssembler E, Dorward 2achine Code C3 12 (.planation Eump if the +,- fla" is set. %f the +,- fla" is set/ &ump forward 12 places. %f the +,- fla" is set/ add 12 to +%*-. %f the +,- fla" is N9T set/ add 2 to +%*-. The assembler calculates the &ump distance. The bi""est possible forward &ump is !12C. Eump if the +,- fla" is set. %f the +,- fla" is set/ &ump bac1 2 places. %f the +,- fla" is set/ add :2 to +%*-. %f the +,- fla" is N9T set/ add 2 to +%*-. The assembler calculates the &ump distance. The bi""est possible bac1ward &ump is :127. D( is :2. This is e1plained here.

E, Bac1

C3 D(

'op"up Help

CI
('! fla&s are .:T set. CI uses the GIH fla&.
The +F- fla" is set if a calculation "i$es a >ero result. )ssembler EF Dorward 2achine Code C1 12 (.planation Eump if the +F- fla" is set. %f the +F- fla" is set/ &ump forward 12 places. %f the +F- fla" is set/ add 12 to +%*-. %f the +F- fla" is N9T set/ add 2 to +%*-. The assembler calculates the &ump distance. The bi""est possible forward &ump is !12C. Eump if the +F- fla" is set. %f the +F- fla" is set/ &ump bac1 2 places.

EF Bac1

C1 D(

%f the +F- fla" is set/ add :2 to +%*-. %f the +F- fla" is N9T set/ add 2 to +%*-. The assembler calculates the &ump distance. The bi""est possible bac1ward &ump is :127. D( is :2. This is e1plained here.

'op"up Help

4IV and M:4


('! #la&s are Set
)ssembler 4%? )5/8 4%? )5/B5 294 )5/8 2achine Code B3 00 08 )3 00 01 B 00 08 (.planation 4i$ide )5 b# 8. )nswer "oes into )5. 4%? differs from the .7 4%?. 4i$ide )5 b# B5. )nswer "oes into )5. 4%? differs from the .7 4%?. 294 )5 b# 8. 6emainder after di$ision "oes into )5. 294 is not an .7 command. 294 )5 b# B5. 6emainder after di$ision "oes into )5. 294 is not an .7 command.

294 )5/B5

) 00 01

The .7 4%? calculates di$ and mod in one command. The answers are put into different re"isters. This is not possible with the 7 bit simulator so di$ and mod are separated and simplified. 7 4%? 3 is 3 +with remainder 2-. 7 294 3 is 2

'op"up Help

M:V ('! fla&s are .:T set


ddressin& Mode %mmediate ssem)ler 01ample Supported 01planation Machine (ode mo$ al/10 40 00 -0 4irect +re"ister4irect +memor#mo$ al/bl mo$ al/G80H 4- 00 50 mo$ G00H/cl 42 60 02 %ndirect mo$ al/GblH 43 00 0mo$ GclH/dl 46 02 03 %nde.ed mo$ al/G20 ! blH N9 K(, K(, K(, N9 K(, Cop# B5 into )5 Cop# data from 6)2 at address 80 into )5. G80H is a pointer to data held in a 6)2 location. Cop# data from C5 into 6)2 at address 00. G00H is a pointer to data held in a 6)2 location. B5 is a pointer to a 6)2 location. Cop# data from that 6)2 location into )5. C5 is a pointer to a 6)2 location. Cop# data from 45 into that 6)2 location. ) data table is held in 6)2 at address 20. B5 inde.es a data item within the data table. Cop# from the data table at address 20!B5 into )5. ) data table is held in 6)2 at address 20. B5 inde.es a data item within the data table. Cop# from )5 into the data table at address 20!B5. B5 points to a data table in memor#. ,% inde.es to a record inside the data table. B5 is called the Bbase re"isterB. ,% is called the Boffset or inde.B. Cop# from 6)2 at address B5!,% into )5. B5 points to a data table in memor#. ,% inde.es to a record inside the data table. B5 is called the Bbase re"isterB. ,% is called the BoffsetB. Cop# from )5 into 6)2 at address B5!,%. K(, Cop# 10 into )5

mo$ G20 ! blH/al

N9

Base 6e"ister

mo$ al/Gbl!siH

N9

mo$ Gbl!siH/al

N9

'op"up Help

M!,
('! #la&s are Set
)ssembler 2U5 )5/B5 2U5 C5/12 2achine Code )2 00 01 B2 02 12 (.planation 2ultipl# )5 b# B5. The result "oes into )5 2U5 differs from the .7 2U5. 2ultipl# C5 b# 12. The result "oes into C5 2U5 differs from the .7 2U5.

The .7 2U5 places the result into more than one re"ister. This is not possible with the 7 bit simulator so 2U5 has been simplified. ) disad$anta"e is that an o$erflow is much more li1el# to occur. 'op"up Help

.:'
('! #la&s are .:T Set
)ssembler 2achine Code (.planation N9* DD 4o nothin". 4o nothin" for one C*U cloc1 c#cle. This is needed to 1eep the C*U s#nchronised with accuratel# timed electronic circuits. The C*U mi"ht need to dela# before the electronics are read#.

'op"up Help

.:T
('! #la&s are Set
)ssembler N9T 45 2achine Code )4 03 (.planation %n$ert all the bits in 45.

%f 45 contained 01010101/ after usin" N9T it will contain 10101010. 'op"up Help

:R
('! #la&s are Set
)ssembler 96 )5/12 96 B5/C5 2achine Code BB 00 12 )B 01 02 (.planation 9r 12 with )5. )nswer "oes into )5 9r C5 with B5. )nswer "oes into B5

The 96 rule is that two nou"hts "i$e a nou"ht. )ll other inputs "i$e one. H0H0H0H0 OR 0000HHHH 66666666 = H0H0HHHH

'op"up Help

:RG
('! #la&s are .:T Set
)ssembler 2achine Code (.planation 96' 80 None 96' is not a C*U instruction. %t is an instruction to the assembler to tell it to "enerate code at a particular address. %t is useful for writin" procedures and interrupts. %t can also be used to specif# where in memor#/ data tables "o.

'op"up Help

:!T and I.
('! #la&s are .:T Set
)ssembler 2achine Code (.planation %N 0C 9UT 03 D0 0C D1 03 %nput from port 0C. The data is stored in the )5 re"ister. 9utput to port 03. The data comes from the )5 re"ister.

'op"up Help

'!SHD ':'D '!SH# and ':'#


('! #la&s are .:T Set
)ssembler *U,= )5 *9* B5 *U,=D *9*D 2achine Code (0 00 (1 01 () (B (.planation ,a$e )5 onto the stac1. 4educt one from the ,tac1 *ointer +,*)dd one to the stac1 pointer +,*-. 6estore B5 from the stac1 *ush the C*U fla"s from the status re"ister +,6- onto the stac1. 4educt one from the ,tac1 *ointer +,*)dd one to the stac1 pointer +,*-. *9* the C*U fla"s from the stac1 into the ststus re"ister +,6-.

*U,= sa$es a b#te onto the stac1. *9* "ets it bac1.The stac1 is an area of memor# that obe#s the 5%D9 rule : 5ast %n Dirst 9ut. @hen pushin" items onto the stac1/ remember to pop them off a"ain in e.act re$erse order. The stac1 can be used to 1. hold the return address of a procedure call 2. hold the return address of an interrupt call 3. pass parameters into procedures 0. "et results bac1 from procedures 8. sa$e and restore re"isters and fla"s . re$erse the order of data. 'op"up Help

R0T and ( ,,
('! #la&s are .:T Set

)ssembler C)55 80

2achine Code C) 80

(.planation Call the procedure at address 80. The C*U pushes the instruction pointer $alue %* ! 2 onto the stac1. 5ater the C*U returns to this address. %* is then set to 80. The C*U instruction pointer is set to 80. The C*U e.ecutes instructions from this address until it reaches the 6(T command. %t then pops the $alue of %* off the stac1 and &umps to this address where e.ecution resumes.

6(T

CB

'op"up Help

R:, and R:R


('! #la&s are Set
)ssembler 695 )5 2achine Code <) 00 (.planation 6otate the bits in )5 left one place. The leftmost bit is mo$ed to the ri"ht end of the b#te. Before 695 10000110 : )fter 695 00001101 6otate the bits in 45 ri"ht one place. The ri"htmost bit is mo$ed to the left end of the b#te. Before 696 10000110 : )fter 696 01000011

696 45

<B 03

'op"up Help

SH, and SHR


('! #la&s are Set
)ssembler ,=5 )5 2achine Code <C 00 (.planation ,hift bits left one place. The leftmost bit is discarded. Before ,=5 10000110 : )fter ,=5 00001100 ,hift bits ri"ht one place. The ri"htmost bit is discarded. Before ,=6 10000110 : )fter ,=6 01000011

,=6 45

<4 03

'op"up Help

STI and (,I


('! #la&s are .:T Set
)ssembler 2achine Code ,T% C5% DC D4 (.planation ,T% sets the %nterrupt fla". C5% clears the %nterrupt fla" A%A in the status re"ister. ,T% sets the interrupt fla" A%A in the status re"ister. The machine code for C5% is D4. The machine code for ,T% is DC. %f +%- is set/ the C*U will respond to interrupts. The simulator "enerates a hardware interrupt at re"ular time inter$als that #ou can ad&ust. %f A%A is set/ there should be an interrupt $ector at address G02H. The C*U will &ump to the code that this $ector points to whene$er there is an interrupt.

'op"up Help

S!%
('! #la&s are Set
)ssembler ,UB )5/12 ,UB B5/C5 2achine Code B1 00 12 )1 01 02 (.planation ,ubtract 12 from )5. The answer "oes into )5. ,ubtract C5 from B5. The answer "oes into B5.

'op"up Help

F:R
('! #la&s are Set
)ssembler J96 )5/12 J96 B5/C5 2achine Code BC 00 12 )C 01 02 (.planation 12 J96 )5. The answer "oes into )5. C5 J96 B5. The answer "oes into B5.

J96 can be used to in$ert selected bits. 0000HHHH This is a bit $as*. KOR 0H0H0H0H 66666666 0H0HH0H0 The left four bits are unaltered. The ri"ht four bits are in$erted.

Truth Ta)les and ,o&ic


(ontents

%oolean :perators " #la&s are Set


) mathematician called Bool in$ented a branch of maths for processin" true and false $alues instead of numbers. This is called Boolean )l"ebra. ,imple Boolean al"ebra is consistent with common sense but if #ou need to process decisions in$ol$in" man# $alues that mi"ht be true or false accordin" to comple. rules/ #ou need this branch of mathematics.

The Rules
Rule :ne ,ine 01planation .4 1 )N4 1 "i$es 1. )n# other input "i$es 0. . .4 +N9T )N4- 1 )N4 1 "i$es 0. )n# other input "i$es 1. :R 0 96 0 "i$es 0. )n# other input "i$es 1. .:R +N9T 96- 0 96 0 "i$es 1. )n# other input "i$es 0. F:R (3ual inputs "i$e 0. Non e3ual inputs "i$e 1. .:T %n$ert input bits. 0 becomes 1. 1 becomes 0. Computers wor1 usin" 59'%C. 4ispla#in" "raphics such as the mouse cursor in$ol$es the J96 +(.clusi$e 96- command. )ddition ma1es use of )N4 and J96. These and a few of the other uses of lo"ic are described below.

Truth Ta)les
The one line descriptions of the rules abo$e are clearer if shown in Truth Tables. These tables show the output for all possible input conditions.

,o&ic Gates
5o"ic "ates are the buildin" bloc1s of microcomputers. 2odern processors contain millions of "ates. (ach "ate is built from a few transistors. The "ates are used to store data/ perform arithmetic and manipulate bits usin" the rules abo$e. The J96 rule can be used to test bits for e3ualit#.

.4

Both inputs must be true for the output to be true. )N4 is used for addition and decision ma1in". 66666666666 A 0 O t% t 66666666666 0 0 0 0 H 0 H 0 0 H H H

:R

Both inputs must be false for the output to be false. 96 is used in decision ma1in". Both )N4 and 96 are used for Bit 2as1in". Bit mas1in" is used to pic1 indi$idual bits out of a b#te or to set particular bits in a b#te. 96 is used to set bits to one. )N4 is used to set bits to nou"ht. )N4 is used to test if bits are one. 96 is used to test if bits are nou"ht. 66666666666 A 0 O t% t 66666666666 0 0 0 0 H H H 0 H H H H

F:R

%f the bits in a "raphical ima"e are J96ed with other bits a new ima"e appears. %f the J96in" is repeated the ima"e disappears a"ain. This is how the mouse and te.t cursors "et mo$ed around the screen. J96 is combined with )N4 for use in addition. J96 detects if the inputs are e3ual or not. 66666666666 A 0 O t% t 66666666666 0 0 0 0 H H H 0 H H H 0

. .4

N)N4 is reall# )N4 followed b# N9T. (lectronic circuits are commonl# built from N)N4 "ates +circuits-. Computer pro"rammin" lan"ua"es and this simulator do not pro$ide N)N4. Use N9T )N4 instead. 66666666666 A 0 O t% t 66666666666 0 0 H 0 H H H 0 H H H 0

.:R

N96 is reall# 96 followed b# N9T. (lectronic circuits are commonl# built from N96 "ates +circuits-. Computer pro"rammin" lan"ua"es and this simulator do not pro$ide N96. Use N9T 96 instead. 66666666666 A 0 O t% t 66666666666 0 0 H 0 H 0 H 0 0 H H 0

.:T

N9T is used to in$ert bits or TrueIDalse $alues. )ll the rules abo$e had two inputs and one output. N9T has a sin"le input and output. 66666666666 A O t% t 66666666666 0 H H 0

The Half

dder Truth Ta)le

The half adder does binar# addition on two bits. The )N4 "ate conputes the carr# bit. The J96 "ate computes the sum bit. 0 0 1 1 ! ! ! ! 0 1 0 1 Q Q Q Q 0/ 1/ 1/ 0/ carr# carr# carr# carr# 0 0 0 1

666666666666666666 A 0 -UM CARR1 666666666666666666 0 0 0 0 0 H H 0 H 0 H 0 H H 0 H

!sin& the 0ditor

(ontents

!sin& the 0ditor


(ditin" the source code in the simulator is similar to most word processors and te.t editors such as the @indows Notepad.

!ndo
Kou can undo an editin" error. @hen #ou ha$e an accident and delete or mess up somethin" b# mista1e/ #ou can press Ctrl!F to UN49 the last thin" #ou did. This can be $er# useful.

(ursor Mo5ements
2o$e the te.t cursor. Dor small mo$ements/ use the )rrow ;e#s/ =ome/ (nd/ *a"e Up and *a"e 4own. Kou can use the mouse too. Dor lar"er mo$ements/ hold down the Ctrl 1e# and use the )rrow ;e#s/ =ome/ (nd/ *a"e Up/ and *a"e 4own. Kou can use the mouse too.

4eletin&
4elete pre$ious character with the Bac1space ;e# 4elete ne.t character with the 4elete ;e#

Hi&hli&htin&
To hi"hli"ht a bloc1 of te.t and hold down the ,hift 1e# and use the )rrow ;e#s/ =ome/ (nd/ *a"e Up and *a"e 4own. Kou can dra" the mouse with the left button pressed to do this too. To hi"hli"ht whole words/ lines or documents/ hold down ,hift and Ctrl and then use the )rrow ;e#s/ =ome/ (nd/ *a"e Up and *a"e 4own. )lternati$el# dra" the mouse with the left button pressed. 7e/ 01planation

(trl+( Cop# a hi"hli"hted bloc1 (trl+F Cut a hi"hli"hted bloc1 (trl+V *aste te.t copied or cut earlier 4elete 4elete a hi"hli"hted bloc1 (trl+S ,a$e a file lt+# a ,a$e a file with a new name (trl+: 9pen a file lt+# 1 Vuit

Virtual 'eripherals
(ontents

!sin& the 'eripheral 4e5ices


7e/)oard 'ort 09 I.T 03

How to !se This is one of the more comple. de$ices. To ma1e the 1e#board $isible/ use :!T 09. ($er# time a 1e# is pressed/ a hardware interrupt/ I.T 03 is "enerated. B# default/ the C*U will i"nore this interrupt. To process the interrupt/ at the start of the pro"ram/ use the STI command to set the interrupt fla" +%- in the C*U status re"ister +,6-. *lace an interrupt $ector at 6)2 address 03. This should point to #our interrupt handler code. The interrupt handler should use I. 09 to read the 1e# press into the , re"ister. 9nce STI has set the +%- fla" in the status re"ister +,6-/ interrupts from the hardware timer will also be "enerated. These must be processed too. The hardware timer "enerates I.T 02. To process this interrupt/ place an interrupt $ector at 6)2 location 02. This should point to the timer interrupt handler code. The timer code can be as simple as IR0T. This will cause an interrupt return without doin" an# other processin". #$% !b !b start H0 (0 ; 7ar!"are Ti$er 2nterr %t Ve&tor ; ,e/boar! 2nterr %t Ve&tor

; ===== 7ar!"are Ti$er ======= org H0 no% ; 'o so$ething no% no% no% no% iret ; ============================

se3 l here

; ===== ,e/boar! 7an!ler ===== org (0 CL2 ; Prevent re6entrant % sh al % sh3 in no% 0=

se

; Pro&ess the *e/ %ress here

no% no% no% no% %o%3 %o% al -T2 iret ; ============================ ; ===== 2!le Loo% ============ start: -T2 ; -et D2E 3lag o t 0= ; Ma*e *e/boar! visible i!le: no% ; 'o so$ething se3 l here no% no% no% no% #$% i!le ; ============================ en! ; ============================

Visual 4ispla/ !nit Memor/ Mapped

How to !se

The ?isual 4ispla# Unit +?4U- is memor# mapped. This means that 6)2 locations correspond to positions on the screen. 6)2 location C0 maps to the top left corner of the ?4U. The screen has 1 columns and four rows mapped to 6)2 locations C0 to DD. @hen #ou write ),C%% codes to these 6)2 locations/ the correspontin" te.t characters appear and the ?4U is made $isible. This de$ice/ when combined with a 1e#board/ is sometimes called a dumb terminal. %t has no "raphics capabilities. =ere is a code snippet to write te.t to the screen. ; ===== Me$or/ Ma%%e! V'U ================================= MOV AL,?H ; A-C22 &o!e o3 AAA MOV ;C0<,AL ; RAM lo&ation $a%%e! to the ; to% le3t &orner o3 the V'U MOV AL,?( ; A-C22 &o!e o3 A0A MOV ;CH<,AL ; RAM lo&ation $a%%e! to the V'U MOV AL,?9 ; A-C22 &o!e o3 ACA MOV ;C(<,AL ; RAM lo&ation $a%%e! to the V'U EN' ; =========================================================

Traffic ,i&hts 'ort 0-

How to !se

The traffic li"hts are connected to *ort 01. %f a b#te of data is sent to this port/ where$er there is a one/ the correspondin" traffic li"ht comes on. %n the ima"e on the left/ the binar# data is 01010101. %f #ou loo1 closel# #ou can see that the li"hts that are on/ correspond to the ones in the data b#te. 01010101 is 88 he.adecimal. =e.A numbers are e.plained here. =ere is a code snippet to control the li"hts. ; ======================================================== ; ===== NNTlight.as$ ===================================== ; ===== Tra33i& Lighte on Port 0H ======================== -tart: MOV AL,JJ ; 0H0H0H0H OUT 0H ; -en! the !ata in AL to Port 0H ; Dthe tra33i& lightsE MOV OUT )MP AL,AA 0H -tart ; H0H0H0H0 ; -en! the !ata in AL to Port 0H ; Dthe tra33i& lightsE

EN' ; ========================================================

Se5en Se&ment 4ispla/s 'ort 02

How to !se

The se$en se"ments displa#s are connected to *ort 02. %f a b#te of data is sent to this port/ where$er there is a one/ the correspondin" se"ment comes on. The ri"htmost bit controls which of the two "roups of se"ments is acti$e. This is a simple e.ample of mulitple.in". %f the least si"nificant bit +5,B- is >ero/ the left se"ments will be acti$e. %f the least si"nificant bit +5,B- is one/ the ri"ht se"ments will be acti$e. =ere is a code snippet. ; ====================================================== ; ===== NNsevseg.as$ =================================== ; ===== -even -eg$ent 'is%la/s Port 0( ================= -tart: MOV AL,FA ; HHHH H0H0 OUT 0( ; -en! the !ata in AL to Port 0( MOV OUT MOV OUT MOV OUT )MP EN' AL,0 0( AL,F0 0( AL,H 0( -tart ; 0000 0000 ; -en! the !ata in AL to Port 0( ; HHHH H0HH ; -en! the !ata in AL to Port 0( ; 0000 000H ; -en! the !ata in AL to Port 0(

; ======================================================

Heater and Thermostat 'ort 03

How to !se

The heater and thermostat s#stem is connected to *ort 03. ,end 00 to port 3 to turn the heater off. ,end 70 to port 03 to turn the heater on. %nput from port 03 to test the thermostat state. The code snippet below is an incomplete solution to control the heater to 1eep the temperature stead# at about 21 C. Kou can clic1 the thermometer to set the temperature. This can sa$e time when #ou are testin" the s#stem. ; ===== 7eater an! Ther$ostat on Port 09 ================= ; ===== NN7eater.as$ ===================================== ; ===== 7eater an! Ther$ostat on Port 09 ================= MOV AL,0 ; Co!e to t rn the heater o33 OUT 09 ; -en! &o!e to the heater 2N AN' )> 7ALT Col!: MOV OUT AL,M0 09 ; Co!e to t rn the heater on ; -en! &o!e to the heater 09 AL,H Col! ; ; ; ; ; 2n% t 3ro$ Port 09 Mas* o33 le3t seven bits 23 the res lt is 4ero, t rn the heater on : it

EN' ; ==========================================================

Sna*e and Ma[e 'ort 06

How to !se

The left four bits control the direction of the sna1e. 70 00 20 10 Up 4own 5eft 6i"ht

The ri"ht four bits control the distance mo$ed. Dor e.ample/ 0D means 4own 18. 0 means down. D means 18. This pro"ram is rather wasteful of 6)2. %f #ou want to tra$erse the entire ma>e and "o bac1 to the strart/ #ou will run out of 6)2. ) "ood learnin" tas1 is to use a data table. This reduces the si>e of the pro"ram "reatl#. )lso/ it is "ood st#le to separate code and data. =ere is a code sample : not usin" a data table. ; =========================================================== =====

; ===== NNsna*e.as$ ====================================== ; ===== -na*e an! Ma4e =================================== -tart: MOV OUT MOV OUT OUT OUT OUT )MP AL,FF 0? AL,?F 0? 0? 0? 0? -tart ; -%e&ial &o!e to reset the sna*e. ; -en! AL to %ort 0? to &ontrol the ; sna*e. ; ; ; ; ; ? $eans -en! ?F -en! ?F -en! ?F -en! ?F 'O+N. to the to the to the to the F $eans HJ. sna*e sna*e sna*e sna*e

EN' ; ========================================================

Stepper Motor 'ort 05

How to !se

=ere is a stepper motor. Normal motors run continuousl# and it is hard to control their mo$ement. ,tepper motors step throu"h a precise an"le when electroma"nets are ener"ised. ,tepper motors are used for precise positional control in printers/ plotters/ robotic de$ices/ dis1 dri$es and for an# application where precise positional accurac# is re3uired. The motor is controlled b# ener"isin" the four ma"nets in turn. %t is possible to ma1e the motor mo$e in half steps b# ener"isin" sin"le and pairs of ma"nets. %f the ma"nets are ener"ised in the wron" se3uence/ the motor complains b# a bleep from the computer spea1er. =ere is a code snippet to control the motor. Note that it would be better codin" st#le to use a data table. ; ================================ ; ===== NN-te%.as$ =============== ; ===== -te%%er Motor ============ $ov al,H o t 0J $ov al,( o t 0J $ov al,? o t 0J $ov al,M o t 0J $ov al,N o t 0J $ov al,H o t 0J $ov al,9 o t 0J $ov al,( o t 0J $ov al,L o t 0J $ov al,? o t 0J $ov al,& o t 0J $ov al,M o t 0J $ov al,N o t 0J $ov al,H o t 0J en! ; ================================

,ift/0le5ator 'ort 08

How to !se

Input Si&nals Bits 7 and C are unused. Bit is wired to the top call button. Bit 8 is wired to the bottom call button. %f these buttons are clic1ed with the mouse/ the correspondin" bits come on. Bit 0 senses the lift and "oes hi"h when the lift ca"e reaches the bottom of the shaft. Bit 3 senses the lift and "oes hi"h when the lift ca"e reaches the top of the shaft. :utputs Bit 2 turns on the lift motor and the ca"e "oes down. Bit 1 turns on the lift motor and the ca"e "oes up.

=a/s To 4estro/ the ,ift 1. Turn on bits 1 and 2 at the same time. This causes the motor to "o up and down simulatneousl#O 2. Crash the lift into the bottom of the shaft. 3. Crash the lift into the top of the shaft. 0. 6un the simulation too slowl#. ($en if the code is lo"icall# correct/ the lift crashes into the end of the shaft before the pro"ram has time to switch off the motor.

Hardware Timer I.T 02

How to !se The hardware timer "enerates I.T 02 at re"ular time inter$als. The time inter$al can be chan"ed usin" the Confi"uration tab as shown in the ima"e. The C*U will i"nore I.T 02 unless the +%fla" in the status re"ister +,6- is set. Use STI to set the +%- fla". Use (,I to clear the +%- fla". The code sample below processes I.T 02 but does nothin" useful. %f the C*U cloc1 is too slow/ a new I.T 02 can occur before the pre$ious one has been handled. This is not necessaril# a problem as lon" as the C*U e$entuall# catches up. To allow this to wor1/ it is essential that the interrupt handler sa$es and restores an# re"isters it uses. Use '!SH and '!S# to sa$e re"isters. Use ':'# and ':' to restore re"isters. 6emember to pop items in the re$erse order that the# were pushed. Code li1e this is Bre:entrantB. %f the C*U is too slow and does not catch up/ the stac1 will "raduall# "row and eat up all the a$ailable 6)2. ($entuall# the stac1 will o$erwrite the pro"ram causin" a crash. %t is a useful learnin" e.ercise to slow the C*U cloc1 and watch this happen.

#$% start !b H0 2nterr %t Ve&tor

; 7ar!"are Ti$er

; ===== 7ar!"are Ti$er ======= org H0 no% ; 'o so$ething se3 l here no% no% no% no% iret ; ============================ ; ===== 2!le Loo% ============ start: -T2 ; -et D2E 3lag i!le: no% ; 'o so$ething se3 l here no% no% no% no% #$% i!le ; ============================ en! ; ============================

.umeric 7e/pad 'ort 0; I.T 06

How to !se

This is one of the more comple. de$ices. To ma1e the numeric 1e#pad $isible/ use :!T 0;. ($er# time a 1e# is pressed/ a hardware interrupt/ I.T 06 is "enerated. B# default/ the C*U will i"nore this interrupt. To process the interrupt/ at the start of the pro"ram/ use the STI command to set the interrupt fla" +%- in the C*U status re"ister +,6-. *lace an interrupt $ector at 6)2 address 06. This should point to #our interrupt handler code. The interrupt handler should use I. 0; to read the 1e# press into the , re"ister. 9nce STI has set the +%- fla" in the status re"ister +,6-/ interrupts from the hardware timer will also be "enerated. These must be processed too. The hardware timer "enerates I.T 02. To process this interrupt/ place an interrupt $ector at 6)2 location 02. This should point to the timer interrupt handler code. The timer code can be as simple as IR0T. This will cause an interrupt return without doin" an# other processin". #$% !b !b !b start H0 00 (0 ; 7ar!"are Ti$er 2nterr %t Ve&tor ; ,e/boar! 2nterr %t Ve&tor D n se!E ; N $eri& ,e/%a! 2nterr %t Ve&tor

; ===== 7ar!"are Ti$er ======= org H0 no% ; 'o so$ething no% no% no%

se3 l here

no% iret ; ============================ ; ===== ,e/boar! 7an!ler ===== org (0 CL2 ; Prevent re6entrant % sh al % sh3 in no% no% no% no% no% 0M

se

; Pro&ess the *e/ %ress here

%o%3 %o% al -T2 iret ; ============================ ; ===== 2!le Loo% ============ start: -T2 ; -et D2E 3lag o t 0M ; Ma*e *e/%a! visible i!le: no% ; 'o so$ething se3 l here no% no% no% no% #$% i!le ; ============================ en! ; ============================

You might also like