5. Memory, Registers and Flags

In addition to the instructions, JTT obviously needs memory to place the data and instructions in. It also needs flags to check for certain conditions (commonly called 'if-statements') and registers in which to store data for manipulation, so that the resulting calculation can be put back in memory. All of these work (necessarily) in conjunction with instructions, and all will be used by the JTT programmer sooner or later.

5.1 Memory

Although memory has been touched on in preceding Chapters, there is more to say:

  • Memory is a sequence of slots in which data is stored. The slots are called addresses, and each address has its own unique number.
  • There are two memory types: instruction memory, which contains the list of instructions that make up a JTT program, and data memory, where the data used by the JTT program to be manipulated and calculated is stored.
  • JTT has a default number of 4,096 memory addresses, numbered (decimal) 0 to (decimal) 4095, or $0 to $1111 1111 1111, or 0x0 to 0xfff. Thus it can store a maximum of 4,096 instructions and 4,096 pieces of data at any time.
  • Any part of memory can be used in conjunction with any other. That is, an instruction anywhere in JTT�s instruction memory can access data anywhere in JTT's data memory, as long as both exist at the same time whilst the program runs.
  • Memory is accessed by the Fetch from Memory and Store to Memory instructions. A value can directly be stored in memory via Load Constant.
  • 5.2 Registers

    There are eight 16-bit registers, to be used and accessed directly by the programmer. Data cannot be manipulated directly in memory, as memory is simply just that - a place where data is remembered. So, for example, to add two data together, the two values should be fetched from memory and stored in two separate registers. Then, an instruction is carried out on these two registers, and the result placed back into a memory address. There are some other rules:

  • Registers can also be used to store data temporarily - the less data is read from or saved to memory, the fewer instructions are likely to be needed and so the faster (and more efficient) the program is.
  • The registers are addressed within a JTT program as 'r' followed by the register's number. There are eight of them, so the registers are called 'r0' through to 'r7'.
  • There are two special flags, r6 and r7, which have their own special rules and should not be used routinely to hold data. See Section 5.3 and Section 5.4 for more.
  • 5.3 The Instruction Pointer (r6)

    Register 6, the Instruction Pointer (IP), is updated by JTT automatically. It points to the instruction in instruction memory that is next to be carried out. Since all programs are carried out in sequence, the IP is incremented by one when the last instruction is dealt with. The IP can be changed freely by the user within a JTT program. This means that it is possible to 'jump' to another instruction in instruction memory, and carry out loops that keep running until a particular circumstance is reached.

  • To jump to one instruction from another, address the IP as 'r6' using the Load Constant instruction, as normal.
  • To carry out loops, use the q(uery) flag and jump forward to new code if the circumstance is reached, or backward to repeat the loop if not.
  • Details about both of these operations are given in Chapter 4.3 and 4.4. The query flag is dealt with in Section 5.4 (next).

    Please Note
    The IP is also commonly referred to as the "Program Counter" (PC) in standard low-level computer terminology.

    5.4 Flags and the Status Word (r7)

    There are eight information bits and eight flags. Each is held in the 16 bits of r7 - the Status Word (SW).

    The leftmost 8 bits of the SW contain information that the user cannot access or change. This includes the current version code of JTT and the existence (or not) of additional utilities, such as the GUI. It is not necessary to know much about these - just be aware of their existence - and so they will be ignored from now on.

    The rightmost 8 bits of the SW are flags. A flag is a single bit that represents a condition. If it set to 1, its condition was satisfied as a result of the last JTT instruction. If set to 0, it was not. Flags can thus be used (and indirectly be set) from within your JTT program to form what are known as 'conditional statements'. Sometimes, you will not know what the result of an operation will be, and will want the program to act differently, depending on a number of possibilities. If the result of adding two numbers is zero, you may want it to do one thing. If not, you may want it to do something else.

    The SW should be considered, and strictly is, read only. This means that you cannot place a value directly in the SW by using, for example, Load Constant. Instead, it is updated automatically as a result of the previous instruction that was carried out whilst running a JTT program, and the user can influence the status of any of the flags it holds. There is one exception to this rule - the q (query) flag.

    Please Note
    As with decimal, binary and hex values, the least significant figure is the rightmost figure, and the most significant figure the leftmost. As such, bit 0 is always the one on the right and bit 15 on the left, and should be read backwards from right to left.

    Flags may be addressed by their number or name, and are detailed below. Afterwards, an explanation of each, and a description of their uses in JTT programs, are be given.

    Bit No. 7 6 5 4 3 2 1 0
    Name e m l v c n z q

    Bit 0 the "Query Flag" [ q ], and the Comparison Instruction Extension

    'Query' is possibly the most important flag of them all. It is the only flag that can be set directly by the programmer within JTT code, and the main way of using it is by using the BitSelect instruction. When a value is thrown at r7, only bit 0, the Query Flag, is affected. The rest of the flags are protected and tamperproof, and updated automatically as the program commences. As a result, BitSelect can be used to place the contents of one flag into the Query Flag for comparison.

    From here, the comparison instruction extension (CE) can be used to determine the course of action. The CE is a question mark that is placed before an instruction. The instruction will then only be carried out if the Query Flag is set to 1. It will be ignored, and the IP will move on to the next line, if it is set to 0. For example, the JTT instruction 'r6=0xf' will unconditionally jump to the instruction at memory address (decimal) 16. However, '? r6=0xf' will go there only if the Query Flag is set to 1.

    Let us take a simple but typical example.

    A program that adds two numbers together needs to jump to instruction memory address 0xf3 if the two numbers equal 50, or to 0xf4 if not. The syntax of the program will follow this pattern:

       1. Add the two numbers together.
       2. Minus 50 from the result made in Step 1.
       3. The Zero Flag (see below) will be set to 1 if the subtraction in Step 
          Two resulted in zero, or 0 if otherwise.
       4. Place the contents of the Zero Flag into the Query Flag using BitSelect.
       5. Using the '?' (CE) extension, tell the IP to jump to 0xf3 if the Query 
          Flag is set, or 0xf4 if it is not.

    Say value A is 10 and value B is 40. Taking into account the above steps:

       1. 10+40=50
       2. 50-50=0
       3. Zero Flag is set to 1
       4. Query Flag is set to 1
       5. CE jumps to 0xf3 because Query Flag is set to 1

    Now let's say A is 5 and B is 7. Taking into account the above steps:

       1. 5+7=12
       2. 50-12=38
       3. Zero Flag is set to 0
       4. Query Flag is set to 0
       5. CE jumps to 0xf4 because Query Flag is set to 0

    This, incidentally, is not the full limit of the usage of the CE. It does not have to jump to a different instruction memory address under certain conditions. The CE can be used to place a value into a register, perform a memory operation or in conjunction with any of the eight JTT instructions.

    The subsequent flags work off the same principle, and may be used and manipulated in much the same way.

    Bit 1, the "Zero" Flag [ z ]

    Zero is always set to 1 when the result of the last instruction was zero, or 0 if otherwise.

    Bit 2, the Negative Flag [ n ]

    Negative is always set to 1 if the last instruction determined a result less than 0, or 0 if otherwise.

    Bit 3, the "Carry" Flag [ c ]

    The Carry flag serves as an 'overflow' flag for non-2s-comp values. If the result of the last calculation was too great to fit into a 16-bit register, then this bit is set to 1 and the 16 least significant bits are stored in the register. Otherwise, it is set to 0. This way, it serves as a "17th bit". Note that using the C flag in this way is hazardous, since the result may have been more than 17 bits long.

    Bit 4, the "Overflow" Flag [ v ]

    The Overflow flag serves the same purpose as the Carry flag, except that it works in conjunction with 32-bit numbers in twos-complement.

    Bit 5, the "Carry or Zero" Flag [ l ]

    The l-flag may be considered the "Carry or Zero" flag. It is set if either the 'c' or the 'z' flag (or both) are set. This means that if the last command resulted in a calculation that was out of bounds or absolute zero, it is set to 1, or otherwise it is set to 0.

    Bit 6, the "Negative and Zero" Flag [ m ]

    The m-flag can be considered the "Negative and Zero" flag. It is used in twos-complement only, and is set to 1 if the last instruction resulted in zero, or to 0 if otherwise (i.e. even if the result was 0).

    Bit 7, the "Any Zero" Flag [ e ]

    The e-flag may be considered the "(Negative and Zero) or Zero" flag. Again used in twos-complement, it is set to 1 if either the M-flag is set or if the result was zero. In other words, it becomes 1 if the last instruction was negative-zero or positive-zero, or 0 if otherwise.

    Please Note
    With all flags, care should be taken to make sure whether the flag is set in 2s-comp or in normal 16-bit operations.
    The "Any Zero" flag can be used in either mode, although its real benefit is in 2s-comp. Otherwise, it is identical to the Zero flag.
    It is always good practice to make full use of every flag available, as long as the usual rules and protocols of use are acknowledged. However, you may wish to construct programs that emulate flags l, m and e via the other flags to get to grips with how they work.
    Out of interest, you may be wondering why, given that JTT attempts to emulate a simple RISC instruction set, flags v, l, m and e exist, when just q, z, n and c should do. The reason is that JTT's architecture happens to support "physically" additional flags, and efficiency demands as little waste as possible.

    Go to next chapter Go to top of page Go to Contents Page Go to HomePage E-mail the author
    Hosted by www.Geocities.ws

    1