新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > GNU ARM汇编--(二)汇编编译链接与运行

GNU ARM汇编--(二)汇编编译链接与运行

作者: 时间:2016-11-26 来源:网络 收藏

[cpp]view plaincopy
  1. CROSS=arm-linux-
  2. CFLAGS=-nostdlib
  3. beep.bin:start.Sbeep.S
  4. ${CROSS}gcc$(CFLAGS)-c-ostart.ostart.S
  5. ${CROSS}gcc$(CFLAGS)-c-obeep.obeep.S
  6. ${CROSS}ld-Tbeep.ldsstart.obeep.o-obeep.elf
  7. ${CROSS}objcopy-Obinary-Sbeep.elfbeep.bin
  8. rm-f*.o
  9. clean:
  10. rm-f*.elf*.o
  11. rm-fbeep.bin

编译后将beep.bin文件烧写到dram中,就可以听到声音了.虽然可以运行了,但还是有两个疑问:

1.lds编译链接文件的写法和技巧 //后续要继续追

2.elf文件的格式 //elf格式是比较新的可执行文件格式,目前在很多OS上都是用这种格式.这个格式可以在有操作系统的情况下直接运行,但是对于裸机的情况,必须对elf文件 做objcopy处理 后续也要继续追

hello world的例子如下:

helloworld.S:

[cpp]view plaincopy
  1. .data
  2. msg:.asciz"hello,world"
  3. .text
  4. .align2
  5. .global_start
  6. _start:
  7. ldrr1,=msg@address
  8. movr0,#1@stdout
  9. movr2,#13@length
  10. swi#0x900004@sys_write
  11. movr0,#0
  12. swi#0x900001@sys_exit
  13. .align2

makefile:

[cpp]view plaincopy
  1. all:
  2. arm-linux-ashelloworld.S-ohelloworld.o
  3. arm-linux-ldhelloworld.o-ohelloworld

将elf文件放到跑有linux的arm板子中,运行就输出hello world.也可以在ubuntu中qemu-arm helloworld模拟.

对比x86下同样用系统调用来输出hello world的程序:

[cpp]view plaincopy
  1. .data
  2. msg:.string"hello"
  3. len=.-msg
  4. .text
  5. .global_start
  6. _start:
  7. nop
  8. movl$len,%edx
  9. movl$msg,%ecx
  10. movl$1,%ebx
  11. movl$4,%eax
  12. int$0x80
  13. movl$0,%ebx
  14. movl$1,%eax
  15. int$0x80

它们有几点不同:

1.arm是用swi指令来进行软中断,陷入内核态来实现系统调用的.而x86是用int $0x80

2.x86的系统调用号是用eax寄存器表示的,是第一个参数.而arm的swi直接带有系统调用号,0x900004是0x900000+4,其中0x900000是base.

根据google,做了上面的总结,对GNU ARM汇编有了认识,并且对系统调用软中断,中断处理,uboot异常向量表等等有了探究的欲望,也对elf格式和编译链接有了兴趣,根据自己的方向和精力,后续对这些内容做一个或深或浅的学习.


上一页 1 2 下一页

评论


技术专区

关闭