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

CSE1303 Computer Science
Semester 2, 2003
Part B
Lecture B11 notes: Working with memory

In this lecture

Listings

Slides 29-33

#include <stdio.h>
#include <stdlib.h>

/* A global variable. */
int g = 123;

int main()
{
    /* Three local variables. */
    int a = -5;
    int b;
    int c = 0x12345678;

    /* Do some arithmetic. */
    b = g + a;

    /* Do some more arithmetic. */
    printf("%d", c - a);

    exit(0);
}
        .data
# g is global, allocate
# in data segment
g:      .word 123

        .text
main:   # Copy $sp into $fp.
        move $fp, $sp

        # Allocate 12 bytes of
        # local variables.
        subu $sp, $sp, 12

        # Initalize local
        # variables.

        li $t0, -5          # a
        sw $t0, -12($fp)

        li $t0, 0x12345678  # c
        sw $t0, -4($fp)

        # Calculate g + a.
        lw $t0, g
        lw $t1, -12($fp)    # a
        add $t0, $t0, $t1
        # Store in b.
        sw $t0, -8($fp)     # b

        li $v0, 1    # Print int.
        # Calculate c - a.
        lw $t0, -4($fp)     # c
        lw $t1, -12($fp)    # a
        sub $a0, $t0, $t1
        syscall      # Do print.

        # Now exit.
        li $v0, 10   # Exit.
        syscall

        # If function returned now
        # it would need to destroy
        # local variables with:
        # addu $sp, $sp, 12


[ Top | Home ]

Last modified 2002-12-05