public class Hello { public static void main(String[] args) { System.out.println("Hello World"); } } |
// // hello.s: hello world in ARM assembler // // How to compile: // // as -o hello.o hello.s // gcc -o hello hello.o // .global main // ************************ Starting variable definition ***************** .data // *********************************************************************** HelloStr: // Label marking this location in memory .asciz "Hello World\n" // ASCII codes for the string // ************************ Starting computer instruction **************** .text // *********************************************************************** main: push {lr} // Save the return address on the stack push {fp} // Save the frame pointer on the stack // Explained later in CS255 /* ---------------------------------------- Pass the string to printf function ---------------------------------------- */ movw r0, #:lower16:HelloStr movt r0, #:upper16:HelloStr /* ---------------------------------------- Call the printf function ---------------------------------------- */ bl printf // Call printf function with input string pop {fp} // Pop the frame pointer pop {pc} // Pop the return address into PC // Explained later in CS255 .end Any text that follws ".end" is ignored (treated as comment) |
|
How to run the program:
ssh -X cs255@cs255host1 (passwd: abc123) cd /home/cs255/cs255/hello as -o hello.o hello.s gcc -o hello hello.o hello |
Output:
cs255@cs255host1(314)> hello Hello World |
(See: click here )
Notice that the assembler program contain the key words:
.text - this keyword announces that program instructions will follow .data - this keyword announces that constant and variables will follow |
The instructions that follows the keyword .text will be stored in the program section (green area)
The data and variable definitions that follows the keyword .data will be stored in the class variable section (yellow area)
System.out.println("Hello World"); |
This statement is a method (function) call statement where:
|
// ************************ Starting computer instruction **************** .text // *********************************************************************** main: push {lr} // Save the return address on the stack push {fp} // Save the frame pointer on the stack // Explained later in CS255 /* ---------------------------------------- Pass the string to printf function ---------------------------------------- */ movw r0, #:lower16:HelloStr movt r0, #:upper16:HelloStr /* ---------------------------------------- Call the printf function ---------------------------------------- */ bl printf // Call printf function with input string pop {fp} // Pop the frame pointer pop {lr} // Pop the return address // Explained later in CS255 |
I will skip the push and pop instructions - this is advanced material in assembler programming that will be explained later in CS255.
The main: identifier (called: label in assembler programming) corresponds to the location of the start of the program.
|
The program instruction in the Hello World program consists of the following 3 (assembler) instructions (given in red in the diagram below):
/* ---------------------------------------- Pass the string to printf function ---------------------------------------- */ movw r0, #:lower16:HelloStr movt r0, #:upper16:HelloStr /* ---------------------------------------- Call the printf function ---------------------------------------- */ bl printf // Call printf function with input string |
The first 2 instruction passes the parameter HelloStr to the function (= method)
The last instruction calls the function (method) printf (which is the same as println in java).
|
We call assembler programming: low level programming because an assembler programmer must know every detail of the computer processor in order to write the program.
In contrast: Java (and C) is a high level programming language where the programmer does not need to know the details of the computer processor !!!
public class Hello { public static void main(String[] args) { System.out.println("Hello World"); } } |
you may think that there are no constants defined in the program.
That's wrong, because:
"Hello World" is a String constant !!! |
// ************************ Starting variable definition ***************** .data // *********************************************************************** HelloStr: // Label marking this location in memory .asciz "Hello World\n" // ASCII codes for the string |
The string constant is defined using the syntax:
.asciz "Hello World\n" |
The identifier HelloStr: (called "label" in assembler programming - later) is used to mark the memory location where this string constant is stored.
The program instructions and String constant are stored inside the computer memory as follows:
Without a suitable tool, I cannot show you the internal organization of the program
This tool is design to:
|
I will use EGTAPI to show you the program organization in the next webpage