Computer Systems Fundamentals

Course Resources

Administrivia: Course Outline | COMP1521 Handbook
Administrivia: Course Timetable | Help Sessions
Meet the Team: Our Team
Platforms: Lecture Recordings | Online Tut-Labs and Help Sessions (via BbCollaborate) | Course Forum
Style Guides: COMP1521 C Style Guide | Assembly Style Guide
MIPS Resources: MIPS Documentation | Text Editors for Assembly
mipsy: mipsy-web | mipsy source code | Debugging with mipsy (video)
Revision: Linux Cheatsheet | C Reference
Assessment: Autotests, Submissions, Marks | Give online: submission | Give online: sturec

Course Content Week-by-Week

Tutorial
Laboratory
Monday Week 1 Lecture Topics
Wednesday Week 1 Lecture Topics

Course Content Topic-by-Topic

Course Intro
Mips Basics

Print hello world in MIPS.
	.text
main:

	li	$v0, 4			# syscall 4: print_string
	la	$a0, hello_world_msg	#
	syscall				# printf("Hello world\n");


	li	$v0, 0
	jr	$ra			# return 0;

	.data

hello_world_msg:
	.asciiz	"Hello world\n"

Download hello_world.s


Perform some basic arithmetic.

#include <stdio.h>

int main(void) {
    int x = 17;
    int y = 25;

    printf("%d\n", 2 * (x + y));

    return 0;
}

Download math.c


Perform some basic arithmetic.

#include <stdio.h>

int main(void) {
    int x = 17;
    int y = 25;

    int z = x + y;
    z = 2 * z;

    printf("%d", z);
    putchar('\n');

    return 0;
}

Download math.simple.c


Do some basic arithmetic in MIPS.
main:
	# Locals:
	# - $t0: int x
	# - $t1: int y
	# - $t2: int z

	li	$t0, 17		# int x = 17;
	li	$t1, 25		# int y = 25;

	add	$t2, $t0, $t1	# int z = x + y;
	mul	$t2, $t2, 2	# z = z * 2;

	li	$v0, 1		# syscall 1: print_int
	move	$a0, $t2	#
	syscall			# printf("%d", z);

	li	$v0, 11		# syscall 11: print_char
	li	$a0, '\n'	#
	syscall			# putchar('\n');

	li	$v0, 0
	jr	$ra		# return 0;

Download math.s


Do some basic arithmetic in MIPS, but with one less register.

main:
	# Locals:
	# - $t0: int x
	# - $t1: int y

	li	$t0, 17		# int x = 17;
	li	$t1, 25		# int y = 25;

	li	$v0, 1		# syscall 1: print_int
	add	$a0, $t0, $t1	# (x + y)
	mul	$a0, $a0, 2	# * 2
	syscall			# printf("%d", 2 * (x + y));

	li	$v0, 11		# syscall 11: print_char
	li	$a0, '\n'	#
	syscall			# putchar('\n');

	li	$v0, 0
	jr	$ra		# return 0;

Download math.fewer_registers.s


Square two numbers and sum their squares.

#include <stdio.h>

int main(void) {
    int a, b;

    printf("Enter a number: ");
    scanf("%d", &a);

    printf("Enter another number: ");
    scanf("%d", &b);

    printf("The sum of the squares of %d and %d is %d\n", a, b, a*a + b*b);

    return 0;
}

Download square_and_add.c


Square two numbers and sum their squares.

#include <stdio.h>

int main(void) {
    int a, b;

    printf("Enter a number: ");
    scanf("%d", &a);

    printf("Enter another number: ");
    scanf("%d", &b);


    printf("The sum of the squares of ");
    printf("%d", a);
    printf(" and ");
    printf("%d", b);
    printf(" is ");

    a = a * a;
    b = b * b;
    printf("%d", a + b);
    putchar('\n');

    return 0;
}

Download square_and_add.simple.c


Square and add two numbers and print the result.

	.text
