Title: Pointers and arrays
1Pointers and arrays
Lecture notes section B13
2Last time
- C goto
- MIPS branch and jump instructions
- Selection
- if-else
- Iteration (loops)
- while
- for
- do-while
3In this lecture
- Pointers
- implementation
- pointer operations
- dereference () operator
- address () operator
- Arrays
- implementation
- indices and addresses
- relationship to pointers
4Pointers
- Pointer is a variable which contains address of
some data - pointer is same size, no matter how big the data
it points to is - in MIPS, 4 bytes
- to get address of a variable, use address
operator () - to access data through pointer, use dereference
operator ()
5Pointers operator
int var -42
0x12345678
int p var
p is a pointer it contains the address of a
piece of data
6Pointers the operator
- In MIPS, the address operator () is implemented
using la (load address) pseudoinstruction - la asks the assembler to determine the absolute
address of a memory value, and then build real
instructions to load that address into a
specified register - unlike lb, lw, etc, la does not access memory at
all, and certainly does not load the contents of
the location - it just transfers its address to a GPR
7Pointers the operator
variable memory
memory name content
address
- / Example of pointer
- operations (part 1). /
- char newline '\n'
- int main()
-
- int num 42
- int iptr
- char cptr
- / Get addresses
- of vars. /
- cptr newline
- iptr num
- / program continues
- later ... /
'\n' (10)
42
0x7FFE3034
0x10010000
8Pointers the operator
- / Example of pointer
- operations (part 1). /
- char newline '\n'
- int main()
-
- int num 42
- int iptr
- char cptr
- / Get addresses
- of vars. /
- cptr newline
- iptr num
- / program continues
- later ... /
- .data
- newline allocate one char.
- .byte '\n'
- .text
- main allocate 12 bytes of locals
- move fp, sp frame ptr
- subu sp, sp, 12
- Initialize num 42.
- li t0, 42 t042
- sw t0, -12(fp) num42
- cptr newline
- la t1, newline t1newl..
- sw t1, -4(fp) cptrt1
- iptr num
- la t2, -12(fp) t2num
9la in data/stack segments
- in the assignment of cptr newline
- address of newline is known to assembler from its
symbol table as 0x10010000 - assembler builds instructions to load this
compile-time 32-bit constant into t1 - la t1, newline ? lui at, 0x1001
ori t1, at, 0x0000 - in the assignment of iptr num
- absolute address of the stack segment item num is
a compile-time unknown (fp?,sp?) - but from stack diagram programmer can express it
in frame pointer relative terms num fp -12 - in assemblers format this is -12(fp) and it
builds this run-time address computation in one
real - la t2, -12(fp) ? addiu t2, fp, -12
10Pointers the operator
- In MIPS, dereference operator () implemented
using load instructions - lb, lh, lw, depending on size of thing being
pointed to (8, 16, or 32 bits) - or store instructions (sb, sh, sw) if dereference
is on left side of assignment - Load pointer value (the datas address) into
register reg with lw - Load data itself using the address 0(reg)
- lw t1, a_ptr t1 a_ptr (assume global)
- lw t2, 0(t1) t2 a_ptr memt10
- could use lw t1, 0(t1) to save registers
11Pointers the operator
- / Pointers, part 2. /
- / This doesnt use
- pointers (yet) /
- / Print num then newline. /
- printf("d", num)
- putc(newline)
- / more later ... /
- continued...
- printf("d", num)
- li v0, 1 print int
- lw a0, -12(fp) num
- syscall
- Syscall 11 prints one
- character (in a0).
- putc(newline)
- li v0, 11 print char
- lbu a0, newline
- syscall
- more...
12Pointers the operator
- / Pointers, part 2. /
- / This doesnt use
- pointers (yet) /
- / Print num then newline. /
- printf("d", num)
- putc(newline)
- / more later ... /
- continued...
- printf("d", num)
- li v0, 1 print int
- lw a0, -12(fp) num
- syscall
- Syscall 11 prints one
- character (in a0).
- putc(newline)
- li v0, 11 print char
- lbu a0, newline
- syscall
- more...
13Pointers the operator
- continued...
- iptr 87
- li t0, 87 t087
- lw t1, -8(fp) t1iptr
- sw t0, 0(t1) iptr87
- printf("d", iptr)
- li v0, 1 print int
- lw t0, -8(fp) t0iptr
- lw a0, 0(t0) a0iptr
- syscall
- putc(cptr)
- li v0, 11 print char
- lw t0, -4(fp) t0cptr
- lbu a0, 0(t0) a0cptr
- syscall
- / Pointers, part 3. /
- / Change num to 87
- through iptr. /
- iptr 87
- / Print num through iptr. /
- printf("d", iptr)
- / Print newline
- through cptr. /
- putc(cptr)
- exit(0)
14Pointers the operator
- continued...
- iptr 87
- li t0, 87 t087
- lw t1, -8(fp) t1iptr
- sw t0, 0(t1) iptr87
- printf("d", iptr)
- li v0, 1 print int
- lw t0, -8(fp) t0iptr
- lw a0, 0(t0) a0iptr
- syscall
- putc(cptr)
- li v0, 11 print char
- lw t0, -4(fp) t0cptr
- lbu a0, 0(t0) a0cptr
- syscall
- / Pointers, part 3. /
- / Change num to 87
- through iptr. /
- iptr 87
- / Print num through iptr. /
- printf("d", iptr)
- / Print newline
- through cptr. /
- putc(cptr)
- exit(0)
15Why use pointers in MIPS?
- All the same reasons to use pointers in C
- to pass values to functions by reference
- for efficiency
- to have them modified
- to dynamically allocate memory
- e.g., with malloc()
- to implement self-referential data structures
- e.g., lists, trees
- to implement arrays
- by pointer/array duality
- and, by extension, strings (arrays of char)
16Arrays
The high-level programmers view array is
accessed through indices 0, 1, 2, ...
0
1
4
9
16
17Arrays
The computers view array is part of memory,
accessed through addresses 0x10012FC0,
0x10012FC4, ...
0
0x10012FC0
1
0x10012FC4
4
0x10012FC8
9
0x10012FCC
16
0x10012FD0
18Arrays
To program arrays in assembly language, need to
understand relationship between indices and
addresses, and need to convert between them.
a0
0x10012FC0
a1
0x10012FC4
a2
0x10012FC8
a3
0x10012FCC
a4
0x10012FD0
19Arrays
addresses for a given array, address of first
element is constant (here, 0x10012FC0)
arrays first index is always 0
a0
0x10012FC0
a1
0x10012FC4
a2
0x10012FC8
a3
0x10012FCC
a4
0x10012FD0
20Arrays
arrays adjacent indices differ by 1
addresses addresses differ by size of array
element type (here, 4 bytes for int)
a0
0x10012FC0
a1
0x10012FC4
a2
0x10012FC8
a3
0x10012FCC
a4
0x10012FD0
21Arrays
- To compute address of ai
- determine start address of array
- address of a0
- numerically smallest address of entire array
- determine size of one element of a
- in bytes
- compute i
- can be an arbitrary integer expression
- address of element i equals start ( size i )
- Need load/store to access ais data
- since above calculation only computes the address
of (i.e., a pointer to) ai
22Arrays
- / Array example. /
- int g5
- 45, -3, 0, 16, -23
-
- int main()
-
- int i
- int total6
- / Get an array of running
- totals. /
- total0 0
- for (i 0 i lt 5 i)
-
- totali 1
- totali gi
-
23Arrays
- / Array example. /
- int g5
- 45, -3, 0, 16, -23
-
- int main()
-
- int i
- int total6
- / Get an array of running
- totals. /
- total0 0
- for (i 0 i lt 5 i)
-
- totali 1
- totali gi
-
- .data
- g .word 45, -3, 0, 16, -23
- .text
- main local variables
- 1 int (i)
- 6 int (total)
- 28 bytes
- move fp, sp
- subu sp, sp, 28
- total0 0
- la t0, -24(fp) total
- sw 0, 0(t0) total00
- i 0
- sw 0, -28(fp) i0
- i lt 5
24Arrays
- / Array example. /
- int g5
- 45, -3, 0, 16, -23
-
- int main()
-
- int i
- int total6
- / Get an array of running
- totals. /
- total0 0
- for (i 0 i lt 5 i)
-
- totali 1
- totali gi
-
- .data
- g .word 45, -3, 0, 16, -23
- .text
- main local variables
- 1 int (i)
- 6 int (total)
- 28 bytes
- move fp, sp
- subu sp, sp, 28
- total0 0
- la t0, -24(fp) total
- sw 0, 0(t0) total00
- i 0
- sw 0, -28(fp) i0
- i lt 5
25Arrays
- ... continued (part 2)
- shift left 2 used here
- to scale by sizeof(int)
- totali
- la t0, -24(fp) total
- lw t1, -28(fp) i
- sll t1, t1, 2 4
- add t0, t0, t1
- lw t2, 0(t0) t2totali
- gi
- la t0, g g
- lw t1, -28(fp) i
- sll t1, t1, 2 i4
- add t0, t0, t1
- lw t3, 0(t0) t3gi
- / Array example. /
- int g5
- 45, -3, 0, 16, -23
-
- int main()
-
- int i
- int total6
- / Get an array of running
- totals. /
- total0 0
- for (i 0 i lt 5 i)
-
- totali 1
- totali gi
-
26Arrays
- ... continued (part 2)
- shift left 2 used here
- to scale by sizeof(int)
- totali
- la t0, -24(fp) total
- lw t1, -28(fp) i
- sll t1, t1, 2 4
- add t0, t0, t1
- lw t2, 0(t0) t2totali
- gi
- la t0, g g
- lw t1, -28(fp) i
- sll t1, t1, 2 i4
- add t0, t0, t1
- lw t3, 0(t0) t3gi
- / Array example. /
- int g5
- 45, -3, 0, 16, -23
-
- int main()
-
- int i
- int total6
- / Get an array of running
- totals. /
- total0 0
- for (i 0 i lt 5 i)
-
- totali 1
- totali gi
-
27Arrays
- / Array example. /
- int g5
- 45, -3, 0, 16, -23
-
- int main()
-
- int i
- int total6
- / Get an array of running
- totals. /
- total0 0
- for (i 0 i lt 5 i)
-
- totali 1
- totali gi
-
- ... continued (part 3)
- totali 1 ...
- la t0, -24(fp) total
- lw t1, -28(fp) i
- add t1, t1, 1 i1
- sll t1, t1, 2 (i1)4
- add t0, t0, t1
- sw t2, 0(t0) ti1a0
- i
- lw t0, -28(fp) i
- add t0, t0, 1 i1
- sw t0, -28(fp) ii1
- Repeat loop.
- j loop
-
- continued ...
28Arrays
- / Array example. /
- int g5
- 45, -3, 0, 16, -23
-
- int main()
-
- int i
- int total6
- / Get an array of running
- totals. /
- total0 0
- for (i 0 i lt 5 i)
-
- totali 1
- totali gi
-
- ... continued (part 3)
- totali 1 ...
- la t0, -24(fp) total
- lw t1, -28(fp) i
- add t1, t1, 1 i1
- sll t1, t1, 2 (i1)4
- add t0, t0, t1
- sw t2, 0(t0) ti1t2
- i
- lw t0, -28(fp) i
- add t0, t0, 1 i1
- sw t0, -28(fp) ii1
- Repeat loop.
- j loop
-
- continued ...
29Arrays
- / Array example. /
- int g5
- 45, -3, 0, 16, -23
-
- int main()
-
- int i
- int total6
- / Get an array of running
- totals. /
- total0 0
- for (i 0 i lt 5 i)
-
- totali 1
- totali gi
-
- ... continued (part 4)
- end Print total5
- li v0, 1 print int
- Allowed arbitrary
- expression provided it
- is constant could have
- said -4(fp) too.
- lw a0, -2454(fp) 5
- syscall
-
- addu sp, sp, 28
- Exit
- li v0, 10 exit
- syscall
30Arrays
- / Array example. /
- int g5
- 45, -3, 0, 16, -23
-
- int main()
-
- int i
- int total6
- / Get an array of running
- totals. /
- total0 0
- for (i 0 i lt 5 i)
-
- totali 1
- totali gi
-
- ... continued (part 4)
- end Print total5
- li v0, 1 print int
- Allowed arbitrary
- expression provided it
- is constant could have
- said -4(fp) too.
- lw a0, -2454(fp) 5
- syscall
-
- addu sp, sp, 28
- Exit
- li v0, 10 exit
- syscall
31Pointers and arrays
- .data
- a .asciiz "Hello World\n"
- i .word 0
- c .space 1
- .text
- main calculate ai (size 1)
- loop la t0, a a
- lw t1, i i
- add t0, t0, t1
- lbu t2, 0(t0) ai
- sb t2, c store in c
- beq t2, 0, end '\0' 0
- putc(c)
- li v0, 11 print char
- lbu a0, c
- syscall
- / Print a string using
- array accesses. /
- char a "Hello world\n"
- int i 0
- char c
- int main()
-
- / get element of array,
- store in c, and stop
- if it is the end-of-
- string character. /
- while ((c ai) ! '\0')
-
- / Print it out. /
- putc(c)
- i
-
32Pointers and arrays
- .data
- a .asciiz "Hello World\n"
- i .word 0
- c .space 1
- .text
- main calculate ai (size 1)
- loop la t0, a a
- lw t1, i i
- add t0, t0, t1
- lbu t2, 0(t0) ai
- sb t2, c store in c
- beq t2, 0, end '\0' 0
- putc(c)
- li v0, 11 print char
- lbu a0, c
- syscall
- / Print a string using
- array accesses. /
- char a "Hello world\n"
- int i 0
- char c
- int main()
-
- / get element of array,
- store in c, and stop
- if it is the end-of-
- string character. /
- while ((c ai) ! '\0')
-
- / Print it out. /
- putc(c)
- i
-
33Pointers and arrays
- / Print a string using
- pointer dereferences. /
- char a "Hello world\n"
- int i 0
- char c
- int main()
-
- / get element of array,
- store in c, and stop
- if it is the end-of-
- string character. /
- while ((c (ai)) ! '\0')
-
- / Print it out. /
- putc(c)
- i
-
- .data
- a .asciiz "Hello World\n"
- i .word 0
- c .space 1
- .text
- main calculate (ai)
- loop la t0, a a
- lw t1, i i
- add t0, t0, t1 ai
- lbu t2, 0(t0) (ai)
- sb t2, c store in c
- beq t2, 0, end '\0' 0
- putc(c)
- li v0, 11 print char
- lbu a0, c
- syscall
34Pointers and arrays
- / Print a string using
- pointer dereferences. /
- char a "Hello world\n"
- int i 0
- char c
- int main()
-
- / get element of array,
- store in c, and stop
- if it is the end-of-
- string character. /
- while ((c (ai)) ! '\0')
-
- / Print it out. /
- putc(c)
- i
-
- .data
- a .asciiz "Hello World\n"
- i .word 0
- c .space 1
- .text
- main calculate (ai)
- loop la t0, a a
- lw t1, i i
- add t0, t0, t1 ai
- lbu t2, 0(t0) (ai)
- sb t2, c store in c
- beq t2, 0, end '\0' 0
- putc(c)
- li v0, 11 print char
- lbu a0, c
- syscall
35Pointers and arrays
- pointer version
- ...
- la t0, a a
- lw t1, i i
- add t0, t0, t1
- lbu t2, 0(t0) (ai)
- ...
- array version
- ...
- la t0, a a
- lw t1, i i
- add t0, t0, t1
- lbu t2, 0(t0) ai
- ...
code for pointer version is identical to code for
array version
This is why ai (ai) they compile to the
same code.
36Covered in this lecture
- Pointers
- implementation
- pointer operations
- dereference () operator
- address () operator
- Arrays
- implementation
- indices and addresses
- relationship to pointers
37Going further
- Two-dimensional arrays
- implement as array-of-arrays
- same technique as for 1-D arrays, applied twice
38Next time
- Functions
- calling
- returning
- local variables
- stack frames
Reading Lecture notes section B14