1.1 编写一个汇编程序mixop.s
– 实现函数 y=mixop(x1,x2,x3)
– y=x1+x2*x3
– 调用c2.c中的multiply函数
1.2 C程序1:c1.c
– 实现函数 void init();
– 作为启动时的起始地址
– 在该函数内要调用mixop函数完成6+40*50,结果存放在变量y中。
1.3 C程序2:c2.c
– 实现函数y=multiply(x1,x2)
– y=x1*x2
提示:init()函数结尾时,设置死循环。
1/**//*
2c1.c
3*/
4
5extern unsigned int mixop( unsigned int x1, unsigned int x2, unsigned int x3 );
6
7void init() {
8 unsigned int y;
9 y = mixop( 6, 40, 50 );
10 for ( ; ; ) {
11 }
12}
13
14/**//*
15生成代码:
16
170x00008000 mov ip, sp
180x00008004 stmdb sp!, {fp, ip, lr, pc}
190x00008008 sub fp, ip, #4
200x0000800c sub sp, sp, #4
210x00008010 mov r0, #6
220x00008014 mov r1, #40
230x00008018 mov r2, #50
240x0000801c bl 0x8058
250x00008020 mov r3, r0
260x00008024 str r3, [fp, -#16]
270x00008028 b 0x8028
28*/
29
1/**//*
2c2.c
3*/
4
5unsigned int multiply( unsigned int x1, unsigned int x2 ) {
6 return x1 * x2;
7}
8
9/**//*
10生成代码:
11
12multiply :
130x0000802c mov ip, sp
140x00008030 stmdb sp!, {fp, ip, lr, pc}
150x00008034 sub fp, ip, #4
160x00008038 sub sp, sp, #8
170x0000803c str r0, [fp, -#16]
180x00008040 str r1, [fp, -#20]
190x00008044 ldr r3, [fp, -#20]
200x00008048 ldr r2, [fp, -#16]
210x0000804c mul r3, r2, r3
220x00008050 mov r0, r3
230x00008054 ldmdb fp, {fp, sp, pc}
24*/
25
1/**//*
2mixop.s
3*/
4
5.extern multiply
6
7/**//*
8unsigned int mixop( unsigned int x1, unsigned int x2, unsigned int x3 );
9*/
10.global mixop
11
12.text
13
14mixop:
15 mov ip, sp
16 stmdb sp!, {fp, ip, lr, pc}
17 sub fp, ip, #4
18
19 stmdb sp!, {r0-r2}
20 mov r0, r1
21 mov r1, r2
22 bl multiply
23 mov r3, r0
24 ldmia sp!, {r0-r2}
25 add r0, r0, r3
26
27 ldmdb fp, {fp, sp, pc}
28
29.end
30
31/**//*
32生成代码:
33
34mixop :
350x00008058 mov ip, sp
360x0000805c stmdb sp!, {fp, ip, lr, pc}
370x00008060 sub fp, ip, #4
380x00008064 stmdb sp!, {r0, r1, r2}
390x00008068 mov r0, r1
400x0000806c mov r1, r2
410x00008070 bl 0x802c
420x00008074 mov r3, r0
430x00008078 ldmia sp!, {r0, r1, r2}
440x0000807c add r0, r0, r3
450x00008080 ldmdb fp, {fp, sp, pc}
46*/
47 存在的问题,SP寄存器没有初始化,程序开始运行时,SP 为零,数据压栈后,SP 下溢出,但结果没受影响。