The Hello World program in C:
#include <stdio.h> int main( ) { printf("Hello World\n"); } |
.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:
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 |
.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:
|
.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)
|
.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)
|
|
.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)
|
.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)
|
.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})
|
.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)
|
.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
|
.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
|
.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)
|
Program organization of the Hello program:
Everything in the program are converted into binary numbers !!!