EMCH 367 Fundamentals of Microcontrollers Example 1

Example 1

Objective

This simple example has the following objectives:

·  Familiarize the user with the THRSim11 simulator environment

·  Introduce the user to the syntax and concepts of Assembly (.asm) language

·  Familiarize the user the way arithmetic operations are handled by the microcontroller

·  Instruct the user to interpret the List (.LST) file.

·  Teach the user to perform the simulation and follow the step-by-step results.

·  Introduce the LDAA, LDAB and ABA operations

·  Introduce immediate addressing mode

·  Introduce inherent addressing mode

program

Ex1.asm program is very simple. It performs the arithmetic operation 3 + 2 = 5 using the ABA operation in immediate mode. To achieve this, the program does the following operations:

·  Load the number 3 into accA using the opcode mnemonic LDAA with operand #3. The symbol # signifies that the number 3 is used immediately. For this reason, this mode of operation is called immediate mode).

·  Load the number 2 into accB using the opcode mnemonic LDAB with operand #2, i.e., using immediate mode

·  Add the number in accA (i.e., 3) with the number in accB (i.e., 2) using the opcode mnemonic ABA and no operand. This mode, which uses no operant, is called inherent mode.

The symbolic representation of this process is:

3 à A (immediate mode)

2 à B (immediate mode)

A + B à A (inherent mode)

This signifies that number 3 is loaded into accA, number 2 is loaded into accB, and the content of accA is added to the content of accB with the result being put back into accA.

execution

  1. Open THRSim11.
  2. Maximize THRSim11 window.
  3. Close the Commands window.
  4. Open file Ex1.asm.

  1. Assemble file by pressing the Assemble button.

During assembly, you must have inserted in the A: floppy disk drive the floppy disk with the file VAR_DEF.ASM. In this way, the assembler will find the file path A:\VAR_DEF.ASM when executing the Assembly directive #INCLUDE. Else, you get error.

  1. Tile windows by pressing the ‘Tile button’.
  2. Set a break point to the line containing the opcode mnemonic SWI.

To do so, select the line, right click, and chose ‘Set Breakpoint’. The line becomes amber.

  1. Change to decimal the display mode of registers A, B in the ‘CPU Registers’ window. To achieve this, right click on the line and select ‘Decimal’.
  2. Put zeros in (i.e. reset) registers D, X, Y

To achieve this, highlight the line, type in the values, and hit Enter. Unless you hit enter, the value will not take effect. (A and B get automatically reset by resetting D, since A and B are the two halves of D). Your screen should look like this:

The green highlight in the .LST window indicates which line of program is to be executed next. The memory address of the highlighted line, as given in the first column, is $c000. This means that the program counter (PC) at $c000. Indeed, in the CPU registers window, you can see PC $c000. The PC value indicates the memory address of the instruction to be executed next.

  1. Use the ‘Step’ button to step through the program.

For the Step button to be active, you need to be in the .LST window. Hence, select the .LST window, if you have not done so already.


Press the Step button once. Your screen should look like this:

The green highlight moves to the next line, which starts at address $c001. The program counter in the CPU registers window is PC $c001.

The line $c001 performs the loading of number 3 into accA. This line has not been executed yet. It will be executed next when the Step button is pressed.

  1. Press the Step button again. Your screen should look like this:

The green highlight has moved to the next line, starting at address $c003. It has jumped by 2 counts because the line that has just been executed had to Opcodes (i.e., machine instructions) in it, 86 and 03. Each Opcode takes up one memory address, hence the program has move forward by 2 locations. The current value of the program counter is PC $c003, as can be readily verified in the CPU registers window.

The result of executing the previous instruction is now apparent. The number 3 has been loaded into accA. You can verify this by noting that, in the CPU registers window, you see A 3.

  1. Press the Step button again. The screen looks like this:

The execution of the previous instruction has loaded the number 2 into accB, just as the number 3 was loaded into accA. The result of this operation is apparent in the CPU registers window where now we can see B 2.

  1. Press the Step button again. The screen looks like this:

The execution of the previous instruction has performed the addition between the contents of accA (i.e., 3) and the contents of accB (i.e., 2). The result of the operation has been put back into accA. This means that the arithmetic operation 3 + 2 = 5 has been executed. The result of the addition is shown in the CPU registers window: the accA has become A 5, as expected.

The green highlight has moved to the next line which starts at memory address $c006. (In the CPU registers window, the program counter indicates PC $c006.) The move of the program counter was only by one memory location because the operation ABA just executed does not have operand (inherent mode), and only requires one Opcode.

The line $c006 contains the opcode mnemonic SWI which signifies the end of the program. You are done! (Do not press the ‘Step’ again, because it will throw you out of the program area.)

  1. To rerun the program, you have to place the program counter again at the beginning of the program. Select the line $c001, i.e., where the meaningful part of the program starts. Right click and select ‘Place program counter on …’. The line that was selected becomes green highlighted. You can now step again through the program and see how it operates. You can also press the ‘Run’ button, and the program will automatically execute up to the next breakpoint. In our case, the program will run all the way to the SWI instruction where the breakpoint is placed.

What you have learned

In this simple exercise, you have learned quite a few things:

·  New words and concepts: Opcode, opcode mnemonic, immediate mode, Assembly language (.asm), list file (.LST), reset, program counter (PC), break point, registers, .

·  Microprocessor opcode mnemonics LDAA, LDAB, and ABA.

·  Meaning of immediate mode and how to invoke it by using the symbol #

·  Meaning of inherent mode.

·  Symbolic representation of the operations and their meaning

·  Open and customize THRSim11 environment

·  Assemble an .asm file

·  Interpret an .LST file

·  Tile the THRSim11 windows (remember that the tiling sequence follows the sequence in which you accessed the files, with the most recently accessed window tiled in the left upper corner!)

·  Put a value in a memory address, e.g. when you put zeros in the registers

·  Set a break point

·  Step through the program simulation and follow the changes taking place in the ‘CPU registers’ window.

·  See how program execution is affecting accA and accB. Identify the results of the calculation, i.e. 3 + 2 = 5 makes accA to take the value A 5.

Dr. Victor Giurgiutiu Page 83 8/4/2006

EMCH 367 Fundamentals of Microcontrollers Example 1

Dr. Victor Giurgiutiu Page 6 5/18/2004