AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > 汇编语言

汇编与C语言配合

51自学网 http://www.51zixue.net

1、先准备两个程序,一个汇编、一个C语言

  在汇编中没有定义变量,因为在一个模块中不会有问题;
  在C中定义了两个函数,一些局部变量,一些全局变量;
  这样我们要考虑的内容都完备了。
   ms.asm                    mc2.c                      
                                                   
  .386                       int sum(int i){          
  .model flat                int k = i;              
  extrn c m:near             int j = 0;              
  public  _start             int s = 0;              
  .code                      for(j=1;j<=k;j++) s+=j;  
_start:                 return s;                
  mov ax,cs             }                        
  mov ds,ax                                      
  call m                  int e = 1;              
stop:                      int f = 2;              
  jmp stop                int ar[6000000L];      
   end  _start                                          
                               extern void m(){         
                                  int d;                  
                                  long c;                  
                            c=1;                    

2、分别编译成obj文件

  ml /c /coff ms.asm                     //指定生成coff格式的obj文件
  cl /c /Fomc.obj   mc2.c                //指定生成的obj文件名为mc.obj
  link /subsystem:windows ms.obj mc.obj  //这里使用32位的链接器,要设好lib路径

  现在得到ms.obj  mc.obj ms.exe  三个文件

3、分析一下源代码,显然程序入口点是_start(在使用/coff参数进行编译时必须有下划线),在汇编中

调用了C中的m函数,这是需要重定位的。在C中m调用了sum函数,这也是要重定位的。

4、现在利用VC6自带的dumpbin.exe工具,生成解析文件:

  dumpbin /all ms.obj>msobj.txt
  dumpbin /all mc.obj>mcobj.txt
  dumpbin /all ms.exe>msexe.txt

  现在得到三个解析文件,下面逐个分析
*******************************************************************************
*msobj.txt
*******************************************************************************
Microsoft (R) COFF Binary File Dumper Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.


Dump of file ms.obj

File Type: COFF OBJECT

FILE HEADER valueS
            14C machine (i386)       //机器类型为386
              3 number of sections   //ms.obj文件有三节
       41AABB57 time date stamp Mon Nov 29 14:01:59 2004
             B2 file pointer to symbol table  //符号表的文件偏移是 0B2H
              B number of symbols             //共 0BH=11 个符号
              0 size of optional header
              0 characteristics

//第一节的头部
SECTION HEADER #1
  .text name
      0 physical address
      0 virtual address
      D size of raw data                   //原始数据长度为 0DH=13 个字节
     8C file pointer to raw data           //其在文件内的偏移为 8Ch
     9A file pointer to relocation table   //其重定位表在文件内的偏移为9Ah
      0 file pointer to line numbers
      1 number of relocations              //需重定位的项有 1 项
      0 number of line numbers
60300020 flags
        Code                               //这是一个代码段
        4 byte align
        Execute Read

RAW DATA #1                                //这里列出了原始数据,恰好 13=0DH 个字节
 00000000: 66 8C C8 66 8E D8 E8 00 00 00 00 EB FE           f..f.........
                                |-->这是偏移7的位置,查下面的重定位表知道它需要重定位。
                                    当前值是 00 00 00 00 ,E8代表call


RELOCATIONS #1                             //这是重定位表
                                               Symbol    Symbol
Offset    Type              Applied To         Index     Name
--------  ----------------  -----------------  --------  ------
00000007  REL32                      00000000         7  _m     //清楚的指出_m需要重定位
   /                                                   /
    /-->在原始数据内的偏移是7                           /
                                                         /-->7表示_m在符号表中的索引号

//第二节的头部
SECTION HEADER #2
  .data name
      D physical address
      0 virtual address
      0 size of raw data
      0 file pointer to raw data
      0 file pointer to relocation table
      0 file pointer to line numbers
      0 number of relocations
      0 number of line numbers
C0300040 flags
        Initialized Data      //这节是初始化的数据段,也就是全局变量段,
        4 byte align          //上面所有的项都是0,说明汇编中没有定义全局变量
        Read Write            //注意,汇编中定义的_start是全局标号,并不是变量!!!


//第三节的头部
SECTION HEADER #3
.drectve name
      D physical address
      0 virtual address
      D size of raw data                  //原始数据共 0Dh
     A4 file pointer to raw data          //在obj文件中的偏移为0A4h
      0 file pointer to relocation table
      0 file pointer to line numbers
      0 number of relocations
      0 number of line numbers
    A00 flags
        Info                      //表明这只是一个信息段,即不是数据也不是代码,
        Remove                    //只是用来说明某种支持信息
        (no align specified)

RAW DATA #3              //看一下原始数据,原来是说明程序的入口点是_start,完全正确
 00000000: 2D 65 6E 74 72 79 3A 73 74 61 72 74 20           -entry:start

  Linker Directives
  -----------------
  -entry:start


//符号表
COFF SYMBOL TABLE
000 00000000 DEBUG  notype       Filename     | .file
   ms.asm
002 001220FC ABS    notype       Static       | @comp.id
003 00000000 SECT1  notype       Static       | .text
   Section length    D, #relocs    1, #linenums    0, checksum        0
005 00000000 SECT2  notype       Static       | .data
   Section length    0, #relocs    0, #linenums    0, checksum        0
007 00000000 UNDEF  notype ()    External     | _m
008 00000000 SECT1  notype ()    External     | start
009 00000000 SECT3  notype       Static       | .drectve
   Section length    D, #relocs    0, #linenums    0, checksum        0

//可以看到_m被说明为未定义(UNDEF)、外部变量(External)、是个函数 ( () )
//start定义在节1中(SECT1)、是个函数(())、可供外部使用(External)


//字符串信息为0,即不存在
String Table Size = 0x0 bytes

 Summary

          0 .data
          D .drectve
          D .text
******************************************************************************
*mc.obj
******************************************************************************
Microsoft (R) COFF Binary File Dumper Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.


Dump of file mc.obj

File Type: COFF OBJECT

FILE HEADER valueS
            14C machine (i386)           //机器类型为386
              2 number of sections       //mc.obj文件有2节
       41AABA2D time date stamp Mon Nov 29 13:57:01 2004
            158 file pointer to symbol table   //符号表的文件偏移是 158H
              C number of symbols              //共 0CH=12 个符号
              0 size of optional header
              0 characteristics


//第一节的头部
SECTION HEADER #1
.drectve name
      0 physical address
      0 virtual address
     26 size of raw data                     //原始数据长充为 26H=38 个字节
     64 file pointer to raw data             //其在文件内的偏移为 64h
      0 file pointer to relocation table
      0 file pointer to line numbers
      0 number of relocations
      0 number of line numbers
 100A00 flags
        Info                                 //这是一个信息段
        Remove
        1 byte align

//第一节的原始数据
RAW DATA #1                                               //原来是说明默认库的信息
 00000000: 2D 64 65 66 61 75 6C 74 6C 69 62 3A 4C 49 42 43  -defaultlib:LIBC
 00000010: 20 2D 64 65 66 61 75 6C 74 6C 69 62 3A 4F 4C 44   -defaultlib:OLD
 00000020: 4E 41 4D 45 53 20                                NAMES

  Linker Directives
  -----------------
  -defaultlib:LIBC
  -defaultlib:OLDNAMES


//第二节的头部
SECTION HEADER #2
  .text name
      0 physical address
      0 virtual address
     A6 size of raw data                  //原始数据长充为 0A6H=166 个字节
     8A file pointer to raw data          //其在文件内的偏移为 8Ah
    130 file pointer to relocation table  //其重定位表在文件内的偏移为130h
      0 file pointer to line numbers
      4 number of relocations             //需重定位的项有4项
      0 number of line numbers
60500020 flags
        Code                              //这是一个代码段
        16 byte align                     //对齐方式是以16个字节的小段边缘对齐
        Execute Read                      //该代码 可读、可执行
                                          //这点可通过编译参数/SECTION来改变

//第二节的原始数据
//使用W32Dasm打开mc.obj文件,输入偏移地址为8Ah(见第二节的头部说明),反编译下面这段
//与汇编生成的lst文件对比,可以看出下面的数据从偏移0开始的55 8B到偏移44H的5D C3是sum
//函数的数据。紧跟其后直至最后的是函数 m 的代码
//这里可以看出,32位编译器把所有的代码按它们在源代码中出现的顺序“堆积”在obj文件中

RAW DATA #2
 00000000: 55 8B EC 83 EC 0C 8B 45 08 89 45 F4 C7 45 F8 00  U......E..E..E..
 00000010: 00 00 00 C7 45 FC 00 00 00 00 C7 45 F8 01 00 00  ....E......E....
 00000020: 00 EB 09 8B 4D F8 83 C1 01 89 4D F8 8B 55 F8 3B  ....M.....M..U.;
 00000030: 55 F4 7F 0B 8B 45 FC 03 45 F8 89 45 FC EB E4 8B  U....E..E..E....
 00000040: 45 FC 8B E5 5D C3 55 8B EC 83 EC 08 C7 45 FC 01  E...].U......E..
 00000050: 00 00 00 C7 45 F8 01 00 00 00 C7 05 00 00 00 00  ....E...........
 00000060: 01 00 00 00 C7 05 00 00 00 00 01 00 00 00 6A 05  ..............j.
 00000070: E8 00 00 00 00 83 C4 04 C7 45 FC 00 00 00 00 EB  .........E......
 00000080: 09 8B 45 FC 83 C0 01 89 45 FC 81 7D FC 80 Cheesy 5B  ..E.....E..}.
 

上一篇:举例说明在汇编语言中,&quot;[]&quot;的用法  下一篇:用汇编语言实现RESET启动和热启动