main:
	# Locals:
	# - $t0: int a
	# - $t1: int b

	li	$v0, 4			# syscall 4: print_string
	la	$a0, prompt1_msg	#
	syscall				# printf("Enter a number: ");

	li	$v0, 5			# syscall 5: read_int 
	syscall				#
	move	$t0, $v0		# scanf("%d", &a);

	li	$v0, 4			# syscall 4: print_string
	la	$a0, prompt2_msg	#
	syscall				# printf("Enter another number: ");

	li	$v0, 5			# syscall 5: read_int
	syscall				#
	move	$t1, $v0		# scanf("%d", &b);


	li	$v0, 4			# syscall 4: print_string
	la	$a0, result_msg_1	# 
	syscall				# printf("The sum of the squares of ");

	li	$v0, 1			# syscall 1: print_int
	move	$a0, $t0		#
	syscall				# printf("%d", a);

	li	$v0, 4			# syscall 4: print_string
	la	$a0, result_msg_2	# 
	syscall				# printf(" and ");

	li	$v0, 1			# syscall 1: print_int
	move	$a0, $t1		#
	syscall				# printf("%d", b);

	li	$v0, 4			# syscall 4: print_string
	la	$a0, result_msg_3	#
	syscall				# printf(" is ");

	mul	$t0, $t0, $t0		# a = a * a;
	mul	$t1, $t1, $t1		# b = b * b;

	li	$v0, 1			# syscall 1: print_int
	add	$a0, $t0, $t1		# 
	syscall				# printf("%d", a + b);

	li	$v0, 11			# syscall 11: print_char
	la	$a0, '\n'		# 
	syscall				# putchar('\n');


	li	$v0, 0
	jr	$ra			# return 0;

	.data
prompt1_msg:
	.asciiz	"Enter a number: "
prompt2_msg:
	.asciiz	"Enter another number: "
result_msg_1:
	.asciiz	"The sum of the squares of "
result_msg_2:
	.asciiz	" and "
result_msg_3:
	.asciiz	" is "

Download square_and_add.s

Mips Control

Print a message only if a number is even.

#include <stdio.h>

int main(void) {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);

    if (n % 2 == 0) {
        printf("even\n");
    }

    return 0;
}

Download print_if_even.c


Print a message only if a number is even.

#include <stdio.h>

int main(void) {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);

    if (n % 2 != 0) goto epilogue;
        printf("even\n");

epilogue:
    return 0;
}

Download print_if_even.simple.c


Calculate 1*1 + 2*2 + ... + 99*99 + 100*100

#include <stdio.h>

int main(void) {
    int sum = 0;

    for (int i = 1; i <= 100; i++) {
        sum += i * i;
    }

    printf("%d\n", sum);

    return 0;
}

Download sum_100_squares.c


Calculate 1*1 + 2*2 + ... + 99*99 + 100*100.

#define UPPER_BOUND 100
#include <stdio.h>

int main(void) {
    int sum = 0;

loop_i_to_100__init:;
    int i = 0;
loop_i_to_100__cond:
    if (i > UPPER_BOUND) goto loop_i_to_100__end;
loop_i_to_100__body:
    sum += i * i;
loop_i_to_100__step:
    i++;
    goto loop_i_to_100__cond;
loop_i_to_100__end:

    printf("%d", sum);
    putchar('\n');

    return 0;
}

Download sum_100_squares.simple.c


Calculate 1*1 + 2*2 + ... + 99*99 + 100*100

UPPER_BOUND = 100


	.text
main:
	# Locals:
	# - $t0: int sum
	# - $t1: int i
	# - $t2: temporary value

	li	$t0, 0					# int sum = 0;

loop_i_to_100__init:
	li	$t1, 1					# int i = 0;
loop_i_to_100__cond:
	bgt	$t1, UPPER_BOUND, loop_i_to_100__end	# while (i < UPPER_BOUND) {
loop_i_to_100__body:
	mul	$t2, $t1, $t1				#   sum = (i * i) +
	add	$t0, $t0, $t2				#         sum;
loop_i_to_100__step:
	addi	$t0, $t0, 1				#   i++;
	b	loop_i_to_100__cond			# }

loop_i_to_100__end:
	li	$v0, 1					# syscall 1: print_int
	move	$a0, $t0				# 
	syscall						# printf("%d", sum);

	li	$v0, 11					# syscall 11: print_char
	li	$a0, '\n'				#
	syscall						# putchar('\n');

	li	$v0, 0
	jr	$ra					# return 0;

Download sum_100_squares.s