[comp.sys.mac.programmer] shutting off the instruction cache on 68020

bier@speedy.cs.wisc.edu (George Bier) (03/29/88)

I recently posted a request asking how to shut off the instruction
cache on MAC II and I have received numerous fairly insulting replies
that under no conditions should I do this (admittedly some replies
were more cordial than others).  In an attempt to stop the barrage,
and as a reply to those people who feel the need to insult, let me
tell you about the application.

I teach the machine language, assembly language class at the University
of Wisconsin.  We are thinking about moving the course from a VAX to
our Macintosh lab.  The way we teach the class here, we start them off
writing machine language programs of the form:

00000010: c0 9f 00000150 9f 000000100  ;addl2  *#200,*#100
0000001b: d1 9f 00000104 9f 00000010c  ;cmpl	*#i,*#10c
00000026: 13 1c 	              ;beql to rsb instruction at 44
00000028: c0 9f 00000108 9f 00000104 ;increment i by 1
00000033: c0 9f 00000110 9f 00000012 ; modify the add at the top of the loop

00000100: 0000000	;answer
00000104: 0000001	;variable i
00000108: 0000001 	;constant 1
0000010c: 000000a	;10 in hexadecimal

The above is an excerpt (the above is not the complete program)  from a 
machine language program written for the VAX which will add together ten 
consecutive values that the students will place starting at address 00000150.

The above is the first assignment for the course, they type in the program
given to them and learn how to use a custom machine language loader
that takes there input file and loads it in at the correct addresses.

The way we teach the class here is to keep as much hidden from them as 
possible and slowly build up to the advanced material.  The above uses
only absolute addressing (9f) which is the easiest mode to understand.  Since
they only know absolute addressing, indexing through the array is
impossible leading to a self-modifying code solution. (the instruction
at 33 moves them to the next value in the array).

We find teaching the class like this extremely effective.  Forcing students
to start with machine language with only one addressing mode leads to
a good understanding of why and how the assembler exists and motivates
the need for additional addressing modes.  In addition writing the machine
language code is as close as we can come to "toggling it in" without
having to provide a machine for students to "toggle" on.

I am in the process of starting to develop the macros we provide the students
for the class and then I will be starting on the machine language loader. 
Although the machines in the lab are Mac plusses, my development machine is a
MAC II.  Hence the need to turn off the instruction cache.   

In reply to those of you who felt the need to point out that their is an
instruction in the 68020 that turns off the cache and therefore
my question fell into the range of "stupid question look it up yourself",
let me point out that it is not necessarily obvious that this was the 
correct way to turn off the cache.  There was some chance that their
was a routine in the toolbox or MAC-OS to do it that was better.

My apologies for the length of this article and for the fact that it
is inappropriate for this news group but I am trying to reduce the number
of people telling me what "hot shit" programmers they are and what a dip
I must be to have to resort to self-modifying code.  Sometimes the obvious
is not always so obvious

--george bier

gnu@hoptoad.uucp (John Gilmore) (03/29/88)

bier@speedy.cs.wisc.edu (George Bier) wrote:
> I teach the machine language, assembly language class at the University
> of Wisconsin.
> The way we teach the class here is to keep as much hidden from them as 
> possible and slowly build up to the advanced material.  The above uses
> only absolute addressing (9f) which is the easiest mode to understand.  Since
> they only know absolute addressing, indexing through the array is
> impossible leading to a self-modifying code solution.

Many thanks for the explanation, Mr. Bier, however, as one of the "hot
shit" programmers who wrote to you, I now feel even more strongly about it.
For an ordinary application to be using self modifying code is one thing,
but you are TEACHING this to unsuspecting students!

Why not start with a program to add add up 10 numbers at fixed
addresses using 10 add instructions?  No big deal.  You can go from
there to "add 10 consecutive numbers at any location in memory" by
using base+offset addressing, and from there to "add any number of
numbers at any location in memory" by introducing looping.  At no
point is it necessary to introduce the idea of self-modifying code,
to convey the basic concepts of machine language programming simply
and one-at-a-time.

You might even take 10 minutes to explain that in most computers, you
are protected from writing on your programs because 999 times out of a
thousand, that was not what you wanted to do; you had a store go wild,
and would be happier if it failed there rather than when it tries to
execute the data later.  Not to mention its making the system more
efficient in sharing program text, and making fast hardware easier to
build (which is why you have to turn off the 68020 cache to get
reliable results when you store in the instruction stream).  There are
many positive sides to the inability to write over instructions, and
I'd much rather that you teach those to people, rather than teaching
them how to circumvent the protection so they can use poor techniques.
-- 
{pyramid,pacbell,amdahl,sun,ihnp4}!hoptoad!gnu			  gnu@toad.com
		"Watch me change my world..." -- Liquid Theatre