The MIPS architecture - University of Alberta
Download
Report
Transcript The MIPS architecture - University of Alberta
In this lecture
Global variables
Local variables
System stack
18/07/2015
CMPUT 229
1
Global variables
Exist for duration of program
Accessible from any part of program
registers too volatile
Use memory
data segment
18/07/2015
# ASSEMBLY PROGRAM
# Global variables
# follow.
.data
CMPUT 229
2
Global variables
Initialized global variables are
declared with initial value
int (32 bits) with .word and value
other types according to size
/* C PROGRAM */
/* declare var = -42 */
int var = -42;
# ASSEMBLY PROGRAM
# Global variables
# follow.
.data
var:
.word -42
.word allocates 4-byte value
18/07/2015
CMPUT 229
value to
store
3
Global variables
Uninitialized global variables are
allocated by specifying size only
use .space directive with bytes
/* C PROGRAM */
/* declare var = -42 */
int var = -42;
/* empty has no
initial value. */
int empty;
# ASSEMBLY PROGRAM
# Global variables
# follow.
.data
var:
.word -42
empty: .space 4
.space allocates empty space
18/07/2015
CMPUT 229
number
of bytes
4
Memory diagrams
contents (some
uninitialized)
/* global variables */
int n = 42;
int i;
int *iptr = &i;
start of
data
segment
lower
addresses
variable
names
n
42
0x10000000
i
???
0x10000004
0x10000004
0x10000008
iptr
when variables contain
addresses of other variables,
helpful to draw arrow (pointer)
18/07/2015
CMPUT 229
higher
addresses
5
Local variables
Properties of local variables
accessible only within function
may have more than one variable with same
name (in different functions)
may have more than one version of the same
function’s variables existing (recursion)
Properties of data segment
accessible from all of program
all labels must be different
each location can hold only one discrete value
Data segment is not suited to storing
local variables
18/07/2015
CMPUT 229
6
Local variables
Properties of local variables
must be allocated at function entry
must be destroyed at function exit
other functions may be called in
between, with same rules
void A()
{
int a;
/* create a */
B();
/* destroy a */
}
18/07/2015
void B()
{
int b;
/* create b */
C();
/* destroy b */
}
CMPUT 229
void C()
{
int c;
/* create c */
...
/* destroy c */
}
7
Local variables
Properties of local variables
allocation/destruction obeys LIFO (last in,
first out) principle
• like stack data structure
stack is ideal data structure for storing local
variables
• allocate a variable by pushing it on the stack
• destroy a variable by popping it off the stack
stack also helpful for storing other function
information, for same reason
• saving registers
• storing return address
• passing function arguments
18/07/2015
CMPUT 229
8
System stack
Function call/return mechanism is very
widespread among computers
Most computers provide a stack in
memory for programs to use
called system stack or runtime stack or stack
in MIPS, stack resides in its own segment of
memory
• stack segment, to address 0x7FFFFFFF
stack is initialized by operating system
user programs push/pop stack as needed
• instruction set provides operations for doing this
• Which ?
18/07/2015
CMPUT 229
9
System stack
Register $sp (stack pointer)
indicates top of stack
contains address of word of memory at
top of stack (with lowest address)
18/07/2015
CMPUT 229
10
System stack
lower
addresses
this is free
space for the
stack to grow
into
0x7FFFB308
0x7FFFB30C
0x7FFFB310
$sp
0x7FFFB310
0x7FFFB314
0x7FFFB318
all of these
words are part
of the stack
0x7FFFB31C
higher
addresses
18/07/2015
CMPUT 229
11
System stack: pushing
lower
addresses
to push
another word
(this one) onto
the stack ...
0x7FFFB308
0x7FFFB30C
0x7FFFB310
$sp
0x7FFFB310
0x7FFFB314
0x7FFFB318
subtract 4
from $sp ...
0x7FFFB31C
higher
addresses
18/07/2015
CMPUT 229
12
System stack: pushing
lower
addresses
to push
another word
(this one) onto
the stack ...
0x7FFFB308
0x7FFFB30C
0x7FFFB310
$sp
0x7FFFB30C
0x7FFFB314
0x7FFFB318
subtract 4
from $sp ...
0x7FFFB31C
then store a
value here
higher
addresses
18/07/2015
CMPUT 229
13
System stack: popping
lower
addresses
to pop a word
(this one) off
the stack ...
0x7FFFB308
... fetch this word
into a register ...
$sp
0x7FFFB30C
0x7FFFB310
0x7FFFB30C
0x7FFFB314
0x7FFFB318
... then add
4 to $sp
0x7FFFB31C
higher
addresses
18/07/2015
CMPUT 229
14
System stack: popping
lower
addresses
to pop a word
(this one) off
the stack ...
0x7FFFB308
... fetch this word
into a register ...
$sp
0x7FFFB30C
0x7FFFB310
0x7FFFB310
0x7FFFB314
0x7FFFB318
... then add
4 to $sp
0x7FFFB31C
higher
addresses
18/07/2015
CMPUT 229
15
Examples programs
stack1.a
stack6.a
18/07/2015
CMPUT 229
16