How do I Run an Assembly Language Program in Linux?

Question: What is Assembly Language Code and How is it Produced for a C Program on Linux System

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Let’s give our attention to assembly code produced for ‘count.c’ program given below

        .file   "count.c"
        .globl  main
        .type   main, @function
        #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
        #body of for loop; increments both i and j by 1
        addl    $1, -8(%rbp)
        addl    $1, -4(%rbp)
        #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
        .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.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.