ARM 汇编(sum, gcd, lcm, sort, max, min, strcpy)

1 一位数组按某种规律求和

1.1 一维数组求和

	AREA ARRAYSUM, CODE, READONLY
	ENTRY
START
	LDR R0, =src
	MOV R1, #0
	MOV R3, #0
	
LOOP
	LDR R2, [R0]
	ADD R0, R0, #4
	ADD R3, R3, #1
	ADD R1, R1, R2
	CMP R3, #len
	BNE LOOP

STOP
	MOV R5, R1
	MOV R0, #24
	LDR R1, =0x0300000
	SWI 0X123456
	
	AREA ARRAY, DATA, READONLY
src DCD 1, 2, 3, 4, 5 ,6 ,7, 8, 9, 10
len EQU 10
	END

1.2 求一维数组中奇数的和

	AREA ARRAYSUM, CODE, READONLY
	ENTRY
START
	LDR R0, =src
	MOV R1, #0
	MOV R3, #0
	
LOOP
	LDR R2, [R0]
	ADD R0, R0, #4
	ADD R3, R3, #1
	AND R4, R2, #1
	CMP R4, #0
	ADDNE R1, R1, R2
	CMP R3, #len
	BNE LOOP

STOP
	MOV R5, R1
	MOV R0, #24
	LDR R1, =0x0300000
	SWI 0X123456
	
	AREA ARRAY, DATA, READONLY
src DCD 1, 2, 3, 4, 5 ,6 ,7, 8, 9, 10
len EQU 10
	END

1.3 求一维数组中的偶数和

	AREA ARRAYSUM, CODE, READONLY
	ENTRY
START
	LDR R0, =src
	MOV R1, #0
	MOV R3, #0
	
LOOP
	LDR R2, [R0]
	ADD R0, R0, #4
	ADD R3, R3, #1
	AND R4, R2, #1
	CMP R4, #1
	ADDNE R1, R1, R2
	CMP R3, #len
	BNE LOOP

STOP
	MOV R5, R1
	MOV R0, #24
	LDR R1, =0x0300000
	SWI 0X123456
	
	AREA ARRAY, DATA, READONLY
src DCD 1, 2, 3, 4, 5 ,6 ,7, 8, 9, 10
len EQU 10
	END

2 求两个数的最大公约数

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}
	AREA SOLVEGCD, CODE, READONLY
	ENTRY
START
	MOV R0, #20;a
	MOV R1, #50;b
	
GCD
	CMP R0, R1;比较两个数的大小,大的数放 R0,小的数放 R1,下三行表示交换数字的过程
	MOVLS R2, R1
	MOVLS R1, R0
	MOVLS R0, R2
	SUB R0, R0, R1;使用更相减损法求最大公约数
	CMP R0, R1;若减数等于差,则得到最大公约数,最大公约数为差
	BNE GCD

STOP
	MOV R2, R1;将结果保存到 R2
	MOV R0, #24
	LDR R1, =0x0300000
	SWI 0X123456
	
	END

3 求两个数的最小公倍数

lcm ⁡ ( a , b ) = a × b gcd(a,b) ⁡ \operatorname{lcm}(a,b)=\frac{a\times b}{\operatorname{gcd(a,b)}} lcm(a,b)=gcd(a,b)a×b

	AREA SOLVELCM, CODE, READONLY
	ENTRY
START
	MOV R0, #20;a
	MOV R1, #50;b
	MUL R3, R0, R1;a * b
	MOV R4, #0;保存 LCM 计算结果
	
GCD
	CMP R0, R1
	MOVLS R2, R1
	MOVLS R1, R0
	MOVLS R0, R2
	SUB R0, R0, R1
	CMP R0, R1
	BNE GCD

;模拟除法
LCM
	SUB R3, R3, R0
	ADD R4, R4, #1
	CMP R3, #0;判断是否除尽
	BNE LCM
	
STOP
	MOV R0, #24
	LDR R1, =0x0300000
	SWI 0X123456
	
	END

4 对一维数组进行排序

冒泡排序

	AREA SORT, CODE, READONLY
	ENTRY
START
	LDR R5, =src
	MOV R3, #1

;外层循环
LOOPI
	LDR R0, =src
	MOV R4, #1

;内层循环
LOOPJ
	LDR R1, [R0]
	LDR R2, [R0, #4]
	CMP R1, R2
	STRGT R2, [R0]
	STRGT R1, [R0, #4]
	ADD R0, R0, #4
	ADD R4, R4, #1
	CMP R4, #len
	BNE LOOPJ
	
	ADD R3, R3, #1
	CMP R3, #len
	BNE LOOPI
	
STOP
	MOV R0, #24
	LDR R1, =0x0300000
	SWI 0X123456
	
	AREA STRING, DATA, READONLY
src DCD 1, 5, 4, 6, 3, 8, 9, 2, 7, 10
len EQU 10
	
	END

5 求一维数组中的最大值

	AREA SOLVEMAX, CODE, READONLY
	ENTRY
START
	LDR R0, =src;将数组的首地址放到R0
	LDR R2, [R0];取数组的第一个值
	MOV R1, #1;R1 用于记录访问了数组多少个元素了

LOOP
	ADD R0, R0, #4;更新地址
	LDR R3, [R0];取值
	ADD R1, R1, #1;计数加一
	CMP R3, R2;比较两个数的大小
	MOVHI R2, R3;如果 R3 大于 R2,则将 R2 的值设为 R3
	CMP R1, #len;判断是否遍历完毕
	BNE LOOP		
	
STOP
	MOV R0, #24
	LDR R1, =0x0300000
	SWI 0X123456
	
	AREA ARRAY, DATA, READONLY
src DCD 4, 5, 2, 3, 1, 6, 4, 3
len EQU 8
	
	END

6 求一维数组中的最小值

	AREA SOLVEMIN, CODE, READONLY
	ENTRY
START
	LDR R0, =src
	MOV R1, #1
	LDR R2, [R0]

LOOP
	ADD R0, R0, #4
	LDR R3, [R0]
	ADD R1, R1, #1
	CMP R2, R3;相比于求最大值的代码,唯一改动的地方
	MOVHI R2, R3
	CMP R1, #len
	BNE LOOP		
	
STOP
	MOV R0, #24
	LDR R1, =0x0300000
	SWI 0X123456
	
	AREA ARRAY, DATA, READONLY
src DCD 4, 5, 2, 3, 1, 6, 4, 3
len EQU 8
	
	END

7 字符串的复制

	AREA SOLVEMAX, CODE, READONLY
	ENTRY
START
	LDR R2, =src
	LDR R3, =des

LOOP
	LDRB R4, [R2], #1
	STRB R4, [R3], #1
	CMP R4, #0
	BNE LOOP
	
STOP
	MOV R0, #24
	LDR R1, =0x0300000
	SWI 0X123456
	
	AREA STRING, DATA, READONLY
src DCB "SWPUACM\0"
des DCB ""
	
	END

你可能感兴趣的:(学习笔记,arm,arm开发)