• C语言与汇编语言相互调用原理以及实例


    C语言与汇编语言相互调用原理以及实例

    1.原理

    其实不管是C语言还是汇编语言想要执行都是最终编译链接成为二进制文件。
    这里一定要明确编译和链接是两个步骤,生成的文件格式也是不一样的。
    编译生成的文件是一定格式的,里面包括函数符号表、参数表...等信息,这些信息主要是提供给链接阶段使用,函数调用是怎么调用的?是不是指定利用的函数的符号?所以链接阶段就是将函数调用的符号变成相对地址(要特别注意这个阶段,因为这个过程使得C语言和汇编语言相互调用成为可能)

    2.下面两个分别是一个hello.asm(汇编语言文件),main.c(c语言文件)

    ;hello.asm
    extern print_helloworld  
    [section .text]  
    global print_two_hello_world  
    print_two_hello_world:    
    call print_helloworld  
    call print_helloworld  
    
    /* main.c */
    #include "stdio.h"  
    
    extern void print_two_hello_world();  
      
    char *strhello = "Hello,world!
    ";  
      
    void print_helloworld ()  
    {  
            printf ("%s",strhello);  
    }  
      
    int  main ()  
    {  
            print_two_hello_world();  
            return 0;  
    }  
    

    首先看NASM代码,先导入一个外部函数print_helloworld(),此函数是C语言定义的一个函数。接下来是定义了一个函数print_two_hello_world,用global关键字导出使其可以在C中调用,函数的内容是调用了两次print_helloword()。
    在来看C代码,代码很简单就具体讲了,这里主要数一下我们将主函数放在了C代码中写,这时因为我们要使用C代码的函数库中的printf()函数,如果C代码中没有什么库的依赖可以将主函数放在汇编代码中

    3.NASM和C互相调用对方的变量

    Nasm代码

     global string  
    extern strhello  
    [section .data]  
    string:  
        db 'I am Chinese.',0x0A,0x0  
    [section .text]  
        global print_hello  
        global cpy_mem  
    print_hello:      
        mov edx, 13  
        mov ecx,[strhello]  
        mov ebx,1  
        mov eax,4  
        int 0x80  
    

    C代码

    #include "stdio.h"  
    #include "string.h"  
    extern char *string;  
    extern void print_hello();  
    extern cpy_mem (void *dest, int len);  
    char *strhello = "Hello,world!
    ";  
    char *str = NULL;  
    int  
    main ()  
    {  
            printf ("%x
    ",&string);  
            str = &string;  
            printf ("%s", str);  
             
            print_hello ();  
            return 0;  
    }  
    

    Make后的执行结果如下:
    I am Chinese.
    Hello,world!
    具体的代码内容就部分析了,这里主要将两个关键的地方。

    1.在C语言中定义了一个strhello的字符串变量,在C语言中strhello表示的是字符串的首地址,比如字符串的地址是0xa00001,而strhello是个指针即4字节其地址为0xb00001, 在C语言中strhello表示的值是 0xa00001 字符串的首地址,但到了NASM中则表示的 strhello变量的首地址了 0xb00001,所以

    Nasm代码

    mov ecx,[strhello]  
    

    代码中加了中括号表示是内容,这一点一定要注意,否则会出错!!

    2.第二点要注意的是,在NASM中定义了一个字符串string , 在C语言中导入的话,就是表示字符串的首地址,因此要引用该字符串一定要取其地址,不要直接转为(char*)类型直接用,否则出错,这是因为直接转的话,就将字符串的前4个字节转成了字符串指针,但该指针是不确定会出现段错误!

  • 相关阅读:
    maven私服
    docker
    mysql ip
    jenkins安装
    centeros7防火墙操作
    centeros7 gitlap安装
    nexus安装及使用(maven私服掌握)
    idea永久激活使用
    redis密码验证
    Nginx服务优化配置
  • 原文地址:https://www.cnblogs.com/liubiyonge/p/9415137.html
Copyright © 2020-2023  润新知