Review: the (in)famous Hello World program (in C)
 

The Hello World program in C:

  #include <stdio.h>

  int main( )
  {
     printf("Hello World\n");
  }
   

The Hello World program in ARM assembler ( pull out )
       .global main    // "Export" the identifier "main"

       .text
main:                  // Start of program
        push    {lr}   // Save the return address on the stack
        push    {fp}   // Save the frame pointer on the stack
                       // Explained later in CS255

        movw    r0, #:lower16:HelloStr
        movt    r0, #:upper16:HelloStr
        bl      printf  // Call printf function     

        pop     {fp}    // Pop the frame pointer
        pop     {pc}    // Pop the return address into PC
                        // Explained later in CS255

       .data
HelloStr:                      // Label marking this location
        .asciz "Hello World\n" // The Hello string
        .end 

Every assembler program starts executing at the instruction at address marked by main:

How to compile and run the Hello World program
 

DEMO:

You must run this program ARM assembler program on the Raspberry Pi cs255host1:

    ssh -X  cs255@cs255host1      (passwd: abc123)   

    cd   /home/cs255/cs255/hello              

    as    -o  hello.o   hello.s      // run assembler    

    gcc   -o  hello     hello.o      // run linker

    hello
   

Structure of a computer program

       .global main

       .text    (starts the instructions section)
main:
        push    {lr}   // Save the return address on the stack
        push    {fp}   // Save the frame pointer on the stack
                       // Explained later in CS255

        movw    r0, #:lower16:HelloStr
        movt    r0, #:upper16:HelloStr
        bl      printf  // Call printf function     

        pop     {fp}    // Pop the frame pointer
        pop     {pc}    // Pop the return address into PC
                        // Explained later in CS255

       .data    (starts the constants and variables section)
HelloStr:                      // Label marking this location
        .asciz "Hello World\n" // The Hello string (constant)
        .end 

An assembler program consists of:

  1. Instructions (statements) and data (constants and variables)   ( stored)
  2. Assembler directives and labels   ( auxiliary elements)

Line 1:   Assembler Directive

       .global main

       .text    (starts the instructions section)
main:
        push    {lr}   // Save the return address on the stack
        push    {fp}   // Save the frame pointer on the stack
                       // Explained later in CS255

        movw    r0, #:lower16:HelloStr
        movt    r0, #:upper16:HelloStr
        bl      printf  // Call printf function     

        pop     {fp}    // Pop the frame pointer
        pop     {pc}    // Pop the return address into PC
                        // Explained later in CS255

       .data    (starts the constants and variables section)
HelloStr:                      // Label marking this location
        .asciz "Hello World\n" // The Hello string (constant)
        .end 

Assembler directive: (e.g.: .global)

  • Assembler directive = a command/request to the assembler (= translator) asking the assembler to "do something"

    .global tells the Assembler to make main available to the Linker

Line 1:   Label

       .global main

       .text    (starts the instructions section)
main:
        push    {lr}   // Save the return address on the stack
        push    {fp}   // Save the frame pointer on the stack
                       // Explained later in CS255

        movw    r0, #:lower16:HelloStr
        movt    r0, #:upper16:HelloStr
        bl      printf  // Call printf function     

        pop     {fp}    // Pop the frame pointer
        pop     {pc}    // Pop the return address into PC
                        // Explained later in CS255

       .data    (starts the constants and variables section)
HelloStr:                      // Label marking this location
        .asciz "Hello World\n" // The Hello string (constant)
        .end 

Label: (e.g.: main)

  • Label = an identifier in an assembler program that is used to mark (identify) the memory location (= address) of an instruction or a variable

    main marks (identifies) the start location of the (assembler) program

Background info:   run time organization of programs

  • A running program is stored in the memory as follows:

    1. Computer instructions (= statements) are stored first in a Read Only Area
    2. Variables (= data) are stored next in a Read/Write Area
    3. Remaining memory are dynamically allocated for objects and parameter/local variables

Line 2:   Another Assembler Directive

       .global main

       .text    (starts the instructions section)
main:
        push    {lr}   // Save the return address on the stack
        push    {fp}   // Save the frame pointer on the stack
                       // Explained later in CS255

        movw    r0, #:lower16:HelloStr
        movt    r0, #:upper16:HelloStr
        bl      printf  // Call printf function     

        pop     {fp}    // Pop the frame pointer
        pop     {pc}    // Pop the return address into PC
                        // Explained later in CS255

       .data    (starts the constants and variables section)
HelloStr:                      // Label marking this location
        .asciz "Hello World\n" // The Hello string (constant)
        .end 

Assembler directive: (e.g.: .text)

  • Assembler directive = a command/request to the assembler (= translator) asking the assembler to "do something"

    .text tells the Assembler to store what follows in the Program Instruction section

Line 3:   Defining a label

       .global main

       .text    (starts the instructions section)
