IF_ICMPEQ (0x9F)


The IF_ICMPEQ instruction is a conditional branch. The branch is successful if the two top values on the stack are equal. This instruction pops the top two elements from the stack, subtracts them, and checks the Z flag. If the flag is set (Z=1 means the difference is 0) then the branch is successful; otherwise the branch fails.

The operand is a two-byte signed offset. If the branch is successful, this offset is added to the byte address at which the IF_CMPEQ opcode was found and the sum is placed into the PC as the address of the next opcode in the instruction stream. Otherwise, the PC is incremented by two in order to skip past the two-byte operand.

When writing assembly language code, the operand must be a program label within the same method. The assembler calculates the proper offset value.

Interpreter Microcode

The microinstructions are listed in the order in which they are executed; not the order in which they are stored in the control store.

0x09f  SP=MAR=SP-1; rd; goto 0x3b
   
0x03b  SP=MAR=SP-1; goto 0x3c
0x03c  H=MDR; rd; goto 0x3d
0x03d  OPC=TOS; goto 0x3e
0x03e  TOS=MDR; goto 0x3f
0x03f  ALU=OPC-H; if Z goto 0x101 else goto 0x1

then (Z = 1)

0x101  OPC=PC-1; fetch; goto 0x2f
...
0x02f  PC=PC+1; fetch; goto 0x30
0x030  H=MBR<<8; goto 0x31
0x031  H=H OR MBRU; goto 0x32
0x032  PC=H+OPC; fetch; goto 0x33
0x033  ALU=0; goto 0x2
...

else (Z = 0)

0x001  PC=PC+1; goto 0x40
...
0x040  PC=PC+1; fetch; goto 0x41
0x041  ALU=0; goto 0x2

If you compare this microcode with the code that interprets the IFEQ and the IFLT instructions, you will notice that the 'then" code and the "else" code are exactly the same in all three.

Example Program

//---------------------------------------------
// Demonstrate the IF_ICMPEQ instruction.
//
// 1. Clear Memory
// 2. Assemble this program.
// 3. Reset the computer.
// 4. Click the "Display Words" radio button
//    below the memory display.
// 5. Click the "Run" button.
//
// This program will display the letter 'T' in
// the input/output window indicating that the
// branch succeeded.
//
// If you bipush any value other than 10 in the
// first statement, the letter 'F' will be
// displayed indicating that the branch failed.
//---------------------------------------------
.main
    bipush 10
    bipush 10
    if_icmpeq a
    bipush 'F'
    goto b
a:  bipush 'T'
b:  out
    halt
.end-main