Mips Assembly
Mips Assembly
# Everything that occurs after a '#' will be ignored by the assembler's lexer.
# Datatype sizes
_byte: .byte 'a' # 1 byte
_halfword: .half 53 # 2 bytes
_word: .word 3 # 4 bytes
_float: .float 3.14 # 4 bytes
_double: .double 7.0 # 8 bytes
# Bitwise Shifting
sll $t0, $t0, 2 # Bitwise shift to the left with
# immediate (constant value) of 2
sllv $t0, $t1, $t2 # Shift left by a variable amount in
# register
srl $t0, $t0, 5 # Bitwise shift to the right (does
# not sign preserve, sign-extends with
0)
srlv $t0, $t1, $t2 # Shift right by a variable amount in
# a register
sra $t0, $t0, 7 # Bitwise arithmetic shift to the right
# (preserves sign)
srav $t0, $t1, $t2 # Shift right by a variable amount
# in a register
# Bitwise operators
and $t0, $t1, $t2 # Bitwise AND
andi $t0, $t1, 0xFFF # Bitwise AND with immediate
or $t0, $t1, $t2 # Bitwise OR
ori $t0, $t1, 0xFFF # Bitwise OR with immediate
xor $t0, $t1, $t2 # Bitwise XOR
xori $t0, $t1, 0xFFF # Bitwise XOR with immediate
nor $t0, $t1, $t2 # Bitwise NOR
## BRANCHING ##
_branching:
# The basic format of these branching instructions typically follow <instr>
# <reg1> <reg2> <label> where label is the label we want to jump to if the
# given conditional evaluates to true
# Sometimes it is easier to write the conditional logic backwards, as seen
# in the simple if statement example below
# Simple if statement
# if (i == j)
# f = g + h;
# f = f - i;
L1:
sub $s0, $s0, $s3 # f = f - i
max_C:
move $v0, $s2 # max = c
## LOOPS ##
_loops:
# The basic structure of loops is having an exit condition and a jump
instruction to continue its execution
li $t0, 0
while:
bgt $t0, 10, end_while # While $t0 is less than 10, keep
iterating
addi $t0, $t0, 1 # Increment the value
j while # Jump back to the beginning of the
loop
end_while:
# 2D Matrix Traversal
# Assume that $a0 stores the address of an integer matrix which is 3 x 3
li $t0, 0 # Counter for i
li $t1, 0 # Counter for j
matrix_row:
bgt $t0, 3, matrix_row_end
matrix_col:
bgt $t1, 3, matrix_col_end
# Do stuff
# Do stuff
## FUNCTIONS ##
_functions:
# Functions are callable procedures that can accept arguments and return
values all denoted with labels, like above
li $v0, 10
syscall
fact_done:
lw $s0, ($sp)
lw $ra, ($sp) # Restore the PC
addi $sp, $sp, 8
jr $ra
## MACROS ##
_macros:
# Macros are extremly useful for substituting repeated code blocks with a
# single label for better readability
# These are in no means substitutes for functions
# These must be declared before it is used
# Macro for printing new lines (since these can be very repetitive)
.macro println()
la $a0, newline # New line string stored here
li $v0, 4
syscall
.end_macro
println() # Assembler will copy that block of
# code here before running
li $t0, 1
print_int($t0)
immediates(3, 5)
print(hello_world)
## ARRAYS ##
.data
list: .word 3, 0, 1, 2, 6 # This is an array of words
char_arr: .asciiz "hello" # This is a char array
buffer: .space 128 # Allocates a block in memory, does
# not automatically clear
# These blocks of memory are aligned
# next each other
.text
la $s0, list # Load address of list
li $t0, 0 # Counter
li $t1, 5 # Length of the list
loop:
bgt $t0, $t1, end_loop
lw $a0, ($s0)
li $v0, 1
syscall # Print the number
## INCLUDE ##
# You do this to import external files into your program (behind the scenes,
# it really just takes whatever code that is in that file and places it where
# the include statement is)
.include "somefile.asm"