main:
        push    {lr}   // Save the return address on the stack
        push    {fp}   // Save the frame pointer on the stack
                       // Explained later in CS255

        movw    r0, #:lower16:HelloStr
        movt    r0, #:upper16:HelloStr
        bl      printf  // Call printf function     

        pop     {fp}    // Pop the frame pointer
        pop     {pc}    // Pop the return address into PC
                        // Explained later in CS255

       .data    (starts the constants and variables section)
HelloStr:                      // Label marking this location
        .asciz "Hello World\n" // The Hello string (constant)
        .end 

Label: (e.g.: main)

  • Label = an identifier in an assembler program that is a symbolic constant for the memory location (= a binary number) of an instruction or a variable

    main is equal to the starting address (binary number) of the (assembler) program

Line 4:   assembler instruction

       .global main

       .text    (starts the instructions section)
main:
        push    {lr}   // Save the return address on the stack
        push    {fp}   // Save the frame pointer on the stack
                       // Explained later in CS255

        movw    r0, #:lower16:HelloStr
        movt    r0, #:upper16:HelloStr
        bl      printf  // Call printf function     

        pop     {fp}    // Pop the frame pointer
        pop     {pc}    // Pop the return address into PC
                        // Explained later in CS255

       .data    (starts the constants and variables section)
HelloStr:                      // Label marking this location
        .asciz "Hello World\n" // The Hello string (constant)
        .end 

Assembler Instruction: (e.g.: push {lr})

  • Assembler instruction = a nmemomic code that represents a machine instruction (= a binary number)

    push {lr} is equal to the ARM instruction 11100101 00101101 11100000 00000100

Line 11:   Another Assembler Directive

       .global main

       .text    (starts the instructions section)
main:
        push    {lr}   // Save the return address on the stack
        push    {fp}   // Save the frame pointer on the stack
                       // Explained later in CS255

        movw    r0, #:lower16:HelloStr
        movt    r0, #:upper16:HelloStr
        bl      printf  // Call printf function     

        pop     {fp}    // Pop the frame pointer
        pop     {pc}    // Pop the return address into PC
                        // Explained later in CS255

       .data    (starts the constants and variables section)
HelloStr:                      // Label marking this location
        .asciz "Hello World\n" // The Hello string (constant)
        .end 

Assembler directive: (e.g.: .data)

  • Assembler directive = a command/request to the assembler (= translator) asking the assembler to "do something"

    .data tells the Assembler to store what follows in the Variables section

Line 12:   Another example of a label

       .global main

       .text    (starts the instructions section)
main:
        push    {lr}   // Save the return address on the stack
        push    {fp}   // Save the frame pointer on the stack
                       // Explained later in CS255

        movw    r0, #:lower16:HelloStr
        movt    r0, #:upper16:HelloStr
        bl      printf  // Call printf function     

        pop     {fp}    // Pop the frame pointer
        pop     {pc}    // Pop the return address into PC
                        // Explained later in CS255

       .data    (starts the constants and variables section)
HelloStr:                      // Label marking this location
        .asciz "Hello World\n" // The Hello string (constant)
        .end 

Label: (e.g.: HelloStr)                                                                                DEMO: demo/asm/0-misc/hello.s

  • Label = an identifier in an assembler program that is a symbolic constant for the memory location (= a binary number) of an instruction or a variable

    HelloStr is equal to the starting address of the string "Hello World\n"

Line 13:   Another Assembler Directive

       .global main

       .text    (starts the instructions section)
main:
        push    {lr}   // Save the return address on the stack
        push    {fp}   // Save the frame pointer on the stack
                       // Explained later in CS255

        movw    r0, #:lower16:HelloStr
        movt    r0, #:upper16:HelloStr
        bl      printf  // Call printf function     

        pop     {fp}    // Pop the frame pointer
        pop     {pc}    // Pop the return address into PC
                        // Explained later in CS255

       .data    (starts the constants and variables section)
HelloStr:                      // Label marking this location
        .asciz "Hello World\n" // The Hello string (constant)
        .end 

Assembler directive: (e.g.: .asciz)                                                     DEMO: demo/asm/0-misc/hello.s

  • Assembler directive = a command/request to the assembler (= translator) asking the assembler to "do something"

    .asciz tells the Assembler to store what follows in ASCII code (= string)

Line 14:   Another Assembler Directive

       .global main

       .text    (starts the instructions section)
main:
        push    {lr}   // Save the return address on the stack
        push    {fp}   // Save the frame pointer on the stack
                       // Explained later in CS255

        movw    r0, #:lower16:HelloStr
        movt    r0, #:upper16:HelloStr
        bl      printf  // Call printf function     

        pop     {fp}    // Pop the frame pointer
        pop     {pc}    // Pop the return address into PC
                        // Explained later in CS255

       .data    (starts the constants and variables section)
HelloStr:                      // Label marking this location
        .asciz "Hello World\n" // The Hello string (constant)
        .end 

Assembler directive: (e.g.: .end)

  • Assembler directive = a command/request to the assembler (= translator) asking the assembler to "do something"

    .end tells the Assembler to stop (any input that follows will be ignored)

How is the Hello World program stored in the computer memory

Program organization of the Hello program:

Everything in the program are converted into binary numbers !!!