S2 v2  modification to S2

For teaching purpose, using flags for jump conditional is confusing.  The following example shows:

if n < 10 then A else B

;; let r1 = n
   sub r0 r1 #10
   jmp GE else
:then 
   A
   jmp always exit
:else
   B
:exit

The difficulty is as follows:
   sub r0 r1 #10

What r0 does?   Why subtract n and 10?  How the condition (flags) is affected?  What are flags?
Where they are?

These questions can be explained as follows.
r0 is a throwaway value of "comparing" r1 and 10.  The comparison is achieved by the instruction "sub".  During the comparison, flags are affected as "sub" is an ALU operation which affects flags.  Flags are "global" variables inside a processor.  They are used exclusively for jump conditional.

Instead of using flags, conditional jump can be achieved by "comparison" instruction which stores its result in a register, then jump tests the value of register for conditional.

Let "lt r1 r2 r3" be an instruction, r1 = r2 < r3
Let "jmp_if_false r1 ads" be if r1 is false jump

    lt r1 r2 r3
    jmp_if_false r1 else

This is much clearer.  r1 is used to store the result of comparison.  Conditional is simplified to two jump instuctions: jump_if_true, jump_if_false.
The comparison instructions are: eq, ne, lt, le, gt, ge (six in all).  Immediate mode is obvious, the second argument can be a constant.  "Jmp always" can be synthesised from jmp_if_false r0.

These instructions can be added without changing the old instructions.  The instruction encoding is as follows:

Instruction format
L-format    op:5 rd1:5 ads:22
D-format    op:5 rd1:5 rs2:5 disp:17
X-format    op:5 rd1:5 rs2:5 rs3:5 xop:12
(rd dest, rs source, ads and disp sign extended)

14..30 undef.  can be used for encoding the immediate and jump instructions:

   op  mode format
14 eq  i    D
15 ne  i    D
16 lt  i    D
17 le  i    D
18 gt  i    D
19 ge  i    D
20 jt  a    L 
21 jf  a    L
22..30 undef.

xop 13..4095 undef. can be used for encoding the register-register instructions:

13 reserved
14 eq  r   X
15 ne  r   X
16 lt  r   X
17 le  r   X
18 gt  r   X
19 ge  r   X
20..4095  undef

All jumps should be relative to PC instead of being absolute address to simplify the relocation.

Carry and Overflow flags are still important but in elementary teaching they play no role.  The instructions can be: jump_if_carry, jump_if_no_carry, jump_if_overflow, jump_if_no_overflow (jc, jnc, jo, jno).    

22 jc
23 jnc
24 jo
25 jno
26..30 undef (but the opcode is running out)

27 Aug 2003
P. Chongstitvatana
