From the course: Advanced C Programming: Integrating C and Assembly Language

Exploring assembly language

- [Instructor] Here is assembly language code. Like all programming languages, it has a visually poetic quality to it. The source code is divided into sections, data for initialized data, bss for uninitialized data or storage and text for coding instructions Underscore start is a label like a function name in C. It's global in that its name is accessible to other modules. The start label is where the program starts for the link I'm using. The word main is also used but with other linkers. The lines that follow are assembly instructions that manipulate the CPU at the atomic level. Specifically what's happening is that these instructions are manipulating the processors' registers. In line 25 you see that the memory address of prompt is placed into register rsi. This action directly manipulates the processor. Yes, this presentation is cryptic. No help here from a human language other than my variable and function names. The code outputs a prompt, accepts input and performs output again. And this may seem like a lot of code for a trivial task when compared with similar actions in other programming languages and that's right. It is, it's a lot of work. The results of your labors are a very small program, smaller than could be generated by just about any other compiler or assembler. Technically the size of this program is a lot less than the two K bytes shown here. It's only a few bites in size. This is one reason why assembly was preferred back in the days of computers with tight memory and slow processors. To appreciate the assembly, you must understand what you're programming. Here is a crude diagram of atypical Intel processor. It contains different types of registers that can be programmed in assembly. These are general purpose registers used for temporary storage of values. Different register names are used for different bit widths. The instruction point of register tracks which instruction to execute next. A flag's register reports the effect of certain operations and floating point registers are used for real number math operations. Four general purpose registers are illustrated here. They are referenced as letters A through D. The registered names depend on the size of the data they're manipulating, and they're named for compatibility with older CPU's. al and ah for example, are 8-bit registers. These combined to form the ax register which is considered a 16 bit register, register eax includes the ax registers values as well as 16 more bits that make for 32-bits of storage and register rax is 64-bits wide and includes the values of all the other, a type registers. The assembly instruction set for Intel processors is vast with hundreds of pneumonics representing machine language instructions. Here are some common ones; mov, which transfers the contents of one register into another or loads of value into a register or loads of value at a memory address similar to a pointer. sub is the subtraction operator. jmp is jump which controls program flow. cmp is used to compare values. And these are, but a few examples. Each of which manipulates the processor, it's registers and memory microscopically. The advantages of assembly are in its speed. The code runs as fast as it can go. And the code is tight. No overhead is added by a compiler or interpreter. On the minus side, development time for assembly language programming is S-L-O-W. It's like building a house one molecule at a time. Further errors and bugs are difficult to track without a debugger. The assembler alerts you to certain things but not flubs or flaws in logic or any other fouls that you would normally expect from a compiler or linger. These are good reasons why it pays to integrate assembly into a higher language like C. In this marriage you get the best of both worlds.

Contents