第一个ARM汇编程序——冒泡排序

引用请注明出处:http://blog.csdn.net/int64ago/article/details/7008883

AREA Sort,CODE,READONLY		;declare for code area
	ENTRY					;entry for the whole code
start						;main code flag
	MOV R4,#0				;clear r4
	LDR R6,=src				;r6 point to the begining of numbers
	ADD R6,R6,#len			;r6 point to the end of numbers  
outer					 	;outer loop begining
	LDR R1,=src				;r1 point to the begining of numbers 
inner						;inner loop begining
	LDR R2,[R1]				;get the number in address of r1
	LDR R3,[R1,#4]			;get the number in address next to r1
	CMP R2,R3				;compare two numbers we gotten
	STRGT R3,[R1]			;if the first > the second 
	STRGT R2,[R1,#4]			;exchange the position of two numbers
	ADD R1,R1,#4				;the point of r1 move
	CMP R1,R6				;compare position current  and ending
	BLT inner				;if not meet the ending go on to loop

	ADD R4,R4,#4				;global counter +1
	CMP R4,#len				;compare the current position
	SUBLE R6,R6,#4			;if not meet the ending
	BLE outer				;go on to loop

	AREA Array,DATA,READWRITE ;decare for data area
src	DCD 2,4,10,8,14,1,20	  	;init the original numbers
len	EQU 7*4					;get the length of numbers

	END						;end of whole code

  • 18
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
使用ARM汇编指令实现冒泡排序的关键是编写内层循环的代码,其中涉及到数据交换和循环次数的控制。首先,可以使用STR(数据装载指令)来实现数据的交换。接着,通过CMP指令来控制内层循环的次数。最后,编写外层循环的代码,同样使用CMP指令来控制整个排序的次数。以下是一个用ARM汇编指令实现冒泡排序的例子: ``` .section .data numbers: .byte 5, 8, 2, 1, 9, 3, 6, 7, 4 @ 待排序的数组 .section .text .global _start _start: LDR r1, =numbers @ 加载数组的地址 LDR r2, =9 @ 数组长度 MOV r3, #0 @ 外层循环计数器 outer_loop: MOV r4, #0 @ 内层循环计数器 MOV r5, r2 @ 将数组长度保存到r5中 inner_loop: LDRB r6, [r1 @ 加载当前元素到r6 LDRB r7, [r1, #1 @ 加载下一个元素到r7 CMP r6, r7 @ 比较两个元素的大小 BLE swap @ 如果r6小于等于r7,则交换两个元素 increment: ADD r1, #1 @ 数组指针加1 ADD r4, #1 @ 内层循环计数器加1 CMP r4, r5 @ 比较内层循环计数器和数组长度 BNE inner_loop @ 如果内层循环计数器不等于数组长度,则继续内层循环 decrement: SUB r5, #1 @ 数组长度减1 CMP r5, #1 @ 比较数组长度和1 BNE outer_loop @ 如果数组长度不等于1,则继续外层循环 exit: B exit swap: STRB r7, [r1 @ 将r7的值存储到当前元素位置 STRB r6, [r1, #1 @ 将r6的值存储到下一个元素位置 B increment @ 继续增加内层循环计数器,进行下一次循环 ``` 这段代码使用ARM汇编指令实现了冒泡排序算法。在排序前,先定义了一个待排序的数组numbers。然后使用LDR指令加载数组的地址和长度。之后,使用外层循环和内层循环实现冒泡排序的逻辑。最后,通过STRB指令将交换后的值存储到对应的位置。请注意,在实际使用中,可能需要根据具体情况对代码进行修改和调试。<span class="em">1</span><span class="em">2</span><span class="em">3</span>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值