@ r0 = destination
@ r1 = source
	.section	.text
	.global	substr
	.type	substr, %function
substr:
	sub	sp, sp, #8	@ Allocate space
	str	r3, [sp]	@ Save the passed length
	ldrb	r3, [r1]	@ source, dereferenced (byte)
	cmp	r2, r3
	bhi	error		@ start > byte
	ldrb	r3, [r1]	@ source, dereferenced (byte)
	str	r2, [sp, #4]	@ Save start
	sub	r2, r3, r2	@ length = byte - start
	ldr	r3, [sp]	@ Get the passed length
	cmp	r3, r2
	bhs	keep_length	@ passed length >= length
	mov	r2, r3		@ Use the passed length instead
keep_length:
	strb	r2, [r0]	@ destination, dereferened = length
	add	r0, r0, #1	@ destination = destination + 1
	ldr	r3, [sp, #4]
	add	sp, sp, #8	@ Deallocate space
	add	r3, r3, #1	@ r3 = start + 1
	add	r1, r3, r1	@ source = source + r3
	b	loop
copy_byte:
	ldrb	r3, [r1]	@ source, dereferenced
	strb	r3, [r0]	@ copy to destination, dereferenced
	sub	r2, r2, #1	@ length = length - 1
	add	r0, r0, #1	@ destination = destination + 1
	add	r1, r1, #1	@ source = source + 1
loop:
	cmp	r2, #0
	bne	copy_byte	@ length <> 0
	mov	pc, lr		@ Return
error:
	mov	r0, #-36	@ Return the String Index Error number
	mov	pc, lr		@ Return
