• Keine Ergebnisse gefunden

Relationship between Program Structure and Memory

EPSONStarting up and checking operation

3.4 Relationship between Program Structure and Memory

This section briefly explains the concept of section management applied to the creation and linkage of source files.

Although it is not specifically have to been concerned about sections in the C source, the assembly source requires that sections be explicitly be defined so that they can be created and linked.

In addition to programs to control the CPU and peripheral circuits, the source file contains descriptions of data such as font data which are always fixed and do not require initialization, symbols for the variables placed in RAM, and I/O memory control registers. Data and symbols that take on different attributes like these finally need to be relocated into the corresponding physical memory locations by the Linker. For example, programs are relocated into a program ROM area, fixed data are relocated into a data ROM area. For this reason, the object code is designed to be classified into sections by attribute.

The following three types of sections exist:

1. CODE section Block for programs and fixed data that have initial values

2. DATA section Block for data that have initial values and can be accessed for read or write 3. BSS section Block that is mapped into RAM

For assembly source

For the assembly source, use the following assembler pseudo-instructions to specify a section:

.code pseudo-instruction Beginning of a CODE section .data pseudo-instruction Beginning of a DATA section

.comm/.lcomm pseudo-instructions Symbol definition to a BSS section and area allocation The following shows the method of specification (see Chapter 11 for details):

· Before describing the program and fixed data to be written to the ROM, declare the beginning of a CODE section by using the .code pseudo-instruction. The source code following this declaration is assembled as the object of a CODE section. If no section is defined, the Assembler assumes a CODE section from the beginning of the file.

· Before setting RAM data that have initial values, declare the beginning of a DATA section by using the .data pseudo-instruction. The source code following this declaration is assembled as the object of a DATA section. However, the initial values in the DATA section have to been copied to the RAM by program.

· If the program requires to secure a variable or work area in the RAM and reference its address with a symbol, allocate this area and define the symbol by using the .lcomm pseudo-instruction. The Assembler allocates a specified area in the BSS section. This area is mapped in the RAM or I/O area, with no object code created there. Symbol information enabling multiple modules to reference this area is created as a BSS section.

For relocatable assembly sources (including one that is created by compiling a C source), sections of the same attribute are located together as one continuous section. Consequently, the assembled module becomes an object that has one CODE section, one DATA section, and one BSS section. (Even undeclared sections are created as those that do not have any actual data.)

For an assembly source where absolute addresses are specified, sections of the same type cannot be put together into one section. In this case, therefore, as many sections as specified separately in the source are created.

For C source

For C sources, there is no need to specify sections in the source because sections are declared by the C Compiler. After the source is compiled, all instructions are located in the CODE section. Data is located in each corresponding section according to its attribute as follows:

Variables without an initial value (e.g., int i;): BSS section Variables with an initial value (e.g., int i=l;): DATA section Constants (e.g., const int i=1;): CODE section

The following shows the section definition of the sample program used in the tutorial as a simple example. Since the assembly source program "boot.s" consists of only a program code, only the .code pseudo-instruction is used.

<boot.s>

; boot.s 1997.2.13

; boot program

#define SP_INI 0x0800 ; sp is in end of 2KB internal RAM

#define GP_INI 0x0000 ; global pointer %r8 is 0x0 .code

.word BOOT ; BOOT VECTOR BOOT:

xld.w %r8,SP_INI

ld.w %sp,%r8 ; set SP

ld.w %r8,GP_INI ; set global pointer xcall main ; goto main

xjp BOOT ; infinity loop (Program explanation)

Boot processing is performed to initialize the stack and global pointers, and call the main function.

Do not use this program in actual applications because the actual applications require setting up the trap processing vector, etc.

<main.c>

/* main.c 1997.2.13 */

/* C main program */

int i;

main()

{ int j;

i = 0;

for (j=0 ; ; j++) { sub(j);

} }

sub(k) int k;

{ if (k & 0x1)

{ i++;

} }

CHAPTER 3: SOFTWARE DEVELOPMENT PROCEDURES

EPSON

