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

CSE1303 Computer Science
Semester 3 (summer), 2003
Part B
Lecture B12 notes: Making decisions

In this lecture

Listings

Slide 4

/* Don't ever write code like this! */

int main()
{
    printf("one");
    goto apple;
orange:
    printf("three");
    goto pomegranate;
apple:
    printf("two");
    goto orange;
pomegranate:
    printf("four");
    exit(0);
}

        .data
str1:   .asciiz "one"
str2:   .asciiz "two"
str3:   .asciiz "three"
str4:   .asciiz "four"

        .text
main:   li $v0, 4    # print one
        la $a0, str1
        syscall

        j apple

orange: li $v0, 4    # print three
        la $a0, str3
        syscall

        j pomegranate

apple:  li $v0, 4    # print two
        la $a0, str2
        syscall

        j orange

pomegranate:
        li $v0, 4    # print four
        li $a0, str4
        syscall

        li $v0, 10
        syscall

Slides 8-10

/* Sane people write
   C like this. */

int main()
{
    int i;

    scanf("%d", &i);

    if (i < 0)
    {
        printf("negative");
    }

    exit(0);
}
/* You could write it
   like this too (ugh). */

int main()
{
    int i;

    scanf("%d", &i);

    /* Skip if i not neg. */
    if (i >= 0) goto skip;

    printf("negative");

skip:
    exit(0);
}
        .data
str1:   .asciiz "negative"

        .text
main:   # Make one local var.
        move $fp, $sp
        subu $sp, $sp, 4

        # Read i.
        li $v0, 5    # read int
        syscall
        sw $v0, -4($fp)   # i

        # Comparison part:
        lw $t0, -4($fp)   # i
        # if (i >= 0) goto skip
        bge $t0, 0, skip
        # ... else fall through
        # to here.

        # Print out string.
        li $v0, 4     # print str
        la $a0, str1
        syscall

        # Join up here.
skip:   li $v0, 10     # Exit
        syscall

Slides 11-12

int main()
{
    int i;

    scanf("%d", &i);

    if (i < 0)
    {
        printf("negative");
    }
    else
    {
        printf("0/positive");
    }
    exit(0);
}
int main()
{
    int i;

    scanf("%d", &i);

    /* Branch based on cond. */
    if (i >= 0) goto false;

    /* Original cond true. */
    printf("negative");
    /* Skip false case. */
    goto endif;

false:
    /* Original cond false. */
    printf("0/positive");

endif:
    exit(0);
}
        .data
str1:   .asciiz "negative"
str2:   .asciiz "0/positive"

        .text
main:   # Make one local var.
        move $fp, $sp
        subu $sp, $sp, 4

        # Read i.
        li $v0, 5    # read int
        syscall
        sw $v0, -4($fp)   # i

        # Comparison part:
        lw $t0, -4($fp)   # i
        # if (i >= 0) goto false
        bge $t0, 0, false
        # ... else fall through.

        # Print out -ve string.
        li $v0, 4     # print str
        la $a0, str1
        syscall
        # Skip over false code.
        j endif

false:  # Print out +ve string.
        li $v0, 4     # print str
        la $a0, str2
        syscall

        # Join up here.
endif:   li $v0, 10     # Exit
        syscall

Slides 14-15

/* while loops written
   like this ... */

main()
{
    int n;
    int f = 1;

    scanf("%d", &n);

    /* Compute factorial */
    while (n > 0)
    {
        f = f * n;
        n--;
    }

    printf("%d", f);
}
/* ... could also be written
   like this. */

main()
{
    int n; int f = 1;
    scanf("%d", &n);

    /* Exit if while condition
       stops being true. */
loop:
    if (n <= 0) goto end;

    /* Body of loop. */ 
    f = f * n;
    n--;

    /* Repeat loop. */
    goto loop;

end:
    printf("%d", f);
}
        .text
