# Assembly Language Programing Exercises

## Exercise 1 - Absolute Value

The absolute value function is defined as:
 abs(x) = x, if x >= 0 = -x, otherwise
Give a minimal sequence of MIPS assembly language instructions which puts the absolute value held in register \$t0, into register \$t1.

## Exercise 2 - Swap

Write a minimal sequence of MIPS assembly language instructions, which when given two addresses of words, swaps the values stored at these addresses. The input registers are:

Inputs

## Exercise 3 - Linear Search

Write an assembly language procedure to find a given 32-bit integer in an array of integers. The input and output registers are:

Inputs
\$a0 Base address of the array
\$a1 Number of elements in the array
\$a2 Element to search for
Outputs
\$v0 Flag (see below)

Register \$v0 will hold the value 1 if the element held in \$a2 is in the array. Otherwise it will hold the value 0.

Note that the array is not sorted, so we cannot perform a binary search. Your code should still work if the size of the array is 0.

You may assume that the procedure is a leaf procedure.

## Exercise 4 - MiniMax

Write an assembly language procedure to find the minimum and maximum elements in an array of 32-bit integers. The input and output registers are:

Inputs
\$a0 Base address of the array
\$a1 Number of elements in the array
Outputs
\$v0 Minimum element
\$v1 Maximum element

The array has at least one element in it.

You can assume that the procedure is a leaf procedure.

Main Page  Section Notes Page