
	.set	noreorder
	.set	noat

	.global	_start

	.text

_start:
	sub		$sp, $sp, 16

	# Save the return address and the arguments
	sw		$ra, 0($sp)
	sw		$a0, 4($sp)
	sw		$a1, 8($sp)
	sw		$a2, 12($sp)

	# Copy our stage2 code to its final destination
	li		$a0, 0xA1FD0000
	la		$a1, CodeStart
	jal		CopyMemory
	addiu	$a2, $zero, (CodeEnd - CodeStart)
	
	# Restore the return address and the arguments
	lw		$ra, 0($sp)
	lw		$a0, 4($sp)
	lw		$a1, 8($sp)
	lw		$a2, 12($sp)

	# Jump to the stage2 code
	j		0x81FD0000
	add		$sp, $sp, 16

CopyMemory:
	beqz	$a2, CopyExit
	move	$v0, $a0
	move	$v1, $zero

CopyLoop:
	addu	$a0, $a1, $v1
	lbu		$a3, 0($a0)
	addu	$a0, $v0, $v1
	addiu	$v1, $v1, 1
	bne		$v1, $a2, CopyLoop
	sb		$a3, 0($a0)

CopyExit:
	jr		$ra
	nop

	.data

CodeStart:
	.incbin	"stage2.bin"
CodeEnd:
