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

CSE1303 Computer Science
Semester 3 (summer), 2003
Part B
Lecture B10 notes: MIPS programming

In this lecture

Listings

Slide 18

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

/* For storing user input. */
int val;

int main()
{
  /* Read an int. */
  scanf("%d", &val);

  /* Print it out squared. */
  printf("%d", val * val);

  exit(0);
}
        .data
        # User input.
val:    .space 4

        .text
main:   # Syscall 5: read int
        li $v0, 5
        syscall
        # Number is in $v0
        sw $v0, val

        # Syscall 1: print int
        li $v0, 1
        lw $t0, val
        # $a0 is what to print
        mul $a0, $t0, $t0
        syscall

        # Syscall 10: exit program
        li $v0, 10
        syscall

Slide 20

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

/* Prompt and buffer. */
char prom[] = "Type a string: ";
char buf[20];

int main()
{
  /* Print prompt */
  fputs(prom, stdout);

  /* Read a string. */
  fgets(buf, 20, stdin);

  /* Print it back out. */
  fputs(buf, stdout);

  exit(0);
}
        .data
prom:   .asciiz "Type a string: "
buf:    .space 20
        .text
main:   # Syscall 4: print str
        li $v0, 4
        # $a0: address of string
        la $a0, prom
        syscall

        # syscall 8: read string
        li $v0, 8
        la $a0, buf
        # $a1: max length of str
        li $a1, 20
        syscall

        li $v0, 4
        la $a0, buf
        syscall

        li $v0, 10  # syscall 10
        syscall

[ Top | Home ]

Last modified 2003-02-13