0% found this document useful (0 votes)
36 views14 pages

External Data & Text

External variables can be zero initialized or non-zero initialized. When a program loads, the text, data, and bss sections are placed in different memory regions. The text section holds machine code. The data section holds non-zero variables. The bss section holds zero initialized variables by skipping bytes. Variables can be accessed using load and store instructions after getting the address into a register with set. Relocation and linking allow labels to be globally accessible across modules that are compiled and linked together. Makefiles help manage dependencies and building of large projects with multiple source files.

Uploaded by

Jack Blair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views14 pages

External Data & Text

External variables can be zero initialized or non-zero initialized. When a program loads, the text, data, and bss sections are placed in different memory regions. The text section holds machine code. The data section holds non-zero variables. The bss section holds zero initialized variables by skipping bytes. Variables can be accessed using load and store instructions after getting the address into a register with set. Relocation and linking allow labels to be globally accessible across modules that are compiled and linked together. Makefiles help manage dependencies and building of large projects with multiple source files.

Uploaded by

Jack Blair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter 9

External Data & Text


Lecture Notes for SPARC
Architecture, Assembly language
programming and C, Richard P. Paul
By Ken Nguyen

External Variables
There

are two types of external variables:

Zero initialized.
Non-zero initialized.
When a program is loaded into memory, the program text,
initialized variable, and zero initialized variables are
loaded into different regions (text, data, and bss
sections, respectively.) of memory, typically starting at
0x2000 byte boundary.

Text Section
Text

section: Read only memory region,


where the machine codes go.
Addresses of machine instructions are relative
to the beginning of the program.
The beginning of the program is signified by
label main, usually made as global.
Calls/branches in a program are counter
relative. (relative to PC.)

Data Section
Data

section: Read/Write memory region,


where non-zero variables go.
Variables are specified by size, like: double
word, word, half word and byte.
Variables can be aligned on boundary using
.align
String/Character type can be represented in
three different ways: .byte, .ascii, and .asciz,
where .asciz gives a null terminated string.

Variables

must be initialized.

Example:

A)
Array: .word 0, 1, 2, 3, 4, 5
B)
NoMem:
.byte
Array2:
.byte 1, 2, 3, 4
NoMem has the same address as Array2

BSS Section
BSS

section (Block Starting Symbol):


Read/Write memory region, where zero
initialized variables go.
Memory allocation through skipping bytes.
Example:
array: .skip 4 * 100

! Initialize 400 byte to 0

.common

usually used to make a variable


in BSS global. Alignment can be provided.
Example:
.common array2, 4*100, 4
Zero 4*100 bytes and keep the first byte
aligned by 4 (word boundary.)

Accessing Variables
Use set instruction to get the address of a variable into a
register.
Use load or store instruction to read/write from/to the
variable.
Example
.section .data
a: .word 5

.section .text
set a, %l0 !get the address of a to %lo
ld [%l0], %o0
!get the value of a into %o0
What is the content of %o0 now????

Example cont.
.section .data
b: .byte 9

.section .text
set b, %l1 ! Load the address of b to %l1
mov 10, %l2 ! Move 10 to %l2
stb %l2, [%l1] ! Store value 10 into b
What is the value of b now????

set

is a combination of two instructions:


sethi and or. Therefore, do not use set in
delay slot.
Example:
set b, %l1
is equivalent to
sethi %hi(b), %l1
or %l1, %lo(b), %l1

Switch statement
Covered

in previous chapters

Relocation and Linking


A

program can be written in one or several


source modules.
A label must be made global to be accessible
from other source modules.
Each section (text, data, bss) is loaded into
memory at different locations at run time.
Therefore, assembler uses address of a label as
an offset from the beginning of the section.
When a section is loaded, the loader adds the
starting address of the loading section to all
references => no problem when any section is
relocated.

Compilation
Multiple

modules can be compiled and


linked as
gcc a.s b.s c.s o project
Where a.s, b.s, c.s are source modules

Make & makefile


Large

project with multiple modules can be hard


to keep track of all dependencies and updates =>
make ultility is used, instead.
make [f filename]
filename is a file with the following format
Target: dependent modules
command (first character is a tab i.e \t)
Example:
proj: project.s a.s b.s c.s
gcc o proj project.s a.s b.s c.s

You might also like