Department of Computer Science
CSU Stanislaus
California State University
Computer Architecture
Simulators for Different Instruction Formats
Xuejun Liang
Fall 2020
Introduction
Assembly language programming and writing, using and modifying processor simulators are major hands-on assignment categories in an undergraduate computer architecture course. There are many computer architectures with different instruction formats such as stack-based, accumulator-based, two-address, or three-address machine. But, in general, only one architecture will be chosen for teaching assembly language programming in a computer architecture class or textbook. It is certainly desirable to have various simple simulators, each for one major computer processor architecture, so that students can program and compare these processors.
To this end, seven simple computer architecture simulators are designed and implemented for different instruction formats, including stack-based, accumulator-based, two-address (2A), and three-address (3A) machines. Both memory-to-memory (M2M) and register-to-register (R2R) architectures are implemented for both 2A and 3A machines. In addition, memory-to-register (M2R) architecture is implemented for 2A machine. These simulators can be used to assemble and run assembly language programs on these simulated computer architectures. Several simple applications are used to illustrate how to develop assembly language programs to deal with arithmetic expressions, arrays, loops, stacks, subroutines, and recursions on these computer architectures.
Students will have a better understanding of computer architectures by using these simulators for their assembly language programming exercises. Students can also modify these simulators to add more instructions, debugging functions, and etc. In addition, these simulated machines can serve as the compiler’s target machines for the code generation practice.
My Papers and Presentations about These
Simulators
Paper: Computer Architecture Simulators for Different Instruction Formats
Presentation: Computer Architecture Simulators for Different Instruction Formats
Paper: More on Computer Architecture Simulators for Different Instruction Formats
Presentation: More on
Computer Architecture Simulators for Different Instruction Formats
| //Program A //Declaration Num1    1       //Variable holding the first number         Num2    1       //Variable holding the second number    Sum     1       //Variable the sum         END //Code     READ        //Read the first number, AC = 23      PUT
  Num1    //Store the first number in Num1     READ        //Read the second number, AC = 48     PUT
  Num2    //Store the second number in
  Num2     ADD
  Num1    //Add the first number, AC =
  48+23     PUT
  Sum     //Store sum at address Sum      PRNT        //Print the Sum     STOP        //Terminate program      END //User input 23              //The
  first number to add 48              //The
  first number to add | //Program B //Declaration Num1    1   23  //The
  first number to add       Num2    1   48  //The
  second number to add        Sum     1       //The sum        END //Code     GET
  Num1    //Get the first number, AC = 23          ADD
  Num2    //Add the second number, AC =
  23+48     PUT
  Sum     //Store sum at address Sum      PRNT        //Print the Sum     STOP        //Terminate program      END //No user input | 

| Name | Number | Usage | 
| $zero | $0 | The constant value 0 | 
| $at | $1 | Reserved for assembler | 
| $v0-$v1 | $2-$3 | Expression evaluation
  and results of a function | 
| $a0-$a3 | $4-$7 | Argument 1-4 | 
| $t0-$t7 | $8-$15 | Temporary (not preserved
  across call) | 
| $s0-$s7 | $16-$23 | Saved temporary
  (preserved across call) | 
| $t8-$t9 | $24-$25 | Temporary (not preserved
  across call) | 
| $k0-$k1 | $26-$27 | Reserved for OS kernel | 
| $gp | $28 | Pointer to global area | 
| $sp | $29 | Stack pointer | 
| $fp | $30 | Frame pointer | 
| $ra | $31 | Return address (used by
  function call) | 
| op | Instruction | Explanation | 
| 0 | ADD     | Pop the top two addends,
  add, and push the sum | 
| 1 | SUB      | Pop the subtrahend and
  minuend, subtract, and push the difference | 
| 2 | MUL    | Pop the multiplicand and
  multiplier, multiply, and push the product | 
| 3 | DIV      | Pop the dividend and
  divisor, divide, and push the quotient | 
| 4 | REM     | Pop the dividend and
  divisor, divide, and push the remainder | 
| 5 | GOTO Label | Unconditionally jump to
  the instruction at address Label | 
| 6 | BEQZ  Label | Pop the top item and
  jump to Label if the popped item is zero | 
| 7 | BNEZ  Label | Pop the top item and
  jump to Label if the popped item is not zero | 
| 8 | BGEZ  Label | Pop the top item and
  jump to Label if the popped item is greater than or equal to 0 | 
| 9 | BLTZ  Label | Pop the top item and
  jump to Label if the popped item is less than 0 | 
| 10 | JNS     Label | Push the return address
  and transfer the control to the instruction at address Label | 
| 11 | JR        nLoc | Pop the return address
  into PC and decrement SP by nLoc | 
| 12 | PUSH  FP | Push the content of FP
  on stack | 
| 13 | PUSH  FP+Imm | Push M[FP+Imm] on stack | 
| 14 | PUSH  Imm | Push a 16-bit integer
  value Imm on stack | 
| 15 | PUSH  Var | Push M[Var] on stack | 
| 16 | PUSHI Var | Push M[M[Var]] on stack | 
| 17 | POP     FP | Pop the top item into FP
  from stack | 
| 18 | POP     FP+Imm | Pop the top item into M[FP+Imm] from stack | 
| 19 | POP     Var | Pop the top item into
  M[Var] from stack | 
| 20 | POPI    Var | Pop the top item into
  M[M[Var]] from stack | 
| 21 | SWAP | Swaps the top two items
  on the stack | 
| 22 | MOVE | Copy content of SP
  into FP | 
| 23 | ISP       nLoc | Increase/decrease SP
  by nLoc | 
| 24 | READ | Read an input and push
  it on stack | 
| 25 | PRNT | Print the top item on
  stack  | 
| 26 | STOP | Terminate the program | 
| Op | Instruction | Meaning | 
| 0 | LI        Imm  LA       Var                  | AC ß Imm          AC ß address
  of Var | 
| 1 | ADDI  Imm  | AC ß AC+Imm | 
| 2 | ADD   Var  | AC ß
  AC+M[Var] | 
| 3 | SUB    Var  | AC ß
  AC-M[Var] | 
| 4 | MUL   Var | AC ß
  AC*M[Var] | 
| 5 | DIV     Var | AC ß
  AC/M[Var] | 
| 6 | REM   Var  | AC ß
  AC%M[Var] | 
| 7 | GET    Var  | AC ß M[Var] | 
| 8 | PUT    Var | M[Var] ß AC | 
| 9 | GOTO Label | PC ß
  Label | 
| 10 | BEQZ  Label | If AC = 0 then PC
  ß
  Label | 
| 11 | BNEZ  Label | If AC  | 
| 12 | BGEZ  Label | If AC  | 
| 13 | BLTZ  Label | If AC < 0 then
  PC ß Label | 
| 14 | JNS     Label | Push the return address
  and PC ß Label | 
| 15 | JR    | Pop the return address
  into PC | 
| 16 | READ | Read an input and save
  it to AC  | 
| 17 | PRNT | Print AC | 
| 18 | STOP | Terminate the program | 
| 19 | GETI  Var  | AC ß
  M[M[Var]] | 
| 20 | PUTI  Var | M[M[Var]] ß AC | 
|  | Instruction | Meaning | 
| 0 | LI         A 
  Imm  LA        A Var | M[A] ß Imm          M[A] ß
  address of Var | 
| 1 | ADDI   A  Imm  | M[A] ß
  M[A]+Imm | 
| 2 | ADD    A 
  B  | M[A] ß
  M[A]+M[B] | 
| 3 | SUB     A 
  B | M[A] ß M[A]-M[B] | 
| 4 | MUL    A 
  B | M[A] ß
  M[A]*M[B] | 
| 5 | DIV      A 
  B | M[A] ß
  M[A]/M[B] | 
| 6 | REM    A 
  B | M[A] ß
  M[A]%M[B] | 
| 7 | GET     A 
  B | M[A] ß
  M[M[B]] | 
| 8 | PUT     A 
  B | M[M[B]] ß
  M[A] | 
| 9 | GOTO  Label | PC ß
  Label | 
| 10 | BEQZ  A 
  Label | If M[A] = 0 GOTO Label | 
| 11 | BNEZ  A 
  Label | If M[A]  | 
| 12 | BGEZ  A 
  Label | If M[ | 
| 13 | BLTZ  A 
  Label | If M[A] < 0 GOTO
  Label | 
| 14 | JNS     Label | M[SP] = M[SP]+1,
  M[M[SP]] = PC, & PC ß
  Label | 
| 15 | JR | PC ß
  M[M[SP]] & M[SP] = M[SP]-1 | 
| 16 | READ  | M[INPUT] ß
  Input | 
| 17 | PRNT | Display M[OUTPUT]
  on screen | 
| 18 | STOP | Terminate program | 
|  | Instruction | Meaning | 
| 0 | LI        A 
  Imm  LA       A 
  Var | M[A] ß Imm          M[A] ß
  address of Var          | 
| 1 | ADDI  A 
  C  Imm
   | M[A] ß
  M[C]+Imm | 
| 2 | ADD    A 
  C  B  | M[A] ß
  M[C]+M[B] | 
| 3 | SUB     A 
  C  B  | M[A] ß
  M[C]-M[B] | 
| 4 | MUL    A 
  C  B | M[A] ß
  M[C]*M[B] | 
| 5 | DIV      A 
  C  B | M[A] ß
  M[C]/M[B] | 
| 6 | REM    A 
  C  B  | M[A] ß
  M[C]%M[B] | 
| 7 | GET     A 
  C  B | M[A] ß
  M[C+M[B]] | 
| 8 | PUT     A 
  C  B | M[C+M[B]] ß
  M[A] | 
| 9 | GOTO  Label | PC ß
  Label | 
| 10 | BEQ     A 
  C  Label | If M[A] =  M[C] 
  GOTO Label | 
| 11 | BNE     A 
  C  Label | If M[A] ≠ M[C] GOTO Label | 
| 12 | BGE     A 
  C  Label | If M[A] ≥ M[C] GOTO Label | 
| 13 | BLT      A 
  C  Label | If M[A] <  M[C] 
  GOTO Label | 
| 14 | JNS       Label | M[SP] = M[SP]+1,
  M[M[SP]] = PC, & PC ß
  Label | 
| 15 | JR | PC ß
  M[M[SP]] & M[SP] = M[SP]-1 | 
| 16 | READ  | M[INPUT] ß
  Input | 
| 17 | PRNT | Display M[OUTPUT]
  on screen | 
| 18 | STOP | Terminate program | 
|  | Instruction | Meaning | 
| 0 | LI         R 
  Imm  LA       R 
  Var | R ß Imm          R ß
  address of Var | 
| 1 | ADDI   R  Imm  | R ß
  R+Imm | 
| 2 | ADD    R 
  R1  | R ß
  R+R1 | 
| 3 | SUB     R 
  R1 | R ß
  R-/R1 | 
| 4 | MUL    R 
  R1 | R ß
  R*R1 | 
| 5 | DIV      R 
  R1 | R ß
  R/R1 | 
| 6 | REM    R 
  R1 | R ß
  R%R1 | 
| 7 | GET     R 
  R1  | R ß
  M[R1] | 
| 8 | PUT     R 
  R1 | M[R1] ß
  R | 
| 9 | GOTO  Label | PC ß
  Label | 
| 10 | BEQZ   R 
  Label | If R = 0 GOTO Label | 
| 11 | BNEZ   R 
  Label | If R  | 
| 12 | BGEZ   R 
  Label | If R  | 
| 13 | BLTZ   R 
  Label | If R < 0 GOTO Label | 
| 14 | JNS      Label | $ra ß
  PC & PC ß Label  | 
| 15 | JR         | PC ß
  $ra | 
| 16 | READ  | $v0 ß
  Input | 
| 17 | PRNT | Print $a0 | 
| 18 | STOP | Terminate program | 
|  | Instruction | Meaning | 
| 0 | LI         R 
  Imm  LA       R 
  Var | R ß Imm          R ß
  address of Var | 
| 1 | ADDI   R  Imm  | R ß
  R+ Imm | 
| 2 | ADD    R 
  A/R1  | R ß
  R+M[A]/R1 | 
| 3 | SUB     R 
  A/R1  | R ß
  R-M[A]/R1 | 
| 4 | MUL    R 
  A/R1 | R ß
  R*M[A]/R1 | 
| 5 | DIV      R 
  A/R1 | R ß
  R/M[A]/R1 | 
| 6 | REM    R 
  A/R1 | R ß
  R%M[A]/R1 | 
| 7 | GET     R 
  A/R1  | R ß
  M[A/R1] | 
| 8 | PUT     R 
  A/R1 | M[A/R1] ß
  R | 
| 9 | GOTO  Label | PC ß
  Label | 
| 10 | BEQZ   R  Label | If R = 0 GOTO Label | 
| 11 | BNEZ   R 
  Label | If R  | 
| 12 | BGEZ   R 
  Label | If R  | 
| 13 | BLTZ   R 
  Label | If R < 0 GOTO Label | 
| 14 | JNS      Label | $ra ß
  PC & PC ß Label  | 
| 15 | JR         | PC ß
  $ra | 
| 16 | READ  | $v0 ß
  Input | 
| 17 | PRNT | Print $a0 | 
| 18 | STOP | Terminate program | 
| op | Instruction | Meaning | 
| 0 | LI        R Imm
   LA      R 
  Var | R ß Imm R ß address
  of Var | 
| 1 | ADDI R R1 Imm  | R ß
  R1+Imm | 
| 2 | ADD  R R1 R2  | R = R1 + R2 | 
| 3 | SUB   R R1 R2 | R = R1 + R2 | 
| 4 | MUL  R R1 R2 | R = R1 + R2 | 
| 5 | DIV    R R1 R2 | R = R1 + R2 | 
| 6 | REM  R R1 R2 | R = R1 + R2 | 
| 7 | GET   R R1 offset | R ß
  M[R1+ offset] | 
| 8 | PUT   R R1 offset | M[R1+ offset] ß R | 
| 9 | GOTO L | PC ß L | 
| 10 | BEQ   R1 R2 
  Label | If R1 = R2  GOTO Label | 
| 11 | BNE   R1 R2 
  Label | If R1  | 
| 12 | BGE   R1 R2 
  Label | If R1  | 
| 13 | BLT   R1 R2 
  Label | If R1 < R2  GOTO Label | 
| 14 | JNS    Label | $ra ß PC
  & PC ß Label | 
| 15 | JR | PC ß
  $ra | 
| 16 | READ 0 0 0 | $v0 ß
  Input | 
| 17 | PRNT 0 0 0 | Print $a0 | 
| 18 | STOP 0 0 0 | Stop |