I recommend you to go through my previous article on Abstraction Layers to better understand Instruction Set Architecture. It’s time to go one step deeper.
Why we need an Instruction Set Architecture?
If you have read my previous post, you may know the fact that computers only understand binary i.e. 1’s and 0’s. For any communication to be understandable, certain rules are required. For a CPU, these set of rules are laid down by the Instruction Set Architecture. ISA is a topic under Computer Architecture.
What is ISA?
Instruction Set Architecture or ISA comprises of certain instructions (array of 1’s and 0’s) called instruction sets and a certain hardware inside the CPU to decode these instructions. Thus, it forms an important link between the hardware and the software. A CPU having an ISA cannot understand an Instruction from another ISA. It would be like – a man talking in Spanish with me (Everything going over my head). Famous microprocessor ISAs include Intel x86 and AMD64, whereas famous microcontroller ISAs are MIPS, ARMv6 and ARMv7.
Instruction sets have their rules like a language has grammar. An instruction includes an opcode that specifies the operation to perform, such as add/multiply/move contents of memory to register—and operand specifiers, which may specify registers, memory locations, or literal data.
According to the opcode, the CPU actually triggers the necessary circuit associated with it to get the required output. These instructions are stored in the ROM of the system and are called upon as the program executes.
NOTE 1: Many CPUs have the same ISA, but the implementation may be different i.e. they use the same set of instructions to execute a particular operation, but the hardware used to implement these instructions may be different. For example, let’s say we have an Intel processor and an AMD processor which can implement x86 instruction sets, meaning both understand the same instructions. However, their logic circuits and gates inside their CPU may be completely different. But at the end of the day, they will give you the same result. Suppose we want to implement an instruction to add two numbers, the gate logic circuit performing this addition process may be different. But both this logic circuits will give you the same answer.
NOTE 2: The implementation actually decides the performance of the CPU. Say, for example, Intel i7 processor and the Intel Core2Duo understand the same instructions, but the i7 processor is going to be much faster. This is because of the difference in its microarchitecture (interconnected logic gate circuit). It would be like comparing a Maruti Alto with Ferrari. Both understand the same instruction of pressing the accelerator paddle, but the acceleration you get in a Ferrari would be incredibly more than that from Maruti Alto. This is because Ferrari’s engine-architecture can execute the same instruction much faster than the Alto.
How do I take care of different ISAs on different microcontrollers?
Well, when we are programming with a high-level programming language, we have to worry little about ISA. The compiler takes care of everything. We need to fill in certain settings, telling the compiler what microcontroller or what architecture we plan to work on. And it converts our high-level language to the necessary assembly language using its internal converting techniques.
NOTE: The assembly code generated for the same C code using two different compilers may be different. This is because they use their own optimization and disassembling techniques.
If you plan to program using assembly language, you will have to go through the datasheet of the microcontroller and understand the ISA it uses. You will get the required instructions in the manual associated with that particular ISA and then you will have to develop your code.
NOTE: To develop a concise and compact code, assembly language is preferred. Using C/C++ programming language, the compiler is intelligent, but it does not know our exact intent and may generate anomalous (very rare but dangerous) or lengthy (frequently observed but not fatal) assembly code. But there are different architectures available in the market, so if you switch from one controller to another, chances are, that you will have to learn a new ISA. This is the reason why most of the programmers (including me) prefer a high-level programming language.