.include "avr/io.h"
.def ledPins = R16 ; Register to store LED pin numbers
.def i = R17 ; Loop counter
.def j = R18 ; Loop counter
.def temp = R19 ; Temporary variable
.def input = R20 ; Input string variable
.def randomNumberPtr = R21 ; Pointer to random_numbers array
.def randomNumber = R22 ; Random number variable
.def start_time = R23 ; Start time variable
.def end_time = R24 ; End time variable
.def time1 = R25 ; Time variable for bubble sort
.def time2 = R26 ; Time variable for insertion sort
.def key = R27 ; Key variable for insertion sort
.equ ledPinStart = 2 ; Starting LED pin number
.equ numLEDs = 8 ; Number of LEDs
.equ numRandomNumbers = 50 ; Number of random numbers
.text
.global main
main:
; Set the pin modes for the LEDs
ldi i, ledPinStart ; Initialize i with the starting LED pin number
ldi temp, (1 << OUTPUT) ; Initialize temp with the output mode bitmask
loop_setup:
st X+, i ; Store the current LED pin number in the ledPins array
cpi i, ledPinStart + numLEDs ; Compare i with the ending LED pin number
brne loop_setup ; Continue the loop if i is not equal to the ending LED pin number
ldi temp, (1 << RXEN0) ; Enable receiver and transmitter in Serial peripheral
sts UCSR0B, temp
ldi temp, (1 << UCSZ01) | (1 << UCSZ00) ; Set the data frame size to 8 bits
sts UCSR0C, temp
ldi temp, (F_CPU / (16UL * 9600) - 1) ; Calculate and load the baud rate divisor
sts UBRR0H, hi8(temp)
sts UBRR0L, lo8(temp)
ldi temp, 100 ; Load the delay time
call delay_ms ; Call the delay_ms subroutine
; Waiting for user input to begin
ldi input, 0 ; Initialize input string
wait_for_input:
lds temp, UCSR0A ; Read the status register of the Serial peripheral
sbrs temp, RXC0 ; Check if data is available in the receive buffer
rjmp wait_for_input ; Continue waiting if data is not available
lds input, UDR0 ; Read the received character into the input variable
; Initialize random seed
call analog_read ; Call the analog_read subroutine
; Initialize random_numbers array
ldi i, numRandomNumbers ; Initialize i with the number of random numbers
ldi randomNumberPtr, random_numbers ; Initialize randomNumberPtr with the address of random_numbers array
init_random_numbers:
call random_256 ; Call the random_256 subroutine to generate a random number
st Z+, randomNumber ; Store the random number in the random_numbers array
dec i ; Decrement i
brne init_random_numbers ; Continue the loop if i is not equal to 0
; Print numbers before sorting
ldi i, 1 ; Initialize i with 1
print_numbers:
lds temp, randomNumberPtr + i ; Load the current random number into temp
call print
; Print numbers after sorting
ldi i, 0 ; Initialize i with 0
print_numbers_sorted:
lds temp, randomNumberPtr + i ; Load the current sorted random number into temp
call print_number ; Call the print_number subroutine
inc i ; Increment i
cpi i, numRandomNumbers ; Compare i with the number of random numbers
brne print_numbers_sorted ; Continue the loop if i is not equal to the number of random numbers
ldi temp, '\n' ; Load newline character
call print_char ; Print newline character
; Get the highest number
ldi i, numRandomNumbers - 1 ; Initialize i with the index of the highest number
lds temp, randomNumberPtr + i ; Load the highest number into temp
mov highest_number, temp ; Store the highest number in highest_number variable
; Display the highest number in binary on LEDs
ldi i, 0 ; Initialize i with 0
display_highest_number:
mov temp, highest_number ; Move the highest number to temp
andi temp, 1 ; Extract the least significant bit
mov bit, temp ; Move the bit value to bit variable
lds temp, ledPins + i ; Load the LED pin number into temp
call digitalWrite_LED ; Call the digitalWrite_LED subroutine
inc i ; Increment i
cpi i, numLEDs ; Compare i with the number of LEDs
brne display_highest_number ; Continue the loop if i is not equal to the number of LEDs
ldi temp, 300 ; Load the delay time
call delay_ms ; Call the delay_ms subroutine
; Get the lowest number
ldi i, 0 ; Initialize i with 0
lds temp, randomNumberPtr + i ; Load the lowest number into temp
mov lowest_number, temp ; Store the lowest number in lowest_number variable
; Display the lowest number in binary on LEDs
ldi i, 0 ; Initialize i with 0
display_lowest_number:
mov temp, lowest_number ; Move the lowest number to temp
andi temp, 1 ; Extract the least significant bit
mov bit, temp ; Move the bit value to bit variable
lds temp, ledPins + i ; Load the LED pin number into temp
call digitalWrite_LED ; Call the digitalWrite_LED subroutine
inc i ; Increment i
cpi i, numLEDs ; Compare i with the number of LEDs
brne display_lowest_number ; Continue the loop if i is not equal to the number of LEDs
ldi temp, '\n' ; Load newline character
call print_char ; Print newline character
; Calculate the time taken for bubble sort
sub time1, end_time, start_time
ldi temp, 0 ; Load the delay time
call delay_ms ; Call the delay_ms subroutine
; Start timer for insertion sort counting in milliseconds
start_time = millis()
; Insertion sort
ldi i, 1 ; Initialize i with 1
insertion_sort:
lds key, randomNumberPtr + i ; Load the key element into key
mov j, i ; Move i to j
sub j, j, 1 ; Decrement j by 1
insertion_sort
; Insertion sort (continued)
lsl j ; Left shift j by 1 to convert it to an index
add j, j, randomNumberPtr ; Add j to the address of the random_numbers array
lds temp, (j) ; Load the current element in j into temp
compare:
cpi j, 0 ; Compare j with 0
brlo end_compare ; Exit the loop if j is less than 0
lds i, (j-1) ; Load the previous element in j into i
cpi i, key ; Compare i with key
brlo end_compare ; Exit the loop if i is less than key
sts j, i ; Store the previous element in j
sub j, j, 1 ; Decrement j by 1
rjmp compare ; Continue the loop
end_compare:
sts (j+1), key ; Store key in the correct position
inc i ; Increment i
cpi i, numRandomNumbers ; Compare i with the number of random numbers
brne insertion_sort ; Continue the loop if i is not equal to the number of random numbers
; End timer for insertion sort
end_time = millis()
; Calculate the time taken for insertion sort
sub time2, end_time, start_time
; Print numbers after sorting
ldi i, 0 ; Initialize i with 0
print_numbers_sorted:
lds temp, randomNumberPtr + i ; Load the current sorted random number into temp
call print_number ; Call the print_number subroutine
inc i ; Increment i
cpi i, numRandomNumbers ; Compare i with the number of random numbers
brne print_numbers_sorted ; Continue the loop if i is not equal to the number of random numbers
ldi temp, '\n' ; Load newline character
call print_char ; Print newline character
; Print "End of List"
ldi temp, 'E' ; Load 'E'
call print_char ; Print 'E'
ldi temp, 'n' ; Load 'n'
call print_char ; Print 'n'
ldi temp, 'd' ; Load 'd'
call print_char ; Print 'd'
ldi temp, ' ' ; Load space character
call print_char ; Print space
ldi temp, 'o' ; Load 'o'
call print_char ; Print 'o'
ldi temp, 'f' ; Load 'f'
call print_char ; Print 'f'
ldi temp, ' ' ; Load space character
call print_char ; Print space
ldi temp, 'L' ; Load 'L'
call print_char ; Print 'L'
ldi temp, 'i' ; Load 'i'
call print_char ; Print 'i'
ldi temp, 's' ; Load 's'
call print_char ; Print 's'
ldi temp, 't' ; Load 't'
call print_char ; Print 't'
ldi temp, '\n' ; Load newline character
call print_char ; Print newline character
; Print highest number
ldi temp, 'H' ; Load 'H'
call print_char ; Print 'H'
ldi temp, 'i' ; Load 'i'
call print_char ; Print 'i'
ldi temp, 'g' ; Load 'g'
call print_char ; Print 'g'
ldi temp, 'h' ; Load 'h'
call print_char ; Print 'h'
ldi temp, 'e' ; Load 'e'
call print_char ; Print 'e'
ldi temp, 's' ; Load 's'
call print_char ; Print 's'
ldi temp, 't' ; Load 't'
call print_char ; Print 't'
ldi temp, ' ' ; Load space character
call print_char ; Print space
ldi temp, 'n' ; Load 'n'
call print_char ; Print 'n'
ldi temp, 'u' ; Load 'u'
call print_char ; Print 'u'
ldi temp, 'm' ; Load 'm'
call print_char ; Print 'm'
ldi temp, 'b' ; Load 'b'
call print_char ; Print 'b'
ldi temp, 'e' ; Load 'e'
call print_char ; Print 'e'
ldi temp, 'r' ; Load 'r'
call print_char ; Print 'r'
ldi temp, ':' ; Load ':' character
call print_char ; Print ':' character
ldi temp, ' ' ; Load space character
call print_char ; Print space
; Print highest number value
lds temp, highest_number ; Load the highest number into temp
call print_number ; Call the print_number subroutine
ldi temp, '\n' ; Load newline character
call print_char ; Print newline character
; Display highest number in binary on LEDs
ldi i, 0 ; Initialize i with 0
display_highest_number:
lds temp, highest_number ; Load the highest number into temp
andi temp, 1 ; Extract the least significant bit
mov bit, temp ; Move the bit value to bit variable
lds temp, ledPins + i ; Load the LED pin number into temp
call digitalWrite_LED ; Call the digitalWrite_LED subroutine
inc i ; Increment i
cpi i, numLEDs ; Compare i with the number of LEDs
brne display_highest_number ; Continue the loop if i is not equal to the number of LEDs
ldi temp, 300 ; Load the delay time
call delay_ms ; Call the delay_ms subroutine
; Print "Lowest number:"
ldi temp, 'L' ; Load 'L'
call print_char ; Print 'L'
ldi temp, 'o' ; Load 'o'
call print_char ; Print 'o'
ldi temp, 'w' ; Load 'w'
call print_char ; Print 'w'
ldi temp, 'e' ; Load 'e'
call print_char ; Print 'e'
ldi temp, 's' ; Load 's'
call print_char ; Print 's'
lti temp, 't' ; Load 't'
call print_char ; Print 't'
ldi temp, ' ' ; Load space character
call print_char ; Print space
ldi temp, 'n' ; Load 'n'
call print_char ; Print 'n'
ldi temp, 'u' ; Load 'u'
call print_char ; Print 'u'
ldi temp, 'm' ; Load 'm'
call print_char ; Print 'm'
ldi temp, 'b' ; Load 'b'
call print_char ; Print 'b'
ldi temp, 'e' ; Load 'e'
call print_char ; Print 'e'
ldi temp, 'r' ; Load 'r'
call print_char ; Print 'r'
lli temp, ':' ; Load ':' character
call print_char ; Print ':' character
ldi temp, ' ' ; Load space character
call print_char ; Print space
; Print lowest number value
lds temp, lowest_number ; Load the lowest number into temp
call print_number ; Call the print_number subroutine
ldi temp, '\n' ; Load newline character
call print_char ; Print newline character
; Display lowest number in binary on LEDs
ldi i, 0 ; Initialize i with 0
display_lowest_number:
lds temp, lowest_number ; Load the lowest number into temp
andi temp, 1 ; Extract the least significant bit
mov bit, temp ; Move the bit value to bit variable
lds temp, ledPins + i ; Load the LED pin number into temp
call digitalWrite_LED ; Call the digitalWrite_LED subroutine
inc i ; Increment i
cpi i, numLEDs ; Compare i with the number of LEDs
brne display_lowest_number ; Continue the loop if i is not equal to the number of LEDs
ldi temp, 300 ; Load the delay time
call delay_ms ; Call the delay_ms subroutine
; Print "Bubble Sort Took: "
ldi temp, 'B' ; Load 'B'
call print_char ; Print 'B'
ldi temp, 'u' ; Load 'u'
call print_char ; Print 'u'
ldi temp, 'b' ; Load 'b'
call print_char ; Print 'b'
ldi temp, 'b' ; Load 'b'
call print_char ; Print 'b'
ldi temp, 'l' ; Load 'l'
call print_char ; Print 'l'
ldi temp, 'e' ; Load 'e'
call print_char ; Print 'e'
ldi temp, ' ' ; Load space character
call print_char ; Print space
ldi temp, 'S' ; Load 'S'
call print_char ; Print 'S'
ldi temp, 'o' ; Load 'o'
call print_char ; Print 'o'
ldi temp, 'r' ; Load 'r'
call print_char ; Print 'r'
ldi temp, 't' ; Load 't'
call print_char ; Print 't'
ldi temp, ' ' ; Load space character
call print_char ; Print space
ldi temp, 'T' ; Load 'T'
call print_char ; Print 'T'
ldi temp, 'o' ; Load 'o'
call print_char ; Print 'o'
ldi temp, 'o' ; Load 'o'
call print_char ; Print 'o'
ldi temp, 'k' ; Load 'k'
call print_char ; Print 'k'
ldi temp, ':' ; Load ':' character
call print_char ; Print ':' character
ldi temp, ' ' ; Load space character
call print_char ; Print space
; Print bubble sort time
lds temp, time1 ; Load the bubble sort time into temp
call print_number ; Call the print_number subroutine
ldi temp, ' ' ; Load space character
call print_char ; Print space
ldi temp, 'm' ; Load 'm'
call print_char ; Print 'm'
ldi temp, 'i' ; Load 'i'
call print_char ; Print 'i'
ldi temp, 'l' ; Load 'l'
call print_char ; Print 'l'
ldi temp, 'l' ; Load 'l'
call print_char ; Print 'l'
ldi temp, 'i' ; Load 'i'
call print_char ; Print 'i'
ldi temp, 's' ; Load 's'
call print_char ; Print 's'
ldi temp, 'e' ; Load 'e'
call print_char ; Print 'e'
ldi temp, 'c' ; Load 'c'
call print_char ; Print 'c'
ldi temp, 'o' ; Load 'o'
call print_char ; Print 'o'
ldi temp, 'n' ; Load 'n'
call print_char ; Print 'n'
ldi temp, 'd' ; Load 'd'
call print_char ; Print 'd'
ldi temp, 's' ; Load 's'
call print_char ; Print 's'
ldi temp, '\n' ; Load newline character
call print_char ; Print newline character
; Print "Insertion Sort Took: "
ldi temp, 'I' ; Load 'I'
call print_char ; Print 'I'
ldi temp, 'n' ; Load 'n'
call print_char ; Print 'n'
ldi temp, 's' ; Load 's'
call print_char ; Print 's'
ldi temp, 'e' ; Load 'e'
call print_char ; Print 'e'
ldi temp, 'r' ; Load 'r'
call print_char ; Print 'r'
ldi temp, 't' ; Load 't'
call print_char ; Print 't'
ldi temp, 'i' ; Load 'i'
call print_char ; Print 'i'
ldi temp, 'o' ; Load 'o'
call print_char ; Print 'o'
ldi temp, 'n' ; Load 'n'
call print_char ; Print 'n'
ldi temp, ' ' ; Load space character
call print_char ; Print space
; Print insertion sort time
lds temp, time2 ; Load the insertion sort time into temp
call print_number ; Call the print_number subroutine
ldi temp, ' ' ; Load space character
call print_char ; Print space
ldi temp, 'm' ; Load 'm'
call print_char ; Print 'm'
ldi temp, 'i' ; Load 'i'
call print_char ; Print 'i'
ldi temp, 'l' ; Load 'l'
call print_char ; Print 'l'
ldi temp, 'l' ; Load 'l'
call print_char ; Print 'l'
ldi temp, 'i' ; Load 'i'
call print_char ; Print 'i'
ldi temp, 's' ; Load 's'
call print_char ; Print 's'
ldi temp, 'e' ; Load 'e'
call print_char ; Print 'e'
ldi temp, 'c' ; Load 'c'
call print_char ; Print 'c'
ldi temp, 'o' ; Load 'o'
call print_char ; Print 'o'
ldi temp, 'n' ; Load 'n'
call print_char ; Print 'n'
ldi temp, '\n' ; Load newline character
call print_char ; Print newline character
; Print "Numbers after sorting:"
ldi temp, 'N' ; Load 'N'
call print_char ; Print 'N'
ldi temp, 'u' ; Load 'u'
call print_char ; Print 'u'
ldi temp, 'm' ; Load 'm'
call print_char ; Print 'm'
ldi temp, 'b' ; Load 'b'
call print_char ; Print 'b'
ldi temp, 'e' ; Load 'e'
call print_char ; Print 'e'
ldi temp, 'r' ; Load 'r'
call print_char ; Print 'r'
lli temp, 's' ; Load 's' character
call print_char ; Print 's' character
lli temp, ' ' ; Load space character
call print_char ; Print space
; Print numbers after sorting
ldi i, 0 ; Initialize i with 0
print_numbers_after_sorting:
ldi temp, i + 1 ; Load the number of the random number
call print_number ; Call the print_number subroutine
ldi temp, ')' ; Load ')' character
call print_char ; Print ')' character
ldi temp, ' ' ; Load space character
call print_char ; Print space
lds temp, random_numbers + i ; Load the random number into temp
call print_number ; Call the print_number subroutine
ldi temp, '\n' ; Load newline character
call print_char ; Print newline character
ldi temp, 100 ; Load the delay time
call delay_ms ; Call the delay_ms subroutine
inc i ; Increment i
cpi i, numNumbers ; Compare i with the number of random numbers
brne print_numbers_after_sorting ; Continue the loop if i is not