## Insertion sort in Assembly for Intel 8080 (and Soviet Union KR580) processor

```	lxi h, arr 	; put address of arr first element in HL
mvi c, 5	; create a counter for 6 elements (starting counting from 0)

inx h;	; using the first element by HL address for data exchange purposes
inx h;	; i = 1

for:	lxi d, arr 	; put address of arr first element in DE
mov a , m	; a = m,  a = arr[i]
xchg		; exchange HL < - > DE
mov m , a	; m = a , arr = a (in HL the address of the first element currently)
xchg		; exchange HL < - > DE

mov b,h	;
mov e,l	; save current position in array

ins:	dcx h		; arr[j-1]
sub m		; compare a and arr[j-1]
jnc endif	; if a > arr[j-1], then exit from loop
add m		; restoring the value of a
mov d,m	; d = m , d = arr[j-1]
inx h		;
mov m,d	; m = d, arr[j] = arr[j-1]
dcx h
jmp ins
endif:
add m		;  restoring the value of  a
inx h		;
mov m,a;	; arr[j] = a

mov h,b	;
mov l,e	; restoring the position in array

inx h		; i++
dcr c		; c--
jnz for	; while c != 0 go to for

hlt		; exit

arr db 00h, 04h, 09h, 07h, 08h, 01h, 02h ; input array```

## Bubble sort in Assembly for Intel 8080 (and Soviet Union KR580) processor

```	  mvi c,5  	; c = 5
next: lxi h,arr	; put reference address to arr in HL
mov d,c	; d = c
move: mov a,m 	; put first element to accumulator a = arr[d]

inx h   	; move to the next array element,	arr[d+1]

sub m 	; a = a - m, arr[d] - arr[d+1]
jnc endif	; compare elements if a > m , arr[d] > arr[d+1], finish

; exchange elements values
add m		; a = a + m
mov b,m	; b = m , b = arr[d+1]
mov m,a	; m = a , arr[d+1] = arr[d]
dcx h		; move to previous element, arr[d]
mov m,b	; m = b, arr[d] = b
inx h		; move to current element, arr[d+1]
endif:dcr d		; d--
jnz move 	; if d <> 0 , then continue to  move
dcr c		; c--
jnz next 	; if c <> 0 then continue to next

hlt		; exit

arr db 02h, 01h, 03h, 04h, 05h ; input array```