Most frequent value in a data array
This assignment further trains your skills in working with data that is stored in armlet memory.

Your task is to write an assembly-language program that finds the most frequently occurring value in a data array that starts at memory address $0 and contains $1 words. The most frequent value must be stored in register $2. If multiple distinct values have the maximum number of occurrences in the data array, returning any one of these values $2 will do.

Hints: Recall that the study material for Round 5 contains example armlet programs that sort an array. You may want to sort the array first and then find the most frequent value in the sorted data. Indeed, once the data is sorted, it is easy to find out how many times each value occurs by making one scan through the data. You probably want to keep track of a value that occurs the maximum number of times in the array scanned so far, and update this value whenever a more frequent value occurs.

You will use armlet assembly.

Armlet assembly instruction set:

Here are the arithmetic and logic instructions that operate on registers:

nop                   # no operation
mov $L, $A            # $L = $A (copy the value of $A to $L)
and $L, $A, $B        # $L = bitwise AND of $A and $B
ior $L, $A, $B        # $L = bitwise (inclusive) OR of $A and $B
eor $L, $A, $B        # $L = bitwise exclusive-OR of $A and $B
not $L, $A            # $L = bitwise NOT of $A
add $L, $A, $B        # $L = $A + $B
sub $L, $A, $B        # $L = $A - $B
neg $L, $A            # $L = -$A
lsl $L, $A, $B        # $L = $A shifted to the left by $B bits
lsr $L, $A, $B        # $L = $A shifted to the right by $B bits
asr $L, $A, $B        # $L = $A (arithmetically) shifted to the right by $B bits
Here are the arithmetic and logic instructions that operate on registers and take immediate data:

mov $L, I             # $L = I (copy the immediate data I to $L)
add $L, $A, I         # $L = $A + I
sub $L, $A, I         # $L = $A - I
and $L, $A, I         # $L = bitwise AND of $A and I
ior $L, $A, I         # $L = bitwise (inclusive) OR of $A and I
eor $L, $A, I         # $L = bitwise exclusive OR of $A and I
lsl $L, $A, I         # $L = $A shifted to the left by I bits
lsr $L, $A, I         # $L = $A shifted to the right by I bits
asr $L, $A, I         # $L = $A (arithmetically) shifted to the right by I bits
Here are the instructions for accessing (loading data from and storing data to) memory:

loa $L, $A            # $L = [contents of memory word at address $A]
sto $L, $A            # [contents of memory word at address $L] = $A
Here are the comparison instructions whose result controls what happens when a branch instruction is executed:

cmp $A, $B           # compare $A (left) and $B (right)
cmp $A, I            # compare $A (left) and I (right)
Here are the jump and branch instructions that take a register operand (the address where we jump or branch, if the branch condition is met):

jmp $A           # jump to address $A
beq $A           # ... if left == right (in the most recent comparison)
bne $A           # ... if left != right
bgt $A           # ... if left > right  (signed)
blt $A           # ... if left < right  (signed)
bge $A           # ... if left >= right (signed)
ble $A           # ... if left <= right (signed)
bab $A           # ... if left > right  (unsigned)
bbw $A           # ... if left < right  (unsigned)
bae $A           # ... if left >= right (unsigned)
bbe $A           # ... if left <= right (unsigned)
Here are the jump and branch instructions that take an immediate operand (the address where we jump or branch, if the branch condition is met):

jmp I            # jump to address I
beq I            # ... if left == right (in the most recent comparison)
bne I            # ... if left != right
bgt I            # ... if left > right  (signed)
blt I            # ... if left < right  (signed)
bge I            # ... if left >= right (signed)
ble I            # ... if left <= right (signed)
bab I            # ... if left > right  (unsigned)
bbw I            # ... if left < right  (unsigned)
bae I            # ... if left >= right (unsigned)
bbe I            # ... if left <= right (unsigned)
The trap instruction halts the execution temporarily so that you may observe the register contents and memory for debugging purposes when your program is running:

trp              # trap (break out of execution for debugging)
Finally, the halt instruction stops the processor:

hlt              # halt execution

Comments are made with # signs.
Loops begin with the symbol @. For example @loop: ...
When accessing a loop, we use > sign. For example, jmp >loop or bge >loop, ble >loop, etc.

Sample armlet program:
# Stores into $2 the sum of the first $0 positive integers

        mov $0, 10      # initially 10 summands remain
        mov $1, 1       # positive integers start from 1
        mov $2, 0       # the sum starts at 0
@loop:
        cmp $0, 0       # compare number of summands remaining with zero
        beq >done       # ... branch to label @done if zero summands remain
        add $2, $2, $1  # add the current integer to the sum
        add $1, $1, 1   # update the current integer to next
        sub $0, $0, 1   # decrease number of integers to sum
        jmp >loop       # jump to label @loop
@done:
        hlt             # halt the processor