next up previous
Next: 7 Student Response to Up: Using J as an Previous: 5 Laboratory Hardware


6 Examples of J in Exposition

A small subset of J suffices when used as an expository notation in a course like this. In this section a few examples of the expository use of J are given. A few helping definitions are needed. Except for a few primitives such as addition, subtraction and catenation, English words are used in place of the primitive J spellings to improve readability. Usually, the English word used is taken from the J Dictionary [Ive 95].

        from =. {
        amend =. }
        take =. {.
        drop =. }.
        rep =. #:
        base =. #.
        time =. 6 !: 2
        display =. 1 !: 2 & 2
        format =. ":

The following words are defined for the primitive circuit gates.

        or =. +.
        and =. *.
        not =. -.

The circuit elements and, or and not can be modeled as:

        bitOr =. 3 : 0
        ('a' ; 'b') =. y.
        a or b
        )

        bitAnd =. 3 : 0
        ('a' ; 'b') =. y.
        a and b
        )

        bitNot =. not

A half-adder can be modeled using these elements as:

        halfAdder =. 3 : 0
        ('a' ; 'b') =. y.
        bitOr (bitAnd a , bitNot b) , bitAnd (bitNot a) , b
        )

Next we can build a 1 bit adder using two half-adders as:

        bitAdder =. 3 : 0
        ('a' ; 'b' ; 'cin') =. y.
        t =. bitHalfAdder a , b
        g =. bitAnd a , b
        p =. bitAnd t , cin
        (bitOr g , p) , bitHalfAdder t , cin
        )

If a language feature is not in the student's current vocabulary when a particular model is described, then that feature would be introduced as a part of the explanation of the model. The idea is that certain language features are introduced when there is an expository need for them so that the focus is mainly on the topic being described rather than the programming notation. Also, notice that explicit definition, except in the simplest cases, rather than tacit definition is used because it is felt that students find it easier to understand explicit references to function arguments. The arguments passed to many of these functions are assigned, indirectly, to local names in an attempt to improve readability. Finally, the above models are monadic since bitAdder requires three arguments; the two summands and a carry input.

Next we can model a wire for connecting circuit elements as:

        wireOutput =. 3 : 0
        ('pin' ; 'outputs') =. y.
        pin from outputs
        )

We can use two bit-adders and some wire to build a 2 bit adder as:

 
        twoBitAdder =. 3 : 0
        ('a1' ; 'a0' ; 'b1' ; 'b0') =. y.
        t0 =. bitAdder a0 , b0 , 0
        t1 =. bitAdder a1 , b1 , wireOutput 0 ; t0
        (wireOutput 0 1 ; t1) , wireOutput 1 ; t0
        )

It needs to be emphasized that while students find such descriptions readable, they may also be used to provide an interactive working model which can be explored during a lecture using the classroom workstation and display system. These descriptions also constitute part of the prepared laboratory software for simple experiments involving computer circuit elements. In one laboratory experiment, students are asked to design and build a model of a 4-bit adder. The lab suggests that they verify experimentally that this adder performs 2's complement arithmetic and asks them to suggest a modification of their design which would allow the adder to subtract as well as add. The above models for adder circuits can be installed in a model of a simple CPU which is used in the lectures describing the organization of a simple computer.

In the lectures on software design and engineering principles, when discussing techniques of modularization and layering of software, the routines for rational arithmetic found in Structure and Interpretation of Computer Programs [Abel 85] have been used. One can demonstrate that it is possible to change the implementation of rational numbers without changing any code in layers above the implementation layer. The benefits of abstraction barriers is explored experimentally in a lab which asks students to form hypotheses concerning three different rational number implementations (don't remove common factors from numerator and denominator, remove common factors at construction and remove common factors at access), gather data, analyze results and draw conclusions. This experimentation involves comparisons based on speed or space used.

In the lectures on computer organization, a very simple one accumulator computer, having four instructions (load, store, add and subtract) is described. A J working model of this computer is given which allows students to not only see, in precise terms, the organization of this machine, but also run a few simple programs on this machine.

The memory of this machine is modeled as a J vector. For example,

        mem =: 0 0 0 0 307 108 409 22 3 0

models a memory containing a machine language program, starting in location 4, for the following J expression.

        c =: a + b

A memory system has two operations, access and store. These are modeled in J as:

        access =. 3 : 0
        y. from mem
        )

        store =. 3 : 0
        mem =: (1 from y.) (0 from y.) amend mem
        )

A processor is modeled by a J function named proc. The processor contains three registers which are modeled by global variables ac, the accumulator, pc, the program counter and ir, the instruction register. The processor is modeled in J as:

     proc =. 3 : 0
     whilst. y. do.
          NB. show registers and memory if tracing
          displayRegisters ''
          NB. fetch instruction
          ir =: access pc
          NB. increment pc
          pc =: pc + 1
          NB. decode instruction
          ('opCode' ; 'address') =. 100 100 rep ir
          NB. interpret instruction
          if. opCode = 1
               do. ac =: ac + access address continue. end.
          if. opCode = 2
               do. ac =: ac - access address continue. end.
          if. opCode = 3
               do. ac =: access address continue. end.
          if. opCode = 4
               do. store address , ac continue.
               else. 'invalid operation code' break. end.
     end.
     )

The processor is started with an argument of zero to execute a single instruction at a time, while running the processor with an argument of 1 causes continuous operation of the machine. A global variable, trace controls whether or not the registers and memory are displayed before each machine cycle as shown in the displayRegisters function.

     displayRegisters =. 3 : 0
     if. trace
          do.
               display 'pc= ' , (format pc) , ', ir= ' , (format ir) , ', ac= ' , format ac
               display 'mem= ' , format mem
          else.   0
          end.
     )

Here, again, the point of such descriptions is that they are readable by humans, yet each such description is also a working model which can be explored in the laboratory.


next up previous
Next: 7 Student Response to Up: Using J as an Previous: 5 Laboratory Hardware
2002-09-30