首页 > 代码库 > mips汇编完整程序的分析
mips汇编完整程序的分析
下面讲解一下mips汇编的一些整个程序的编译过程的分析:
这是我最近看Mips汇编所总结的一些东西,有的地方可能会有一些错误,
希望看到这边博客的各位给与指点:在此表示诚挚的感谢。
#include <stdio.h>
int main(int argc,char *argv[])
{
int b = 2;
int c = 3;
int a = b*c;
int d = c/3;
printf("a == %d,b == %d,c == %d,c == %d\n",a,b,c,d);
return a;
}
$: mipsel-linux-gcc -O2 test.c -o test --save-temps
$: mipsel-linux-objdump -d -S test > dump
生成的汇编程序: vi test.s
.file 1 "test.c"
.section .mdebug.abi32#软件标准为o32.previous
.abicalls
.section .rodata.str1.4,"aMS",@progbits,1
.align 2 # 2字节对齐也就是:2*2
$LC0:
.ascii "a == %d,b == %d,c == %d,c == %d\012\000"
.text #程序段
.align 2 #2字节对齐
.globl main #全局变量
.ent main #main函数的开始
.type main, @function #类型,指定为函数,但是也可以是变量
main:
.frame $sp,40,$31 # vars= 0, regs= 1/0, args= 24, gp= 8
.mask 0x80000000,-8
.fmask 0x00000000,0
.set noreorder #.set设置属性为不优化代码
.cpload $25 #
.set nomacro
addiu $sp,$sp,-40 #将堆栈指针的值-40,会在程序的最后恢复sp的值
sw $31,32($sp)
.cprestore 24
lw $4,%got($LC0)($28) #这一段是printf函数内的字符所在的got表中的地址加载(load)到$4寄存器中。
lw $25,%call16(printf)($28) # t9代表的是printf函数在GOT表中的地址的值加载(load)到$25寄存器中
li $2,1 # 0x1 #这是经过优化后的给d所赋得值
sw $2,16($sp)
addiu $4,$4,%lo($LC0)
li $5,6 # 0x6 #这是加载a=b*c的值的操作
li $6,2 # 0x2 #加载b的值到$6寄存器中
jalr $25
li $7,3 # 0x3 #加载c的值到$7寄存器中
lw $28,24($sp)
lw $31,32($sp)
li $2,6 # 0x6
j $31 #跳转到函数的结束
addiu $sp,$sp,40 #恢复在main函数开始的时候对堆栈指针的操作
.set macro
.set reorder #设置属性为优化代码
.end main #main函数的结束
.ident "GCC: (GNU) 4.1.2"#指定编译器的版本号
test: file format elf32-tradlittlemips
下面打开反汇编生成的dump文件:vi dump
函数的实际执行的入口不是main()而是在main()之前已经做了一些初始化的操作,入口为:<_init>
而结束的地方为<_fini>,在反汇编生成的文件中需要主要的地方为<_main>中的GOT表中的项
Disassembly of section .init:
00400424 <_init>:
400424: 3c1c0005 lui gp,0x5
400428: 279c849c addiu gp,gp,-31588
40042c: 0399e021 addu gp,gp,t9
400430: 27bdffe0 addiu sp,sp,-32
400434: afbf0018 sw ra,24(sp)
400438: afbc0010 sw gp,16(sp)
40043c: 8f998018 lw t9,-32744(gp)
400440: 00000000 nop
400444: 27390540 addiu t9,t9,1344
400448: 0320f809 jalr t9
40044c: 00000000 nop
400450: 8fbc0010 lw gp,16(sp)
400454: 00000000 nop
400458: 04110001 bal 400460 <_init+0x3c>
40045c: 00000000 nop
400460: 3c1c0005 lui gp,0x5
400464: 279c8460 addiu gp,gp,-31648
400468: 039fe021 addu gp,gp,ra
40046c: 8f998018 lw t9,-32744(gp)
400470: 00000000 nop
400474: 27390604 addiu t9,t9,1540
400478: 0320f809 jalr t9
40047c: 00000000 nop
400480: 8fbc0010 lw gp,16(sp)
400484: 00000000 nop
400488: 04110001 bal 400490 <_init+0x6c>
40048c: 00000000 nop
400490: 3c1c0005 lui gp,0x5
400494: 279c8430 addiu gp,gp,-31696
400498: 039fe021 addu gp,gp,ra
40049c: 8f998018 lw t9,-32744(gp)
4004a0: 00000000 nop
4004a4: 27390750 addiu t9,t9,1872
4004a8: 0320f809 jalr t9
4004ac: 00000000 nop
4004b0: 8fbc0010 lw gp,16(sp)
4004b4: 00000000 nop
4004b8: 8fbf0018 lw ra,24(sp)
4004bc: 00000000 nop
4004c0: 03e00008 jr ra
4004c4: 27bd0020 addiu sp,sp,32
Disassembly of section .text:
004004d0 <__start>:
4004d0: 03e00021 move zero,ra
4004d4: 04110001 bal 4004dc <__start+0xc>
4004d8: 00000000 nop
4004dc: 3c1c0005 lui gp,0x5
4004e0: 279c83e4 addiu gp,gp,-31772
4004e4: 039fe021 addu gp,gp,ra
4004e8: 0000f821 move ra,zero
4004ec: 8f848048 lw a0,-32696(gp)
4004f0: 8fa50000 lw a1,0(sp)
4004f4: 27a60004 addiu a2,sp,4
4004f8: 2401fff8 li at,-8
4004fc: 03a1e824 and sp,sp,at
400500: 27bdffe0 addiu sp,sp,-32
400504: 8f87804c lw a3,-32692(gp)
400508: 8f888054 lw t0,-32684(gp)
40050c: 00000000 nop
400510: afa80010 sw t0,16(sp)
400514: afa20014 sw v0,20(sp)
400518: afbd0018 sw sp,24(sp)
40051c: 8f998044 lw t9,-32700(gp)
400520: 00000000 nop
400524: 0320f809 jalr t9
400528: 00000000 nop
0040052c <hlt>:
40052c: 1000ffff b 40052c <hlt>
400530: 00000000 nop
...
00400540 <call_gmon_start>:
400540: 3c1c0005 lui gp,0x5
400544: 279c8380 addiu gp,gp,-31872
400548: 0399e021 addu gp,gp,t9
40054c: 8f828038 lw v0,-32712(gp)
400550: 8f998038 lw t9,-32712(gp)
400554: 10400003 beqz v0,400564 <call_gmon_start+0x24>
400558: 00000000 nop
40055c: 03200008 jr t9
400560: 00000000 nop
400564: 03e00008 jr ra
400568: 00000000 nop
40056c: 00000000 nop
00400570 <__do_global_dtors_aux>:
400570: 3c1c0005 lui gp,0x5
400574: 279c8350 addiu gp,gp,-31920
400578: 0399e021 addu gp,gp,t9
40057c: 27bdffd8 addiu sp,sp,-40
400580: afbf0020 sw ra,32(sp)
400584: afb1001c sw s1,28(sp)
400588: afb00018 sw s0,24(sp)
40058c: afbc0010 sw gp,16(sp)
400590: 8f91801c lw s1,-32740(gp)
400594: 00000000 nop
400598: 92220920 lbu v0,2336(s1)
40059c: 00000000 nop
4005a0: 14400013 bnez v0,4005f0 <__do_global_dtors_aux+0x80>
4005a4: 00000000 nop
4005a8: 8f90801c lw s0,-32740(gp)
4005ac: 00000000 nop
4005b0: 8e0208b0 lw v0,2224(s0)
4005b4: 00000000 nop
4005b8: 8c590000 lw t9,0(v0)
4005bc: 00000000 nop
4005c0: 13200009 beqz t9,4005e8 <__do_global_dtors_aux+0x78>
4005c4: 24420004 addiu v0,v0,4
4005c8: 0320f809 jalr t9
4005cc: ae0208b0 sw v0,2224(s0)
4005d0: 8e0208b0 lw v0,2224(s0)
4005d4: 8fbc0010 lw gp,16(sp)
4005d8: 8c590000 lw t9,0(v0)
4005dc: 00000000 nop
4005e0: 1720fff9 bnez t9,4005c8 <__do_global_dtors_aux+0x58>
4005e4: 24420004 addiu v0,v0,4
4005e8: 24020001 li v0,1
4005ec: a2220920 sb v0,2336(s1)
4005f0: 8fbf0020 lw ra,32(sp)
4005f4: 8fb1001c lw s1,28(sp)
4005f8: 8fb00018 lw s0,24(sp)
4005fc: 03e00008 jr ra
400600: 27bd0028 addiu sp,sp,40
00400604 <frame_dummy>:
400604: 3c1c0005 lui gp,0x5
400608: 279c82bc addiu gp,gp,-32068
40060c: 0399e021 addu gp,gp,t9
400610: 8f84801c lw a0,-32740(gp)
400614: 8f99803c lw t9,-32708(gp)
400618: 8c820884 lw v0,2180(a0)
40061c: 00000000 nop
400620: 10400005 beqz v0,400638 <frame_dummy+0x34>
400624: 24840884 addiu a0,a0,2180
400628: 13200003 beqz t9,400638 <frame_dummy+0x34>
40062c: 00000000 nop
400630: 03200008 jr t9
400634: 00000000 nop
400638: 03e00008 jr ra
40063c: 00000000 nop
00400640 <main>:
400640: 3c1c0005 lui gp,0x5
400644: 279c8280 addiu gp,gp,-32128
400648: 0399e021 addu gp,gp,t9
40064c: 27bdffd8 addiu sp,sp,-40
400650: afbf0020 sw ra,32(sp)
400654: afbc0018 sw gp,24(sp)
400658: 8f848018 lw a0,-32744(gp)#这里是将printf函数打印的字符串在GOT表中的地址加载到a0寄存器中。
40065c: 8f998040 lw t9,-32704(gp)#这里是将printf函数在GOT表中的地址加载到t9寄存器中。
400660: 24020001 li v0,1
400664: afa20010 sw v0,16(sp)
400668: 2484084c addiu a0,a0,2124
40066c: 24050006 li a1,6
400670: 24060002 li a2,2
400674: 0320f809 jalr t9
400678: 24070003 li a3,3
40067c: 8fbc0018 lw gp,24(sp)
400680: 8fbf0020 lw ra,32(sp)
400684: 24020006 li v0,6
400688: 03e00008 jr ra
40068c: 27bd0028 addiu sp,sp,40
00400690 <__libc_csu_fini>:
400690: 03e00008 jr ra
400694: 00000000 nop
00400698 <__libc_csu_init>:
400698: 3c1c0005 lui gp,0x5
40069c: 279c8228 addiu gp,gp,-32216
4006a0: 0399e021 addu gp,gp,t9
4006a4: 27bdffc8 addiu sp,sp,-56
4006a8: afbf0030 sw ra,48(sp)
4006ac: afb5002c sw s5,44(sp)
4006b0: afb40028 sw s4,40(sp)
4006b4: afb30024 sw s3,36(sp)
4006b8: afb20020 sw s2,32(sp)
4006bc: afb1001c sw s1,28(sp)
4006c0: afb00018 sw s0,24(sp)
4006c4: afbc0010 sw gp,16(sp)
4006c8: 8f998050 lw t9,-32688(gp)
4006cc: 0080a821 move s5,a0
4006d0: 00a0a021 move s4,a1
4006d4: 0320f809 jalr t9
4006d8: 00c09821 move s3,a2
4006dc: 8fbc0010 lw gp,16(sp)
4006e0: 00000000 nop
4006e4: 8f838020 lw v1,-32736(gp)
4006e8: 8f828020 lw v0,-32736(gp)
4006ec: 00000000 nop
4006f0: 00431023 subu v0,v0,v1
4006f4: 00029083 sra s2,v0,0x2
4006f8: 1240000b beqz s2,400728 <__libc_csu_init+0x90>
4006fc: 00608821 move s1,v1
400700: 00008021 move s0,zero
400704: 8e390000 lw t9,0(s1)
400708: 02a02021 move a0,s5
40070c: 02802821 move a1,s4
400710: 02603021 move a2,s3
400714: 0320f809 jalr t9
400718: 26100001 addiu s0,s0,1
40071c: 8fbc0010 lw gp,16(sp)
400720: 1650fff8 bne s2,s0,400704 <__libc_csu_init+0x6c>
400724: 26310004 addiu s1,s1,4
400728: 8fbf0030 lw ra,48(sp)
40072c: 8fb5002c lw s5,44(sp)
400730: 8fb40028 lw s4,40(sp)
400734: 8fb30024 lw s3,36(sp)
400738: 8fb20020 lw s2,32(sp)
40073c: 8fb1001c lw s1,28(sp)
400740: 8fb00018 lw s0,24(sp)
400744: 03e00008 jr ra
400748: 27bd0038 addiu sp,sp,56
40074c: 00000000 nop
00400750 <__do_global_ctors_aux>:
400750: 3c1c0005 lui gp,0x5
400754: 279c8170 addiu gp,gp,-32400
400758: 0399e021 addu gp,gp,t9
40075c: 27bdffd8 addiu sp,sp,-40
400760: afbf0020 sw ra,32(sp)
400764: afb1001c sw s1,28(sp)
400768: afb00018 sw s0,24(sp)
40076c: afbc0010 sw gp,16(sp)
400770: 8f82801c lw v0,-32740(gp)
400774: 2403ffff li v1,-1
400778: 24420878 addiu v0,v0,2168
40077c: 8c59fffc lw t9,-4(v0)
400780: 00000000 nop
400784: 13230008 beq t9,v1,4007a8 <__do_global_ctors_aux+0x58>
400788: 2450fffc addiu s0,v0,-4
40078c: 2411ffff li s1,-1
400790: 0320f809 jalr t9
400794: 2610fffc addiu s0,s0,-4
400798: 8e190000 lw t9,0(s0)
40079c: 8fbc0010 lw gp,16(sp)
4007a0: 1731fffb bne t9,s1,400790 <__do_global_ctors_aux+0x40>
4007a4: 00000000 nop
4007a8: 8fbf0020 lw ra,32(sp)
4007ac: 8fb1001c lw s1,28(sp)
4007b0: 8fb00018 lw s0,24(sp)
4007b4: 03e00008 jr ra
4007b8: 27bd0028 addiu sp,sp,40
4007bc: 00000000 nop
Disassembly of section .MIPS.stubs:
004007c0 <.MIPS.stubs>:
4007c0: 8f998010 lw t9,-32752(gp)
4007c4: 03e07821 move t7,ra
4007c8: 0320f809 jalr t9
4007cc: 24180007 li t8,7
4007d0: 8f998010 lw t9,-32752(gp)
4007d4: 03e07821 move t7,ra
4007d8: 0320f809 jalr t9
4007dc: 24180006 li t8,6
...
Disassembly of section .fini:
004007f0 <_fini>:
4007f0: 3c1c0005 lui gp,0x5
4007f4: 279c80d0 addiu gp,gp,-32560
4007f8: 0399e021 addu gp,gp,t9
4007fc: 27bdffe0 addiu sp,sp,-32
400800: afbf0018 sw ra,24(sp)
400804: afbc0010 sw gp,16(sp)
400808: 04110001 bal 400810 <_fini+0x20>
40080c: 00000000 nop
400810: 3c1c0005 lui gp,0x5
400814: 279c80b0 addiu gp,gp,-32592
400818: 039fe021 addu gp,gp,ra
40081c: 8f998018 lw t9,-32744(gp)
400820: 00000000 nop
400824: 27390570 addiu t9,t9,1392
400828: 0320f809 jalr t9
40082c: 00000000 nop
400830: 8fbc0010 lw gp,16(sp)
400834: 00000000 nop
400838: 8fbf0018 lw ra,24(sp)
40083c: 00000000 nop
400840: 03e00008 jr ra
400844: 27bd0020 addiu sp,sp,32
mips汇编完整程序的分析