UPC EETAC Bachelor's Degree in Telecommunications Systems and in Network Engineering EEL

 

 

Basic ideas on assembly language

L9


Assembly language is encoded, saved in program memory and executed instruction by instruction by the microcontroller's CPU.

High-level C language may be compatible between several microcontroller families, even from different vendors, thus, designing projects using high-level languages and relying in compilers to obtain executable code is very convenient and practical. However, low-level assembly is specific for each family or microprocessor architecture, even if many instructions are compatible. Only very specific time constrained applications required such approach.

Assembly language is printed in microcontroller datasheets, for instance in PIC18F4520 chapter 24. many books on the subject explain the main details on how  machine code is executed.

Most instructions are a single program memory word (16 bits), but there are four instructions that require two program memory locations

Example of instruction format

Fig. 1. Example of instruction format. Most of them are 1-word (16-bit).

 

Example of instructions

Fig. 2. Example of some instructions.

Example C code and assembly in MPLABX.

Example of program (ROM)

Fig. 3. Example of C code disassembled in MPLABX simulation environment. We can track step by step

When working at this level, normally for debugging purposes, all the microcontroller resources are available for watching content and addresses.

All single-word instructions are executed in a single instruction cycle, unless a conditional test is true or the program counter is changed as a result of the instruction. In these cases, the execution takes two instruction cycles, with the additional instruction cycle(s) executed as a NOP. The double-word instructions execute in two instruction cycles.

One instruction cycle consists of four oscillator periods. Thus, for an oscillator frequency of 4 MHz, the normal instruction execution time is 1 μs. If a conditional test is true, or the program counter is changed as a result of an instruction, the instruction execution time is 2 μs. Two-word branch instructions (if true) would take 3 μs.

In this way we can estimate how long does it take to execute a given section of code. And normally we will do it using breakpoints when debugging.

Debugging in Proteus

Fig. 4. Similar code view when debugging in Proteus.

 

Example question: in P9 tutorial on the Adder_BCD_1digit, (1) count and print the number of assembly instructions in the function truth_table(). (2) calculate how long does it take to execute the function if the μC oscillator is a 12 MHz quartz crystal.