SWAP (0x5F)


The SWAP instruction swaps the top two elements on the stack.

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.

0x05f  MAR=SP-1; rd; goto 0xe
...
0x00e  MAR=SP; goto 0xf
0x00f  H=MDR; wr; goto 0x11
...
0x011  MDR=TOS; goto 0x12
0x012  MAR=SP-1; wr; goto 0x14
...
0x014  TOS=H; goto 0x2

Example Program

//---------------------------------------------
// Demonstrate the SWAP 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 pushes a 1 and 2 onto the stack
// and then swaps them. When the program
// terminates, the 1 will be on the top of the
// stack, at 4098, and the 2 will be at 4097.
//---------------------------------------------
.main
    bipush 1
    bipush 2
    swap
    halt
.end-main