Taking a look at the C Compiler output "main.ps" you will find that the CODE and the BSS sections are defined by the Compiler.

<main.ps>

.file "main.c"

; GNU C 2.7.2 [AL 1.1, MM 40] RISC NEWS-OS compiled by CC

; Cc1 defaults:

; -mmemcpy

; Cc1 arguments (-G value = 0, Cpu = 3000, ISA = 1):

; -quiet -dumpbase -g -O -o gcc2_compiled.:

__gnu_compiled_c:

.code .align 1

.def main, val main, scl 2, type 0x24, endef

.global main :: .comm i 4 .endfile

The symbol of global variable i is defined, and a 4-byte area is located in the BSS section by the ".comm" pseudo-instruction. Since variables j and k are local variables, they are allocated to general-purpose registers and stacks.

No BSS section is used for these variables.

When these modules are linked by make in the tutorial, separate CODE sections are combined into one section.

CODE1

CODE1

DATA1 DATA1

BSS1

BSS1

tst_asm test.srf

0x0000000 0x0080000 CODE2

CODE2

DATA2

DATA2

BSS2

BSS2 tst_main

Fig. 3.4.1 Section allocation after linkage

During a linking, each file and each section can be address-specified so that they correspond to the actual memory configuration. For details, refer to Section 12.5, "Linker Commands", and Section 12.6, "Locating Sections".

Take a look at the link map file generated by the lk33 and the disassembly list created from the linked object file

"test.srf" by the dis33. They will show how the sections are allocated after linkage.

Link map file <test.map>

Code Section mapping

Address Vaddress Size File ID Attr

00080000 --- 00000014 boot.o 0 REL

00080014 --- 00000034 main.o 0 REL

Data Section mapping

Address Vaddress Size File ID Attr

00080048 --- 00000000 boot.o 1 REL

00080048 --- 00000000 main.o 1 REL

Bss Section mapping

Address Vaddress Size File ID Attr

00000000 --- 00000000 boot.o 2 REL

00000000 --- 00000004 main.o 2 REL

The link map file shows the relationship between the sections in each file (File) and the located addresses (Address).

Disassembly list file <test.dis>

**** Disassemble code and source code ****

Addr Code Unassemble Line Source

00080000 0004 ***

00080002 0008 ***

boot.s ---00001 ; boot.s 1997.2.13 00002 ; boot program 00003

00004 #define SP_INI 0x0800 ; sp is in end of 2KB internal RAM 00005 #define GP_INI 0x0000 ; global pointer %r8 is 0x0 00006

00007 .code

00008 .word BOOT ; BOOT VECTOR

00009 BOOT:

00080004 C020 ext 0x20 00010 xld.w %r8,SP_INI

00080006 6C08 ld.w %r8,0x0

00080008 A081 ld.w %sp,%r8 00011 ld.w %sp,%r8 ; set SP

0008000A 6C08 ld.w %r8,0x0 00012 ld.w %r8,GP_INI ; set global pointer

0008000C C000 ext 0x0 00013 xcall main ; goto main

0008000E C000 ext 0x0 00080010 1C02 call 0x2

00080012 1EF9 jp 0xf9 00014 xjp BOOT ; infinity loop

main.c

---00001 /* tst_main.c 1997.2.13 */

00002 /* C main program */

00003 00004 int i;

00005 00006 main()

00080014 0200 pushn %r0 00007 {

00008 int j;

CHAPTER 3: SOFTWARE DEVELOPMENT PROCEDURES

EPSON

00021 {

00080032 C000 ext 0x0 00022 i++;

00080034 C000 ext 0x0 00080036 6C09 ld.w %r9,0x0 00080038 309A ld.w %r10,[%r9]

0008003A 601A add %r10,0x1 0008003C C000 ext 0x0 0008003E C000 ext 0x0 00080040 6C09 ld.w %r9,0x0 00080042 3C9A ld.w [%r9],%r10

00023 }

00080044 0640 ret 00024 }

00080046 0000 nop

The above is just a quick review of the sections. For more information, refer to the chapters where the Assembler and Linker are discussed.