Answer: Assembly language code is a close representation for machine code for a given implementation. Therefore assembly language is specific for each architecture type. Assembly instructions begin with mnemonic. A mnemonic is a short string that is usually an abbreviation of the instruction’s function. For example, MULPS is the mnemonic used to represent a ‘MULtiply of Packed Single precision floating-point’ values. Following the mnemonic are zero or more operands. The legal number and types of operands depend on the instruction, but by convention, in AT&T syntax assembly the output operand is always the rightmost operand. An operand may be a register, a memory reference, or a literal value, usually called an immediate. Register operands can be used as input, output, or both and are indicated in assembly by prefixing the register name with the % character. Immediate operands are always used as input and are indicated in assembly by prefixing the literal value with the $ character. Sometimes, the literal value is something that is not known until link time such as the address of a global variable. In assembly, a link-time constant is represented symbolically as shown in the following instruction, which moves the address of x into register EAX.
movl $x, %eax
Operands not prefixed either by ‘%’ or ‘$’ are memory operands. Like register operands, memory operands can be used as input, output, or both.
My Linux runs on ‘Intel x86_64-bit’ architecture and therefore ‘gcc’ generates assembly code for it. In contrast to IA32 ISA (Instruction Set Architecture for Intel’s 32-bit), there have been introduced several new features apart from General Purpose Registers which are now doubled and are all 64-bit registers. Though, however, you can access different sub-sections of each register. For example, register ‘%rax’ is 64-bit and it’s 8-, 16- and 32- bit sub-sections can be accessed as ‘%al’, ‘%ax’ and ‘%eax’. It might be that your machine is different from mine, so, please refer to Instruction Set Manual for your machine to better understand assembly code it generates. Let’s now write a simple C program, generates assembly code and try to understand that,
/* count.c */ #include <stdio.h> int main(void) { int i, j = 0; for (i = 0; i < 10; i++) j = j + 1; return 0; }
Let’s produce assembly code for above program by executing following command
# gcc -S count.c
Above command when executed, gcc produced assembly code for ‘count.c’ C program into a file ‘count.s’. Notice the suffix “.s” indicates file contains assembly code.
Let’s give our attention to assembly code produced for ‘count.c’ program given below
.file "count.c" .text .globl main .type main, @function main: .LFB0: .cfi_startproc #pushes the return address on the top of stack pushq %rbp #moves current stack pointer into register 'rbp' (base pointer) movq %rsp, %rbp #pushes local variable j = 0 to 8 bytes below the base pointer #and variable i = 0 to 4 bytes below from base pointer movl $0, -8(%rbp) movl $0, -4(%rbp) #jump to label .L2 jmp .L2 .L3: #body of for loop; increments both i and j by 1 addl $1, -8(%rbp) addl $1, -4(%rbp) .L2: #for loop comparision takes here cmpl $9, -4(%rbp) #jump to lable .L3 if variable i is less than equal to 9 jle .L3 movl $0, %eax popq %rbp ret .cfi_endproc .LFE0: .size main, .-main .ident "GCC: (GNU) 4.6.0 20110428 (Red Hat 4.6.0-6)" .section .note.GNU-stack,"",@progbits
Notice that “.text” directive causes to enter text segment where all executable instructions are stored. Further, I edited the code by deleting some irrelevant instructions and inserted comments prefixed with ‘#’ character to simplify understanding assembly code for ‘count.c’ program.
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Apply for C Internship
- Check Computer Science Books
- Watch Advanced C Programming Videos
Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.