Monash University > School of Computer Science and Software Engineering > CSE1303 > Part B > Lectures > Lecture B13 notes

CSE1303 Computer Science
Semester 2, 2003
Part B
Lecture B13 notes: Pointers and arrays

In this lecture

Listings

Slides 7-14

/* Example of pointer
   operations. */

char newline = '\n';

int main()
{
    int num = 42;
    int *iptr;
    char *cptr;

    /* Get addresses
       of vars. */
    cptr = &newline;
    iptr = #

    /* This doesn use
       pointers (yet): */

    /* Print num then newline. */
    printf("%d", num);
    putc(newline);

    /* Change num to 87
       through iptr. */
    *iptr = 87;

    /* Print num through iptr. */
    printf("%d", *iptr);

    /* Print newline
       through cptr. */
    putc(*cptr);

    exit(0);
}
        .data
newline: # allocate one char.
        .byte '\n'

        .text
main:   # 12 bytes of locals.
        move $fp, $sp
        subu $sp, $sp, 12

        # Initialize num = 42.
        li $v0, 42
        sw $v0, -12($fp)  # num

        # cptr = &newline;
        la $t0, newline
        sw $t0, -4($fp)  # cptr

        # iptr = #
        la $t0, -12($fp)  # num
        sw $t0, -8($fp)  # iptr

        # printf("%d", num);
        li $v0, 1   # print int
        lw $a0, -12($fp)  # num
        syscall

        # Syscall 11 prints a
        # character (in $a0).

        # putc(newline);
        li $v0, 11  # print char
        lbu $a0, newline
        syscall

        # *iptr = 87;
        li $t0, 87
        lw $t1, -8($fp)  # iptr
        sw $t0, 0($t1)  # *iptr

        # printf("%d", *iptr);
        li $v0, 1   # print int
        lw $t0, -8($fp)  # iptr
        lw $a0, 0($t0)  # *iptr
        syscall

        # putc(*cptr);
        li $v0, 11  # print char
        lw $t0, -4($fp)  # cptr
        lbu $a0, 0($t0)  # *cptr
        syscall

        li $v0, 10  # exit
        syscall

Slides 23-31

/* Array example. */

int g[5] = { 
    45, -3, 0, 16, -23
};

int main()
{
    int i;
    int total[6];

    /* Get an array of running
       totals. */
    total[0] = 0;
    for (i = 0; i < 5; i++)
    {
        total[i + 1] =
            total[i] + g[i];
    }

    /* Print final total. */
    printf("%d", total[5]);
}
        .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

        # total[0] = 0;
        la $t0, -24($fp) # total
        sw $0, 0($t0)

        # i = 0;
        sw $0, -28($fp)  # i

        # i < 5
loop:   lw $t0, -28($fp)  # i
        bge $t0, 5, end

        # shift left 2 used here
        # to scale by sizeof(int)

        # total[i]
        la $t0, -24($fp)  # total
        lw $t1, -28($fp)  # i
        sll $t1, $t1, 2   # * 4
        add $t0, $t0, $t1
        lw $t2, 0($t0)

        # g[i]
        la $t0, g         # g
        lw $t1, -28($fp)  # i
        sll $t1, $t1, 2   # * 4
        add $t0, $t0, $t1
        lw $t3, 0($t0)

        # Add two array elements.
        add $t2, $t2, $t3

        # total[i + 1] = ...
        la $t0, -24($fp)  # total
        lw $t1, -28($fp)  # i
        add $t1, $t1, 1   # + 1
        sll $t1, $t1, 2   # * 4
        add $t0, $t0, $t1
        sw $t2, 0($t0)

        # i++
        lw $t0, -28($fp)  # i
        add $t0, $t0, 1
        sw $t0, -28($fp)  # i

        # Repeat loop.
        j loop

end:    # Print total[5]
        li $v0, 1   # print int
        # Allowed arbitrary
        # expression provided it
        # is constant; could have
        # said -4($fp) too.
        lw $a0, -24+5*4($fp) # [5]
        syscall
        
        # Exit
        li $v0, 10  # exit
        syscall

Slides 32-33

/* 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 = a[i]) != '\0')
    {
        /* Print it out. */
        putc(c);
        i++;
    }
}
        .data
a:      .asciiz "Hello World\n"
i:      .word 0
c:      .space 1

        .text
main:   # calculate a[i] (size = 1)
loop:   la $t0, a        # a
        lw $t1, i        # i
        add $t0, $t0, $t1
        lbu $t2, 0($t0)  # a[i]
        sb $t2, c        # store in c
        beq $t2, 0, end  # '\0' == 0

        # putc(c);
        li $v0, 11   # print char
        lbu $a0, c
        syscall

        # i++;
        lw $t0, i
        add $t0, $t0, 1
        sw $t0, i
        j loop       # repeat loop

end:    li $v0, 10
        syscall

Slides 34-35

/* 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 = *(a+i)) != '\0')
    {
        /* Print it out. */
        putc(c);
        i++;
    }
}
        .data
a:      .asciiz "Hello World\n"
i:      .word 0
c:      .space 1

        .text
main:   # calculate *(a+i)
loop:   la $t0, a        # a
        lw $t1, i        # i
        add $t0, $t0, $t1 # +
        lbu $t2, 0($t0)  # *(a+i)
        sb $t2, c        # store in c
        beq $t2, 0, end  # '\0' == 0

        # putc(c);
        li $v0, 11   # print char
        lbu $a0, c
        syscall

        # i++;
        lw $t0, i
        add $t0, $t0, 1
        sw $t0, i
        j loop       # repeat loop

end:    li $v0, 10
        syscall

[ Top | Home ]

Last modified 2002-12-05