main:   # Two local variables.
        move $fp, $sp
        subu $sp, $sp, 8

        # Initialization
        li $t0, 1
        sw $t0, -4($fp)  # f

        # Read n.
        li $v0, 5     # read int
        syscall
        sw $v0, -8($fp)  # n

        # Now comes the loop.

        # Exit loop if n <= 0.
loop:   lw $t0, -8($fp)
        ble $t0, 0, end

        # ... else fall through.

        # Body of while loop.
        # f = f * n
        lw $t0, -4($fp)  # f
        lw $t1, -8($fp)  # n
        mul $t0, $t0, $t1
        sw $t0, -4($fp)  # f

        # n-- (n = n - 1)
        lw $t0, -8($fp)  # n
        sub $t0, $t0, 1
        sw $t0, -8($fp)  # n

        # End of loop, go back
        j loop

end:    # Print result
        li $v0, 1    # print int
        lw $a0, -4($fp)  # f
        syscall
        li $v0, 10   # exit
        syscall

Slides 16-17

/* do-while loops written
   like this ... */

int v;

main()
{
    /* Keep prompting until user
       enters valid value. */
    do {
        printf("type num <= 31");
        scanf("%d", &v);
    } while (v > 31);

    /* Output 2 to power v. */
    printf("%d", 1 << v);
}
/* ... could also be written
   like this. */

int v;

main()
{
    /* Keep prompting until user
       enters valid value. */
loop:
    printf("type num <= 31");
    scanf("%d", &v);

    /* Repeat loop if condition
       is still satisfied. */
    if (v > 31) goto loop;
    /* End of loop. */

    /* Program continues. */
    printf("%d", 1 << v);
}
        .data
v:      .space 4
prom:   .asciiz "type num <= 31"

        .text
main:   # Begin loop.
loop:   # Print prompt
        li $v0, 4    # print str
        la $a0, str
        syscall

        # Read v.
        li $v0, 5    # read int
        syscall
        sw $v0, v

        # Repeat if v > 31
        lw $t0, v
        bgt $t0, 31, loop
        # Loop ends here.

        # Program continues.
        # Print 1 << v
        li $v0, 1    # print int
        lw $t0, 1
        li $t1, v
        sll $a0, $t0, $t1
        syscall

        li $v0, 10   # exit
        syscall

Slides 19-20

/* turn this for loop... */

main()
{
    /* An old drinking
       song, abbreviated. */
    int bottles;

    /* Count down bottles. */
    for (bottles = 10;
         bottles != 1;
         bottles--)
    {
         printf(
             "%d green bottles\n",
             bottles
         );
    }
    /* Special case, no plural. */
    printf("1 green bottle\n");
    /* End of song. */
    printf("no green bottles\n");
}
/* ... into this while loop. */

main()
{
    int bottles;

    bottles = 10;
    while (bottles != 1)
    {
        printf(
            "%d green bottles\n",
            bottles
        );
        bottles--;
    }

    /* Special case, no plural. */
    printf("1 green bottle\n");
    /* End of song. */
    printf("no green bottles\n");
}
        .data
botn:   .asciiz " green bottles\n"
bot1:   .asciiz "one green bottle\n"
bot0:   .asciiz "no green bottles\n"

main:   # One local var.
        move $fp, $sp
        subu $sp, $sp, 4

        # For loop: initialize.
        li $t0, 10
        sw $t0, -4($fp) # bottles

        # For loop: condition.
loop:   lw $t0, -4($fp) # bottles
        # End when bottles == 1.
        beq $t0, 1, end

        # For loop: body.
        li $v0, 1    # print int
        lw $a0, -4($fp) # bottles
        syscall

        li $v0, 4    # print str
        la $a0, botn
        syscall

        # For loop: increment.
        lw $t0, -4($fp) # bottles
        sub $t0, $t0, 1
        sw $t0, -4($fp) # bottles

        # Return to loop start.
        j loop

end:    # Program continues
        li $v0, 4    # print str
        la $a0, bot1
        syscall

        li $v0, 4    # print str
        la $a0, bot0
        syscall

        li $v0, 10   # exit
        syscall

[ Top | Home ]

Last modified 2002-12-05