# Assembly Language Programming Exercises - Answers

## Exercise 1 - Absolute Value

```      bltz \$t1, neg          # If \$t0 < 0 then branch to the instruction
# labelled neg
add  \$t1, \$t0,   \$zero # Set \$t1 to \$t0
j    skip              # Jump over the next instruction
neg:  sub  \$t1, \$zero, \$t0   # Set \$t1 to -\$t0
skip:
```
Multiplying by -1 to perform the negation is tricky: the result is held in special registers and needs to be transferred to the general purpose registers.

## Exercise 2 - Swap

```lw   \$t0, 0(\$a0) # Load the first word into \$t0
lw   \$t1, 0(\$a1) # Load the second word into \$t1
sw   \$t0, 0(\$a1) # Store \$t0 into the old second word
sw   \$t1, 0(\$a0) # Store \$t1 into the old first word
```

## Exercise 3 - Linear Search

```linSearch: # A procedure to perform linear searching of an integer array
# On entry:
# \$a0 - base address of the array
# \$a1 - number of elements in the array
# \$a2 - the number to search for
# On exit:
# \$v0 - 1 if the number was found, 0 otherwise

addi  \$t0, \$a0,   0     # Initialize \$t0 to the base address of the
# array

# Set \$t1 to the address just beyond the
# last element in the array, which is:
sll   \$t1, \$a1,   2     # the number of elements multiplied by 4

# \$t1 has this odd "beyond the end" address
# that an addition instruction can be
# removed

loop:      beq   \$t0, \$t1,   not   # If \$t0 == \$t1 then exit the loop, the

lw    \$t2, 0(\$t0)       # Load the current element into \$t2
beq   \$t2, \$a2,   found # If it is the number we are searching for
# then exit the loop

addi  \$t0, \$t0,   4     # Make \$t0 point to the next array element

not:       addi  \$v0, \$zero, 0     # Set \$v0 to 0
jr    \$ra               # Return from the procedure

found:     addi  \$v0, \$zero, 1     # Set \$v0 to 1
jr    \$ra               # Return from the procedure
```
By using a pointer into the array, you can avoid having to multiply the index by 4 and adding it to the base address of the array. This is to save instructions so that the code executes quicker.

## Exercise 4 - MiniMax

```miniMax: # A procedure to find the minimum and maximum elements of an integer
# array
# On entry:
# \$a0 - base address of the array
# \$a1 - number of elements in the array
# On exit:
# \$v0 - the minimum element
# \$v1 - the maximum element

addi  \$t0, \$a0,   4      # Initialize \$t0 to the base address of the
# array plus 4. This allows for an addition
# instruction to be removed

# Set \$t1 to the address just beyond the
# last element in the array, which is:
sll   \$t1, \$a1,    2     # the number of elements multiplied by 4

# \$t1 has this odd "beyond the end" value so
# that an addition instruction can be removed

lw    \$v0, -4(\$t0)       # Initialize \$v0 and \$v1 to the first element
addi  \$v1, \$v0,    0     # in the array. Notice the use of the offset
# field to "look backwards" - we get a "free"
# subtraction operation

loop:    beq   \$t0, \$t1,    exit  # If \$t0 == \$t1 then exit the loop

lw    \$t2, 0(\$t0)        # Load the current element into \$t2

slt   \$t3, \$t2,    \$v0   # If \$t2 >= the smallest element so far
beq   \$t3, \$zero,  skip1 # then skip the next instruction

addi  \$v0, \$t2,    0     # Set \$v0 to the current element

skip1:   slt   \$t3, \$v1,    \$t2   # If the largest element so far >= \$t2
beq   \$t3, \$zero,  skip2 # then skip the next instruction

addi  \$v1, \$t2,    0     # Set \$v1 to the current element

skip2:   addi  \$t0, \$t0,    4     # Make \$t0 point to the next array element