Computer Systems Fundamentals

print a string in C
#include <stdio.h>

int main(void) {
    printf("I love MIPS\n");

    return 0;
}
print a string in MIPS assembly
main:
    # ... pass address of string as argument
    la  $a0, string
    # ... 4 is printf "%s" syscall number
    li  $v0, 4
    syscall
    li  $v0, 0      # return 0
    jr  $ra

    .data
string:
    .asciiz "I love MIPS\n"
main:
   lw    $t0, x         # $t0 = x
   addi  $t0, $t0, 4    # $t0 = x + 4
   li    $t1, 2         # $t1 = 2
   mul   $t0, $t0, $t1  # $t0 = (x+4) * 2
   sw    $t0, y         #   y = (x+4) * 2
   
   li    $v0, 0         # return 0
   jr    $ra

   .data
x: .word  3   # int x = 3;
y: .space 4   # int y;

A simple example that prints out an int
main:
    li      $v0, 1       # printf("%d",42);
    li      $a0, 42
    syscall

    li      $v0, 0       # set return value for main to be 0
    jr      $ra          # return from main to (__start)


main:
#int main(void) {
#   int x = 3; //local variable
#   printf("%d\n", x+5);
#   return 0;
#}
    li $t0, 3           # int x = 3;
    addi $t1, $t0, 5    # $t1 = $t0 + 5
                        #     =   x + 5 
    
    li $v0, 1           # syscall 1 printf("%d");
    move $a0, $t1       # $a0 = $t1
    syscall
    
    li      $v0, 11	# printf("%c", '\n');
    li      $a0, '\n'
    syscall          
                        
    li $v0, 0           # return 0
    jr $ra

main:
#int x = 3; //global variable
#int main(void) {
#   
#   printf("%d\n", x+5);
#   return 0;
#}
    lw $t0, x           # $t0 = x
    addi $t1, $t0, 5    # $t1 = $t0 + 5
                        #     =   x + 5 
     
    li $v0, 1           # syscall 1 printf("%d");
    move $a0, $t1       # $a0 = $t1
    syscall
    
    li      $v0, 11	# printf("%c", '\n');
    li      $a0, '\n'
    syscall          
    
    li $v0, 0           # return 0
    jr $ra
    
    .data
x:  .word 3   #int x = 3; #global variable    
              #.word means 4 byte value
main:
#int main(void) {
#   int x, y;
#   printf("First number: ");
#   scanf("%d", &x);
#   printf("Second number: ");
#   scanf("%d", &y);
#   printf("%d\n", x+y);
#   return 0;
#}
    li $v0, 4	             #printf("First number: ");
    la $a0, prompt1
    syscall
    
    li $v0, 5		     # scanf("%d", &x);
    syscall
    move $t0, $v0            # $t0, x
    
    li $v0, 4	             #printf("Second number: ");
    la $a0, prompt2
    syscall
    
    li $v0, 5		     # scanf("%d", &y);
    syscall
    move $t1, $v0            # $t1, y
    
    add $t2, $t0, $t1        # $t2 = x + y
    
    li $v0, 1                #printf("%d);
    move $a0, $t2
    syscall
    
    li      $v0, 11	     # printf("%c", '\n');
    li      $a0, '\n'
    syscall   
    
    li $v0, 0
    jr $ra
    
    .data
prompt1:
    .asciiz "First number: "  #char *prompt1 = "First number: "
prompt2:
    .asciiz "Second number: "
main:
#int main(void) {
#   int x, y;
#   printf("First number: ");
#   scanf("%d", &x);
#   printf("Second number: ");
#   scanf("%d", &y);
#   printf("%d\n", (x+y)/2);
#   return 0;
#}
    li $v0, 4	             #printf("First number: ");
    la $a0, prompt1
    syscall
    
    li $v0, 5		     # scanf("%d", &x);
    syscall
    move $t0, $v0            # $t0, x
    
    li $v0, 4	             #printf("Second number: ");
    la $a0, prompt2
    syscall
    
    li $v0, 5		     # scanf("%d", &y);
    syscall
    move $t1, $v0            # $t1, y
    
    add $t2, $t0, $t1        # $t2 = x + y
    div $t2, $t2, 2          # $t2 = $t2/2
    
    li $v0, 1                #printf("%d);
    move $a0, $t2
    syscall
    
    li      $v0, 11	     # printf("%c", '\n');
    li      $a0, '\n'
    syscall   
    
    li $v0, 0
    jr $ra
    
    .data
prompt1:
    .asciiz "First number: "  #char *prompt1 = "First number: "
prompt2:
    .asciiz "Second number: "
main:
#int main(void) {
#   unsigned int x = 42;
#   x = x >> 1;
#   printf("%d\n",x);
#   x = x << 2;
#   printf("%d\n",x);
#   return 0;
#}
    li $t0, 42
    srl $t0, $t0, 1    # x >> 1
    
    li $v0, 1          #printf("%d)
    move $a0, $t0
    syscall
     
    li   $v0, 11       # syscall 11 printf("%c",'\n');
    li   $a0, '\n'
    syscall
    
    sll $t0, $t0, 2    # x << 2
     
    li $v0, 1          #printf("%d)
    move $a0, $t0
    syscall
     
    li   $v0, 11       # syscall 11 printf("%c",'\n');
    li   $a0, '\n'
    syscall

    jr $ra
add 3, 17 and 25 then print the result
#include <stdio.h>

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

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

    return 0;
}
add 3, 17 and 25 then print the result
#include <stdio.h>

int main(void) {
    int w, x, y, z;

    w = 3;
    x = 17;
    y = 25;

    z = w + x;
    z = z + y;

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

    return 0;
}
add 3, 17 and 25 then print the result
main:
    # w in $t0, x in $t0 
    # y in $t2, z in $t3

    li   $t0, 3        # w = 3;
    li   $t1, 17       # x = 17;
    li   $t2, 25       # y = 25;

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

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

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

    li   $v0, 0        # return 0
    jr   $ra