ARM体系结构与编程作业

时间:2021-04-27 19:08:17

作业一

AREA exone,CODE,READWRITE
ENTRY
CODE32
MOV r0,#100 ;向寄存器中存入要比较的次数
MOV r1,#1 ;初始的两个数
MOV r2,#0
loop
ADD r2,r2,r1 ;两两相加
ADD r1,r1,#1;自动加一
SUB r0,r0,#1;要加的数减一
CMP r0,#0 ;判断比较次数是否为零
BNE loop
LDR r3,=RESULT
STR r2,[r3]
RESULT
DCD 0
STOP
B STOP

作业二

AREA extwo, CODE,READWRITE
ENTRY
CODE32
;排序:将目标地址处的数据进行冒泡排序,从小到大
MOVS r2, #1 ;r2 = 排序完成标志,1表示未完成
again
LDR r0, =data ;r0 = 目的数据块地址
MOV r1, #40;r2 = 排序字数量
SUBS r1, r1, #4
ANDS r2, r2, #1;判断排序是否完成
BEQ stop;若r2=0,完成
MOVS r2, #0;否则,标志设置为0
;STMFD sp!, {r4} ;保存工作寄存器
judge
LDR r3, [r0];读出r0地址处得值到r3
LDR r4, [r0,#4];读出r0地址处得值到r4
CMP r3, r4;比较二者的大小
BLS next;若r3<=r4,跳到next
exchange
MOVS r2, #1;发生数据交换,就设置标志为1
STR r3, [r0,#4];交换二者的值
STR r4, [r0]
next
ADDS r0, r0, #4;目标地址加4
SUBS r1, r1, #4;计数值减1
BNE judge;本趟排序未结束,跳到judge_2
B again;否则下一趟
LDMFD sp!, {r4};恢复工作寄存器
;将程序的控制权转交给调试器
stop

data
DCD 9,4,6,7,8,1,3,2,0,5
STOP
B STOP

作业三

;add.s汇编程序
stack_top equ 0x40002000
PRESERVE8
EXPORT add
AREA add,CODE,READONLY
IMPORT sum
export start
entry
start
ldr r13,=stack_top
mov r0,#1
mov r1,#100
BL sum
ldr r5,=result
str r0,[r5]
result
dcd 0
end

//sum.c程序
int sum(int a,int b)
{
int sum = 0,i;
for(i=a;i<=b;i++)
sum+=i;
return sum;
}

;add1.s汇编程序
stack_top equ 0x40002000
PRESERVE8
AREA sum,CODE,READONLY
export add1
import main
export start
start
ldr r13,=stack_top
b main
entry
add1
ADD r2,r2,r0 ;两两相加
ADD r0,r0,#1;自动加一
SUB r1,r1,#1;要加的数减一
CMP r1,#0 ;判断比较次数是否为零
BNE add1
MOV pc,lr
END
//sum1.c
#include<stdio.h>
extern int add1(int a,int b,int c);
int main()
{
int a = 0;
printf("sum 1 to 100:");
a = add1(1,100,0);
printf("%d",a);
return 1;
}

;strcopy.s
stack_top equ 0x40002000
PRESERVE8
AREA scopy,CODE,READONLY
export strcopy
import main
export start
start
ldr r13,=stack_top
b main
entry
strcopy
LDRB r2,[r1],#1
STRB r2,[r0],#1
CMP r2,#0
BNE strcopy
MOV pc,lr
end

//main.C程序
#include<stdio.h>
extern void strcopy(char *d,const char *s);
int main()
{
const char *srcstr = "abcdefghij";
char detstr[] = "second string";
printf("before copying:\n");
strcopy(detstr,srcstr);
printf("after copying:\n");
printf("%s\n%s\n",srcstr,detstr);
return 1;
}

;strcopy.s
stack_top equ 0x40002000
PRESERVE8
export copy
AREA copy,CODE,READONLY
import copystr
export start
ENTRY
start
ldr r13,=stack_top
ldr r0,=src
ldr r1,=dst
BL copystr
src
dcb "abcdefghij"
dst
dcb "helloworld"
end

//copystr.C程序
#include<stdio.h>
void copystr(char *d,char *s)
{
while((*d++=*s++)!='\0');
}

作业四

1、
AREA myarea,CODE
ENTRY
mov r0,#9
mov r1,#15
start
cmp r0,r1
bne judge
beq stop
sub1
sub r0,r0,r1
sub2
sub r1,r1,r0
judge
cmp r0,r1
bgt sub1
blt sub2
stop
b stop
end

2、
arry_size equ 10
area text,code
entry
start
mov r2,#10
ldr r0,=arry_data
ldr r3,[r0]
b addnext
save
ldr r4,=result
b stop
addnext
ldr r1,[r0,#4]!
add r3,r1,r3
sub r2,r2,#1
cmp r2,#0
beq save
bne addnext
arry_data
dcd 1,2,3,4,5,6,7,8,9,0
space arry_size
result
dcd 0
stop
b stop
end

3、
STACK_TOP EQU 0x40001000
AREA MYEX,CODE
ENTRY
MOV R0,#0
MOV R1,#1
MOV R2,#2
MOV R3,#3
MOV R4,#4
MOV R5,#5
MOV R6,#6


LDR R13,=STACK_TOP
STMFD R13!,{R0-R6} ;事前递减方式,保存现场
LDMFD R13!,{R3,R4,R6} ;事后递增方式,恢复现场
LDMFD R13!,{R5}
LDMFD R13!,{R0,R1,R2}
data
dcd 1,2,3
STOP
B STOP

4、
area test4,code
entry
start
ldr r5,=data
ldr r0,[r5]
mov r1,#0x000000FF
mov r2,#0x0000FF00
mov r3,#0x00FF0000
mov r4,#0xFF000000
;看书完成向左移位之后再加起来
and r1,r0
lsl r1,#24
ldr r0,[r5]
and r2,r0
lsl r2,#8
ldr r0,[r5]
and r3,r0
lsr r3,#8
ldr r0,[r5]
and r4,r0
lsr r4,#24
add r1,r1,r2
add r3,r3,r4
add r0,r1,r3
b stop

data
dcd 0xAABBCCDD
stop
b stop
end