现在位置:首页 >> 嵌入式操作系统 >> LINUX
arm linux 下最小的hello world程序
作者:phantom 时间:2009/8/15 文章来源:来自网络

/*
* =====================================================================================
*
* Filename: tinyhelloworld.c
*
* Description: tinyhelloworld
*
* Version: 1.0
* Created: 2009年07月04日 10时15分54秒
* Revision: none
* Compiler: gcc
*
* Author: (btree), booktree.zhou@gmail.com
* Company: none
*
* =====================================================================================
*/

char *str = "hello world!\n";
/**********************************************
*
* function :print
*description :use the syscall write
*prototype of wirte:
* ssize_t write(int fd, const void *buf, size_t count)
**********************************************/
void print(void)
{
__asm__ __volatile__(
"mov r0, #0x0\n\t" /*fd*/
"mov r1, %0\n\t" /*buf*/
"mov r2, #0x0d\n\t" /*count*/
"swi (0x00900000 + 4)\n\t"
::"r"(str):"r0","r1","r2");
}

/********************************************
* function: exit
* description: use the syscall exit
********************************************/
void exit(void)
{
__asm__ __volatile__(
"mov r0, #42\n\t"
"swi (0x00900000 + 1)\n\t");
}

void nomain(void)
{
print();
exit();
}

arm-linux-gcc -c -fno-builtin tinyhelloworld.c
arm-linux-ld -static -e nomain -o tinyhelloworld tinyhelloworld.o

运行:
/home/tester $ ls
helloworld tinyhelloworld
/home/tester $ ./tinyhelloworld
hello world!
/home/tester $ echo $?
42
你会发现tinyhelloworld的大小为:
-rwxr-xr-x 1 btree btree 1.2K 2009-07-04 16:30 tinyhelloworld
--------------------------------------------------------------------------------------------------------------------------------------------
添加tinyhelloworld.lds,进一步对section裁减
ENTRY(nomain)
SECTIONS
{
. = 0x00008000 + SIZEOF_HEADERS;
tinytext :
{
*(.text)
*(.data)
*(.rodata)
}
/DISCARD/ :
{
*(.comment)
}
}
下面是Makefile
inyhelloworld: tinyhelloworld.c
arm-linux-gcc -c -fno-builtin tinyhelloworld.c
arm-linux-ld -static -T tinyhelloworld.lds -o tinyhelloworld tinyhelloworld.o
clean:
-rm tinyhelloworld *.o
执行make后你会发现,tinyhelloworld更加小
-rwxr-xr-x 1 root root 513352 Jul 4 2009 helloworld //C代码写的helloworld
-rwxr-xr-x 1 root root 926 Jul 4 2009 tinyhelloworld //对section 裁减后的tinyhelloworld,只有926个字节

让狂风来得更猛烈些吧!


上一篇:使用虚拟机嵌入式Linux开发[返回列表]下一篇: arm2410的Linux系统移植—