Computer Systems Fundamentals


A simple demonstration of goto in C.

#include <stdio.h>

int main(void) {

    printf("In this essay I will\n");

    goto signature;

    printf("tell you why I deserve\n");
    printf("a 100 on this assignment.\n");
    printf("\n");

    printf("I have been working very\n");
    printf("hard on this assignment\n");
    printf("and I think I deserve a 100.\n");
    printf("\n");
    
    printf("I have been working on this\n");
    printf("assignment for a long time\n");
    printf("and I think I deserve a 100.\n");
    printf("\n");
    
    printf("It is my humble opinion\n");
    printf("that I deserve a 100 on\n");
    printf("this assignment.\n");
    printf("\n");

signature:
    printf("Kind regards,\n");
    printf("Abiram Nadarajah\n");
    printf("COMP1521 20T2 student\n");

    return 0; 
}

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;
}

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;
}

Print a message only if a number is even.

	.text
main:
	# Locals:
	# - $t0: int n
	# - $t1: n % 2

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

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

	rem	$t1, $t0, 2		# if ((n % 2)
	bnez	$t1, epilogue	#     != 0) goto epilogue;

	li	$v0, 4			# syscall 4: print_string
	la	$a0, even_msg		#
	syscall				# printf("even\n");

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

	.data
prompt_msg:
	.asciiz	"Enter a number: "
even_msg:
	.asciiz	"even\n"

Print out whether a value is odd or even.


#include <stdio.h>

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

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

    return 0;
}

Print out whether a value is odd or even.

#include <stdio.h>

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

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

n_mod_2_ne_0:
    printf("odd\n");

epilogue:
    return 0;
}

Print out whether a value is odd or even.

	.text
main:
	# Locals:
	# - $t0: int n
	# - $t1: n % 2

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

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

	rem	$t1, $t0, 2		# if ((n % 2)
	bnez	$t1, n_mod_2_ne_0	#     != 0) goto n_mod_2_ne_0;

	li	$v0, 4			# syscall 4: print_string
	la	$a0, even_msg		#
	syscall				# printf("even\n");

	b	epilogue		# goto epilogue;

n_mod_2_ne_0:
	li	$v0, 4			# syscall 4: print_string
	la	$a0, odd_msg		#
	syscall				# printf("odd\n");

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

	.data
prompt_msg:
	.asciiz	"Enter a number: "
even_msg:
	.asciiz	"even\n"
odd_msg:
	.asciiz	"odd\n"
print integers 1..10 one per line
#include <stdio.h>

int main(void) {
    for (int i = 1; i <= 10; i++) {
        printf("%d\n", i);
    }
    return 0;
}
#include <stdio.h>

int main(void) {
    int i;
    i = 1;
loop:
    if (i > 10) goto end;
        printf("%d", i);
        printf("\n");
        i++;
    goto loop;
end:
    return 0;
}
print integers 1..10 one per line
main:                 # int main(void) {
                      # int i;  // in register $t0

    li    $t0, 1      # i = 1;

loop:                 # loop:
    bgt  $t0, 10, end # if (i > 10) goto end;

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

    li   $a0, '\n'    #   printf("%c", '\n');
    li   $v0, 11
    syscall

    addi $t0, $t0, 1  #   i++;

    b    loop         # goto loop;

end:
    li   $v0, 0       # return 0
    jr   $ra

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;
}

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;
}

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;

Count from 1 to 10 with a loop.

#include <stdio.h>

int main(void) {
    for (int i = 1; i <= 10; i++) {
        printf("%d\n", i);
    }
    return 0;
}

Count from 1 to 10 with a loop.

#include <stdio.h>

int main(void) {

loop_i_to_10__init:;
    int i = 1;
loop_i_to_10__cond:
    if (i > 10) goto loop_i_to_10__end;

loop_i_to_10__body:
    printf("%d", i);
    putchar('\n');
loop_i_to_10__step:
    i++;                        // i = i + 1;
    goto loop_i_to_10__cond;
   
loop_i_to_10__end:
    return 0;
}

Count from 1 to 10 with a loop.

	.text
main:
	# Locals:
	# - $t0: int i

loop_i_to_10__init:
	li	$t0, 1				# int i = 1;
loop_i_to_10__cond:
	bge	$t0, 10, loop_i_to_10__end	# if (i > 10) goto loop_i_to_10__end;

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

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

loop_i_to_10__step:
	addi	$t0, $t0, 1			# i = i + 1;
	b	loop_i_to_10__cond

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

Do while example
#include <stdio.h>

int main(void) {
    int val;
    do {
        printf("val? ");
        scanf("%d", &val);
        printf("%d",val);
        printf("\n");
    } while (val > 0);
}
read/write characters until the user types a '!'
val is represented by $t0
    .text
main:

loop_start:		# do {

    la   $a0, prompt	#   printf("val? ");
    li   $v0, 4
    syscall

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

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

    li   $a0, '\n'	#    printf("\n");
    li   $v0, 11
    syscall

    blt  $t0,1,loop_end # } while (val > 0);
    b    loop_start
loop_end:

    li   $v0, 0		# return 0
    jr   $ra

# read 10 numbers into an array then print the 10 numbers

    .data

prompt:
    .asciiz "val? "