Download LC-3 Code for Array References and Passing Arrays in C and more Study notes Programming Languages in PDF only on Docsity!
CS 350
Computer Organization & Assembly
Language Programming
Spring 2009
Notes 22, 2009–04–
1
Chapter 16
Pointers & Arrays
(continued)
LC-3 Code for Array
References
; x = grid[3] + 1 ADD R0, R5, #-9 ; R0 = &grid[0] LDR R1, R0, #3 ; R1 = grid[3] ADD R1, R1, #1 ; plus 1 STR R1, R5, #-10 ; x = R ; grid[6] = 5; AND R0, R0, # ADD R0, R0, #5 ; R0 = 5 ADD R1, R5, #-9 ; R1 = &grid[0] STR R0, R1, #6 ; grid[6] = R
x
grid[0]
grid[1]
grid[2]
grid[3]
grid[4]
grid[5]
grid[6]
grid[7]
grid[8]
R5^ grid[9]
3
In symbol table,
grid offset = -
x offset = -
More LC-3 Code
; Calculate grid[x] + 2 LDR R0, R5, #-10 ; R0 = x ADD R1, R5, #-9 ; R1 = &grid[0] ADD R1, R0, R1 ; R1 = &grid[x] LDR R2, R1, #0 ; R2 = grid[x] ADD R2, R2, #2 ; add 2 ; Assign grid[x+1] = grid[x] + 2 LDR R0, R5, #-10 ; R0 = x ADD R0, R0, #1 ; R0 = x+ ADD R1, R5, #-9 ; R1 = &grid[0] ADD R1, R0, R1 ; R1 = &grid[x+1] STR R2, R1, #0 ; grid[x+1] = R
x
grid[0]
grid[1]
grid[2]
grid[3]
grid[4]
grid[5]
grid[6]
grid[7]
grid[8]
R5^ grid[9]
Array-Passing Example
int f(int b[]) { b[0] = 1; return 0; } int main() { int a[4]; a[0] = 17; /* Note a[1], a[2], a[3] undefined / f(a); printf("%d\n", a[0]); / prints 1 */ } 7
Parameter Array Length?
• How does^ f^ know how long^ b^ is?
int f(int b[]) {
b[0] = 1;
return 0;
• It doesn't!^ You can include it, but the
compiler doesn't check for bad indexes ....
int f(int b[4]) { b[99]=2; }
Pass the Array Length
• For the called function to know how long
an array is, you have to pass the length as
a parameter.
int g(int b[], int n, int i) {
if (0 <= i && i < n)
b[i] = something;
9
Another Length-Passing
Example
/* zero_out(a,n) sets a[0], a[1], ... a[n-1] to 0 */ int zero_out(int a[], int n) { int i = 0; for ( ; i < n ; i++) a[i] = 0; return 0; } int main() { int x[5]; zero_out(x, 5); }
Passing Fake Array
/* zero_out(a,n) sets a[0], a[1], ... a[n-1] to 0 / int zero_out(int a[], int n) { int i = 0; for ( ; i < n ; i++) a[i] = 0; return 0; } int main() { int b[20]; zero_out(&b[5], 8); / Zero b[5], ... b[12] */ } 13
Common Pitfalls with Arrays
in C
• Overrun array limits
- There is no checking at run-time (and at most
little checking at compile-time) to see whether
reference is within array bounds.
int array[10]; int i; for (i = 0; i <= 10; i++) array[i] = 0;
Common Pitfalls with Arrays
in C
• Declaration with variable size
- Size of array must be known at compile time.
- Not allowed:
void SomeFunction(int num_elements) { int temp[num_elements]; … } 15
C Strings
• The C library string-manipulating/printing
code expects strings to have the form
- Sequence of^ ASCII chars (1 char/byte)
- Followed by byte = 0.
• One way to create a string is with a
character array declaration:
char str[3]; str[0] = 'H'; str[1] = 'i'; str[2] = 0x0; printf("%s\n", str);
String Initialization vs Char
Array Initialization
• Initializing a character array with a string is
syntactic sugar for an equivalent (longer)
char[ ] initialization.
char s1[] = "Hi"; char s2[] = {'H', 'i', 0}; char s3[] = {72, 105, 0}; printf("%s %s %s\n", s1, s2, s3); 19
I/O with Strings
• Printf^ and^ scanf^ use^ %s^ format for
string input/output
• Printf^ prints chars (not terminating 0)
printf("%s", outputString);
• Scanf^ reads chars until whitespace,
stores chars in string, adds terminating 0.
scanf("%s", inputString);
String I/O Example
int main() { char str[] = "abc\nefg"; /* length 8 / printf("str is '%s'\n", str); printf("Enter string (& hit return): "); scanf("%s", str); / Not &str */ printf("str is '%s'\n", str); printf("str[6] = %c\n", str[6]); printf("str[4..7] = %s\n", &str[4]); } 21
If you type in 3 chars as input, then
the last 8-(3+1) = 4 chars of str
aren't changed and get reprinted
Relationship between Arrays
and Pointers
• An array name is essentially a pointer to
the first element in the array
char word[10]; char cptr; cptr = word; / points to word[0] */
• Array names are constants, not variables
- OK:^ cptr^ =^ cptr^ +^ 1;
- Not OK:^ word^ =^ word^ +^ 1;
Addresses of Array Elements
• If^ a^ is an array of elements of type^ T , and
it takes k words to store an element of
type T , then
- a[0]^ refers to a location word L
- a[1]^ refers to location L+ k
- a[2]^ refers to location L+2 k
- a[i]^ refers to location L^ + i* k
25
Pointer Arithmetic
• If^ p^ is a pointer to values of type^ T , and it
takes k words to store an elt of type T
- p^ points to a location L
- p+1^ points to location L+ k
- p+2^ points to location L+2 k
- p+i^ points to location L+i k
Array Indexing vs Pointer
Arithmetic
• In C, the notation^ a[e]^ means^ *(a+e)
- E.g.^ a[2]=5;^ is the same as^ *(a+2)=5;
- You can use the notations interchangeably
for (i=0; i<n; i++) a[i]=0; for (i=0; i<n; i++) *(a+i)=0;
- Pointer variables can be changed but array
names can't
for (i=0; i<n; i++) {*p=0; p++;}