That means:
|
Example: the Hello World program
.global main
// ************************ Starting variable definition *****************
.data
// ***********************************************************************
HelloStr:
.asciz "Hello World\n" // <--- This a String constant
// ************************ 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
pop {fp} // Pop the frame pointer
pop {lr} // Pop the return address
// Explained later in CS255
.end
|
After processing the .data directive, the assembler will:
|
The assembler will keep doing this until it encounters a .text dirctive
So you can switch between .data and .text section as often as you wish in an assembler program
In Java, you define an initialized variable as follows:
int x = 123; // x has an initial value 123 short y = 22; // y has an initial value 22 |
|
Each type of value is defined using its own assembler directive:
|
.byte Value - defines an 1 byte 2's complementary binary code the represents Value .2byte Value - defines a 2 bytes 2's complementary binary code the represents Value .4byte Value - defines a 4 bytes 2's complementary binary code the represents Value .float Value - defines a 4 bytes IEEE 754 binary code the represents Value .double Value - defines an 8 bytes IEEE 754 binary code the represents Value .asciz "..." - defines a series ASCII binary code the represents string "..." |
// ************************ Starting variable definition ***************** .data // *********************************************************************** DataStart: .byte 15 .2byte 15 .4byte 15 .float 5 .asciz "ABC" |
These assembler directives will translate into the following .data section -- I will show you this in class using EGTAPI:
How to run the program:
|
You can see the values stored in memory as follows:
|
In Java, an uninitialized variable is defined like this:
int x; or short y; |
.skip n // will reserve n bytes for a program variable |
Maybe you learned this in CS170 or CS171.
In any case, here are the sizes of the data types used in most high level programming languages:
byte type: 1 byte short type: 2 bytes int type: 4 bytes float type: 4 bytes double type: 8 bytes char type: 1 byte (Java uses 2 bytes for Unicode) |
In C/Java How to define such variable in assembler ------------ --------------------------------------------- int i; .skip 4 short s; .skip 2 float f; .skip 4 |
In order to access the constant/variable in the memory, we need to provide the address of constant/variable in memory !!!
So we must remember the location of every variable defined in the program:
|
|
Well, we have discuss this mechanism in the previous webpage...
But I will repeat the answer here for emphasis
This mechinism is called a label (see: click here )
For example: in the Hello World program
// ************************ Starting variable definition *****************
.data
// ***********************************************************************
HelloStr: // <---- This is a label
.asciz "Hello World\n"
|
Notice that:
|
// ************************ Starting variable definition ***************** .data // *********************************************************************** byteConst: .byte 15 shortConst: .2byte 15 intConst: .4byte 15 floatConst: .float 5 doubleConst: .double 5 strConst: .asciz "ABC" |
I.e.:
|
When the assembler finds a label (such as HelloStr:) in the assembler program, the assembler will equate the label to the memory address where it will put the next constant or program variable
How to run the program:
|
We will learn soon how to access a constant/program variable in a computer program using its label