SPARC Segments
- External variables
- The text segment
- The data segment
- The bss segment
- Initialized variable declarations:
C Variables
- "extern": globally visible
- "static": only visible within file/function
- "automatic": on stack
- "register": in registers (do not have address)
- "volatile": cannot be cached
- constants: read-only (e.g. strings)
SPARC segments
- variables initialized to 0 (default):
-- bss segment
- variables initialized to non-zero values:
-- data segment
- read-only constants and executable code, initialized to non-zero
values:
-- text segment
- stack, variable-sized area, uninitialized:
-- stack segment
SPARC segments
- segments are aligned to page boundaries (multiples of 0x2000 or 8192)
- anything declared in an assembly program goes into the text segment
unless otherwise declared.
- declarations .text, .data, .bss, tell the assembler
that what follows goes into the specified segment
SPARC memory organization
location | description |
0x0 | 1 page of system-related stuff |
0x2000 | text segment |
| data segment |
| bss segment |
| unused memory |
%sp | stack segment |
|
text segment
- loaded from binary file
- read-only
- starts at location 0x2000
- mostly location-independent:
- stack locations are relative to %sp or %fp
- all branches are relative
- main must be known (which is why we use .global)
data segment
- loaded from binary file
- read-write
- .word for one or more 32-bit quantities
- .half for one or more 16-bit quantities
- .byte for one or more 8-bit quantities:
- .byte 150, 145, 154, 154, 157
- .byte "h", "e", "l", "l", "o"
- .ascii "hello"
- .ascii for strings of characters (unterminated)
- .asciz for null-terminated strings of characters (C strings)
Example: Hello World
main () { printf("hello world\n"); }
can be written as
include(macrodefs.m)
.global printf
format: .asciz "hello world\n"
.align 4
beginmain
set format, %o0
call printf
nop
endmain
String is in text segment, and is read-only.
.global
declarations
- used for symbols visible outside this file
- code labels (entry points), variable labels
- only .global symbols are visible to the debugger
- all non-global symbols are only visible within the file
they are declared in
- each global symbol should only be declared once
- .common name, size lets us declare name any
number of times; each occurrence refers to the same memory
.align
declaration
- assembler context for each line includes:
- current segment s
- current segment counter c
- .align n increases c
by a number i \in (0... n - 1) so c is a multiple of n
Example: Array of Pointers
char * wkday [] = {"mon",
"tue", "wed", "thu",
"fri", "sat", "sun"}
get pointer to Thursday (wkday[3]) into %o0:
.align 4
.global wkday
wkday: .word monm, tuem, wedm
.word thum, frim, satm,
.word sunm
monm: .asciz "mon"
...
sunm: .asciz "sun"
set wkday + (3 << 2), %o0
ld [%o0], %o0
bss segment
- size loaded from binary file
- automatically initialized to zero
- read-write
- .skip size to reserve space
bss segment examples
.bss
.align 4
arraym:.skip 8 * 100
cm: .skip 1
.align 4
im: .skip 4
.align 2
.common shared, 50