Untitled

 avatar
unknown
assembly_x86
5 months ago
1.0 kB
1
Indexable
.global merge_s

.global merge_sort_s

@ r0 = original array
@ r1 = aux array
@ r2 = start index
@ r3 = end index

merge_sort_s:
	sub sp, sp, #20			@prologue
	str lr, [sp]
	str r4, [sp, #4]
	str r2, [sp, #8]
	str r3, [sp, #12]
	cmp r3, r2			@check if array is of single element or less
	ble done
	add r4, r2, r3
	mov ip, #0
dividebytwo:				@divide by two by repeated subtraction
	sub r4, r4, #2
	cmp r4, #0
	blt midpoint
	add ip, ip, #1
	b dividebytwo
midpoint:
	str ip, [sp, #16]
	mov r3, ip			@setup midpoint as end index for recursive call
	bl merge_sort_s
	ldr ip, [sp, #16]		@reload original indices from when the function was called
	ldr r2, [sp, #8]
	ldr r3, [sp, #12]
	add ip, ip, #1			@setup midpoint + 1 as start index for recursive call
	mov r2, ip
	bl merge_sort_s
	ldr r2, [sp, #8]		@reload original start and end indices
	ldr r3, [sp, #12]
	bl merge_s			@merge the subarrays with those indices
done:
	ldr r4, [sp, #4]		@epilogue
	ldr lr, [sp]
	add sp, sp, #20
	bx lr
Editor is loading...
Leave a Comment