GNU ARM汇编--(二)汇编编译链接与运行
[cpp]view plaincopy
- CROSS=arm-linux-
- CFLAGS=-nostdlib
- beep.bin:start.Sbeep.S
- ${CROSS}gcc$(CFLAGS)-c-ostart.ostart.S
- ${CROSS}gcc$(CFLAGS)-c-obeep.obeep.S
- ${CROSS}ld-Tbeep.ldsstart.obeep.o-obeep.elf
- ${CROSS}objcopy-Obinary-Sbeep.elfbeep.bin
- rm-f*.o
- clean:
- rm-f*.elf*.o
- rm-fbeep.bin
编译后将beep.bin文件烧写到dram中,就可以听到声音了.虽然可以运行了,但还是有两个疑问:
1.lds编译链接文件的写法和技巧 //后续要继续追
2.elf文件的格式 //elf格式是比较新的可执行文件格式,目前在很多OS上都是用这种格式.这个格式可以在有操作系统的情况下直接运行,但是对于裸机的情况,必须对elf文件 做objcopy处理 后续也要继续追
hello world的例子如下:
helloworld.S:
[cpp]view plaincopy
- .data
- msg:.asciz"hello,world"
- .text
- .align2
- .global_start
- _start:
- ldrr1,=msg@address
- movr0,#1@stdout
- movr2,#13@length
- swi#0x900004@sys_write
- movr0,#0
- swi#0x900001@sys_exit
- .align2
makefile:
[cpp]view plaincopy
- all:
- arm-linux-ashelloworld.S-ohelloworld.o
- arm-linux-ldhelloworld.o-ohelloworld
将elf文件放到跑有linux的arm板子中,运行就输出hello world.也可以在ubuntu中qemu-arm helloworld模拟.
对比x86下同样用系统调用来输出hello world的程序:
[cpp]view plaincopy
- .data
- msg:.string"hello"
- len=.-msg
- .text
- .global_start
- _start:
- nop
- movl$len,%edx
- movl$msg,%ecx
- movl$1,%ebx
- movl$4,%eax
- int$0x80
- movl$0,%ebx
- movl$1,%eax
- int$0x80
它们有几点不同:
1.arm是用swi指令来进行软中断,陷入内核态来实现系统调用的.而x86是用int $0x80
2.x86的系统调用号是用eax寄存器表示的,是第一个参数.而arm的swi直接带有系统调用号,0x900004是0x900000+4,其中0x900000是base.
根据google,做了上面的总结,对GNU ARM汇编有了认识,并且对系统调用软中断,中断处理,uboot异常向量表等等有了探究的欲望,也对elf格式和编译链接有了兴趣,根据自己的方向和精力,后续对这些内容做一个或深或浅的学习.
评论