sort.s

来自「pebble」· S 代码 · 共 55 行

S
55
字号
/* 
 * Copyright 1999, 2000, 2001, 2002 Lucent Technologies Inc.
 * All Rights Reserved.
 * Information Sciences Research Center, Bell Labs.
 *
 * LUCENT TECHNOLOGIES DOES NOT CLAIM MERCHANTABILITY OF THIS SOFTWARE 
 * OR THE SUITABILITY OF THIS SOFTWARE FOR ANY PARTICULAR PURPOSE. The
 * software is provided "as is" without expressed or implied warranty 
 * of any kind.
 *
 * These notices must be retained in any copies of any part of this
 * software.
 *
 */

/*
 * simple minded bubble sort
 */

#include <mips/asm.h>
#include <mips/regdef.h>
#include <mips/cpu.h>

.text

/*
 * sort(v, n)
 */
LEAF(sort)
	addi	t0, a1, -1	# for (i = n-1; i >= 0; i--)
1:
	slt	t1, t0, zero
	bne	t1, zero, 2f	# if (i < 0) then exit
	li	t2, 0		# for (j = 0; j < i; j++)
	move	t3, a0		# t3 := &v[j]
3:
	slt	t1, t2, t0	# if (j >= i) break;
	beqz	t1, 4f
	ld	t4, 0(t3)
	ld	t5, 8(t3)
	sgt	t1, t4, t5	# if (a[j] > a[j+1])
	beqz	t1, 5f
	sd	t4, 8(t3)	# swap a[j] with a[j+1]
	sd	t5, 0(t3)
5:
	addi	t2, t2, 1	# j++
	addi	t3, t3, 8	# &v[j]
	b	3b
4:
	addi	t0, t0, -1
	b	1b
2:
	jr	ra
END(sort)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?