Trace a given simple assembly language program

Resources | Subject Notes | Computer Science

Assembly Language - Trace a Program

Assembly Language - Trace a Simple Program

Objective

To trace the execution of a simple assembly language program and understand the flow of instructions.

Example Assembly Program (Simplified MIPS-like)

Consider the following assembly program:

        # Program to add two numbers
        # Input: num1 in register $t0, num2 in register $t1
        # Output: sum in register $t2

        add $t2, $t0, $t1
        # Assume other instructions for input and output are omitted for simplicity
    

Registers

This program uses a few registers:

  • $t0: Holds the first input number.
  • $t1: Holds the second input number.
  • $t2: Will hold the sum of the two numbers.

Instruction Set

The program uses a single instruction:

  • add $rd, $rs, $rt: Adds the contents of register $rs and register $rt and stores the result in register $rd.

Program Trace

Let's trace the execution of the program, assuming the registers are initialized as follows:

Instruction $t0 $t1 $t2 Program Counter
add $t2, $t0, $t1 Initial Value (e.g., 5) Initial Value (e.g., 10) Initial Value (e.g., 0) 0
add $t2, $t0, $t1 5 10 0 + 5 + 10 = 15 1

Explanation

The program starts by loading the values from registers $t0 and $t1 into the operands of the add instruction. The add instruction then performs the addition and stores the result in register $t2. The program counter (PC) is incremented by 1 to point to the next instruction.

Conclusion

By tracing the program instruction by instruction, we can understand how the assembly code manipulates data using registers and the instruction set architecture. This process is crucial for debugging and optimizing assembly language programs.