diff options
| author | Spiderpig86 <slim679975@gmail.com> | 2018-08-06 23:34:56 -0400 | 
|---|---|---|
| committer | Spiderpig86 <slim679975@gmail.com> | 2018-08-06 23:34:56 -0400 | 
| commit | 9d908ebb4a0b1dd52084333581c3021b8916eace (patch) | |
| tree | 52e044275b731271fbc1d58af3bf7df324181fe9 | |
| parent | e6961a69421b3e93bff949ad8731c3eb8045608f (diff) | |
feat(mips.html.markdown): Added entry on functions
| -rw-r--r-- | mips.html.markdown | 64 | 
1 files changed, 62 insertions, 2 deletions
| diff --git a/mips.html.markdown b/mips.html.markdown index 27a11e2f..9b176842 100644 --- a/mips.html.markdown +++ b/mips.html.markdown @@ -200,7 +200,8 @@ hello_world .asciiz "Hello World\n"         # Declare a null terminated string  ## LOOPS ##    _loops: -    # The basic structure of loops is having an exit condition and a jump instruction to continue its execution +    # 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 @@ -220,7 +221,7 @@ hello_world .asciiz "Hello World\n"         # Declare a null terminated string          # Do stuff -        addi $t1, $t1, 1 # Increment the col counter +        addi $t1, $t1, 1                  # Increment the col counter        matrix_col_end:        # Do stuff @@ -228,4 +229,63 @@ hello_world .asciiz "Hello World\n"         # Declare a null terminated string        addi $t0, $t0, 1      matrix_row_end: +## FUNCTIONS ## +  _functions: +    # Functions are callable procedures that can accept arguments and return  +    values all denoted with labels, like above + +    main:                                 # Programs begin with main func +      jal return_1                        # jal will store the current PC in $ra +                                          # and then jump to return_1 + +      # What if we want to pass in args? +      # First we must pass in our parameters to the argument registers +      li $a0, 1 +      li $a1, 2 +      jal sum                             # Now we can call the function + +      # How about recursion? +      # This is a bit more work since we need to make sure we save and restore +      # the previous PC in $ra since jal will automatically overwrite on each call +      li $a0, 3 +      jal fact + +      li $v0, 10 +      syscall +     +    # This function returns 1 +    return_1: +      li $v0, 1                           # Load val in return register $v0 +      jr $ra                              # Jump back to old PC to continue exec + + +    # Function with 2 args +    sum: +      add $v0, $a0, $a1 +      jr $ra                              # Return + +    # Recursive function to find factorial +    fact: +      addi $sp, $sp, -8                   # Allocate space in stack +      sw $s0, ($sp)                       # Store reg that holds current num +      sw $ra, 4($sp)                      # Store previous PC + +      li $v0, 1                           # Init return value +      beq $a0, 0, fact_done               # Finish if param is 0 + +      # Otherwise, continue recursion +      move $s0, $a0                       # Copy $a0 to $s0 +      sub $a0, $a0, 1 +      jal fact + +      mul $v0, $s0, $v0                   # Multiplication is done + +      fact_done: +        lw $s0, ($sp) +        lw $ra, ($sp)                     # Restore the PC +        addi $sp, $sp, 8 + +        jr $ra + +  ``` | 
