ARM
在uboot下运行程序
- tftp 0x30000000 hello2
- go 0x30000000(一按就重启)
解决方案
- void printf(const char *fmt)为函数原型
- 进入uboot.map,找到printf的为0x33f963a8
mkdir 1_hello+cd 1_hello
-
vim hello.c
void (*my_printf)(const char* fmt,...); int_start() { my_printf=(void *)0x33f963a8; my_printf("你瞅啥\n"); return 0; }
-
编写一个makefile
CC=arm-linux- TARGET=hello all: $(CC)gcc -c $(TARGET).c -o $(TARGET).o $(CC)ld -Ttext 0x30000000 $(TARGET).o -o $(TARGET) $(CC)objcopy -O binary $(TARGET) $(TARGET).bin cp $(TARGET).bin /tftpboot/ clean: rm /tftpboot/$(TARGET).bin *.o *.bin $(TARGET)
make生成.bin文件
-
进入u-boot终端
- tftp 0x30000000 hello.bin
- go 0x30000000
写一个scanf
int (*my_scanf)(void);
void (*my_printf)(const char * fmt,...);
int _start()
{
int ch;
my_scanf=(void *)0x33f965f0;
my_printf=(void *)0x33f963a8;
while(1)
{
ch=my_scanf();
if(ch=='q')
{
break;
}
my_printf("%c\n",ch);
}
my_printf("lalalalla\n");
return 0;
}
- common.h路径
- /home/sunsuhui/1612/ARM/src/bootloader/u-boot/include/common.h
- u-boot.map路径
- /home/sunsuhui/1612/ARM/src/bootloader/u-boot/u-boot.map
汇编与C一起使用
汇编语言
- 指用助记符(Memoni)代替操作码,用地址符号(Symbol)或标签(Label)代替地址码的编程语言
- 优点
- 可以直接访问硬件
- 目标代码简短,执行速度快
- 缺点
- 可移植性差
- 可阅读性差
-
ARM寄存器组
ARM指令集
add
void (*my_printf)(const char* fmt,...);
int (*my_getc)(void);
int _start(void)
{
int a,b,c;
my_printf = (void *)0x33f963a8;
my_getc = (void *)0x33f965f0;
a=my_getc();
my_printf("%c\n",a);
a-='0';
b=my_getc();
b-='0';
my_printf("%d\n",b);
__asm__ __volatile__
(
"mov r0,%1\n"
"mov r1,%2\n"//r0,r1放上a,b的值
"add r2,r0,r1\n"
"mov %0,r2\n"
:"=r"(c)//r0
:"r"(a),"r"(b)//a:r1,b:r2
:
)
my_printf("%d+%d=%d",a,b,c);
return 0;
}
- 执行结果为
ldr使用
void (*my_printf)(const char* fmt,...);
int (*my_getc)(void);
int _start(void)
{
int a,b;
my_printf = (void *)0x33f963a8;
my_getc = (void *)0x33f965f0;
a=my_getc();
my_printf("%c\n",a);
__asm__ __volatile__(
"mov r1,%1\n"//a存r1
"ldr r0,=0x30008000\n"//创建一个指针
"str r1,[r0]\n"//a存入指针
"ldr r2,[r0]\n"//把指针内容给r2
"mov %0,r2\n"//把r2放入b
:"=r"(b)
:"r"(a)
:"r0","r1","r2"
);
my_printf("%c",b);
return 0;
}
ldmstm
void (*my_printf)(const char* fmt,...);
int _start(void)
{
int a,b,c,d;
my_printf = (void *)0x33f963a8;
__asm__ __volatile__(
"mov r1,#0x01\n"
"mov r2,#0x02\n"
"mov r3,#0x03\n"
"mov r4,#0x04\n"
"ldr r0,=0x30008000\n"//创建指针
"stmia r0!,{r1-r4}\n"//指定r0的变化方式
"ldr r0,=0x30008000\n"
"ldmia r0!,{r1-r4}\n"
"mov %0,r1\n"
"mov %1,r2\n"
"mov %2,r3\n"
"mov %3,r4\n"
:"=r"(a),"=r"(b),"=r"(c),"=r"(d)
:
:"r0","r1","r2","r3","r4"
);
my_printf("a=%d,b=%d,c=%d,d=%d",a,b,c,d);
return 0;
}
bl
int _start(void)
{
void (*my_printf)(const char* fmt,...);
my_printf = (void *)0x33f963a8;
my_printf("In %s: before bl.\n",__func__);
__asm__ __volatile__(
"bl mytest\n"
);
my_printf("In %s: after bl.\n",__func__);
return 0;
}
void mytest()
{
void (*my_printf)(const char* fmt,...);
my_printf = (void *)0x33f963a8;
my_printf("In %s:in bl.\n",__func__);
}
条件码
void (*my_printf)(const char* fmt,...);
int (*my_getc)(void);
int _start(void)
{
int a,b;
my_printf = (void *)0x33f963a8;
my_getc = (void *)0x33f965f0;
a=my_getc();
my_printf("%c\n",a);
a-='0';
b=my_getc();
my_printf("%c\n",b);
b-='0';
__asm__ __volatile__(
"mov r0,%2\n"
"mov r1,%3\n"
"cmp r0,r1\n"
"addhi r0,r0,#1\n"
"addls r1,r1,#1\n"
"mov %0,r0\n"
"mov %1,r1\n"
:"=r"(a),"=r"(b)
:"r"(a),"r"(b)
:"r0","r1"
);
my_printf("a=%d,b=%d",a,b);
return 0;
}