Computer Systems Fundamentals

add 17 and 25 and print result
#include <stdio.h>

int main(void) {
    int x = 17;
    int y = 25;
    printf("%d\n", x + y);

    return 0;
}
#include <stdio.h>

int main(void) {
    int x, y, z;
    x = 17;
    y = 25;
    z = x + y;
    printf("%d", z);
    printf("\n");
    return 0;
}
add 17 and 25 and print result
main:                  # x,y,z in $t0,$t1,$t2
    li   $t0, 17       # x = 17;

    li   $t1, 25       # y = 25;

    add  $t2, $t1, $t0 # z = x + y

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

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

    li   $v0, 0        # return 0
    jr   $ra
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;
        i++;
        printf("%d", i);
        printf("\n");
    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
read a number and print whther its odd or even
#include <stdio.h>

int main(void) {
    int x;

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

    if ((x & 1) == 0) {
        printf("Even\n");
    } else {
        printf("Odd\n");
    }

    return 0;
}
#include <stdio.h>

int main(void) {
    int x, v0;

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

    v0 = x & 1;
    if (v0 == 1) goto odd;
        printf("Even\n");
    goto end;
odd:
        printf("Odd\n");
end:
    return 0;
}
read a number and print whether its odd or even
main:
    la   $a0, string0    # printf("Enter a number: ");
    li   $v0, 4
    syscall

    li   $v0, 5          # scanf("%d", x);
    syscall

    and  $t0, $v0, 1     # if (x & 1 == 0) {
    beq  $t0, 1, odd

    la   $a0, string1    # printf("Even\n");
    li   $v0, 4
    syscall

    b    end

odd:                     # else
    la   $a0, string2    # printf("Odd\n");
    li   $v0, 4
    syscall

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

    .data
string0:
    .asciiz "Enter a number: "
string1:
    .asciiz "Even\n"
string2:
    .asciiz "Odd\n"
calculate 1*1 + 2*2 + ... + 99 * 99 + 100 * 100
#include <stdio.h>

int main(void) {
    int sum = 0;

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

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

    return 0;
}
#include <stdio.h>

// sum of first 100 squares.

int main(void) {
    int i, sum, square;

    sum = 0;
    i = 0;
    loop:
       if (i > 100) goto end;
       square = i * i;
       sum = sum + square;
       i = i + 1;
    goto loop;

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

    return 0;
}
calculate 1*1 + 2*2 + ... + 99 * 99 + 100 * 100
sum in $t0, i in $t1, square in $t2
main:
    li   $t0, 0         # sum = 0;
    li   $t1, 0         # i = 0
loop:
    bgt  $t1, 100, end  # if (i > 100) goto end;
    mul  $t2, $t1, $t1  # square = i * i;
    add  $t0, $t0, $t2  # sum = sum + square;
    addi $t1, $t1, 1    # i = i + 1;
    b    loop
end:
    move $a0, $t0       # printf("%d", sum);
    li   $v0, 1
    syscall
    li   $a0, '\n'      # printf("%c", '\n');
    li   $v0, 11
    syscall
    li   $v0, 0         # return 0
    jr   $ra