Openwrt MT7688: 彙編實現hello world

在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

《程序員的自我修養--鏈接,裝載庫》(俞甲子等)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章