Openwrt MT7688: 汇编实现hello world

  • Post author:
  • Post category:其他

在openwrt-widora环境下用汇编实现hello world, 用syscall来调用print和exit,其特点就是编译出来的可执行文件比用C编译出的小了很多。

1. 代码:  asmhello.S

#include <asm/regdef.h>
#include <asm/unistd.h>

.data
msg:    .ascii  "Hello World!\n"
length: .word   . - msg

.text
.globl main
main:
	move a0,$0
	la a1,msg
	lw a2,length
	li v0,__NR_write
	syscall

	li v0,__NR_exit
	syscall

2. 编译:

./openwrt-gcc -c -fno-builtin asmhello.S

3. 链接:
./openwrt-ld -e main -static asmhello.o -o hello

4. 剥离符号:
./openwrt-strip hello -o hello

(openwrt-xx等是相应的openwrt环境下的交叉编译工具)

5. 查看编译出来的文件:

./openwrt-objdump -d /tmp/hello
   004000b0 <.text>:
  4000b0:       00002021        move    a0,zero
  4000b4:       3c050041        lui     a1,0x41
  4000b8:       24a500e0        addiu   a1,a1,224
  4000bc:       3c060041        lui     a2,0x41
  4000c0:       8cc600f0        lw      a2,240(a2)
  4000c4:       24020fa4        li      v0,4004
  4000c8:       0000000c        syscall
  4000cc:       24020fa1        li      v0,4001
  4000d0:       0000000c        syscall

./openwrt-objdump -s /tmp/hello
/tmp/hello:     file format elf32-tradlittlemips
Contents of section .reginfo:
 400094 74000000 00000000 00000000 00000000  t...............
 4000a4 00000000 f0804100                    ......A.
Contents of section .text:
 4000b0 21200000 4100053c e000a524 4100063c  ! ..A..<...$A..<
 4000c0 f000c68c a40f0224 0c000000 a10f0224  .......$.......$
 4000d0 0c000000 00000000 00000000 00000000  ................
Contents of section .data:
 4100e0 48656c6c 6f205769 646f7261 210a0000  Hello World!...
 4100f0 10000000 00000000 00000000 00000000  ................

参考资料:

  http://www.tldp.org/HOWTO/Assembly-HOWTO/mips.html

《程序员的自我修养–链接,装载库》(俞甲子等)

 


版权声明:本文为midaszhk原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。