简体   繁体   English

通过在寄存器中传递函数地址,从asm文件中调用C函数

[英]Call C function from asm file by passing function address in a register

I have a function named system_handler which is defined in a func.c . 我有一个名为system_handler的函数,该函数在func.c定义。 I need to call system_handler from another assembly program by passing the address of the function to a register and call the register. 我需要通过将函数的地址传递给寄存器并调用该寄存器,从另一个汇编程序调用system_handler

So far I have written this: 到目前为止,我已经写了这个:

       .extern system_handler   ; Is defined in func.c
       mov system_handler, %eax
       call %eax   ; This call is making run time error in emulator 
                   ; fatal: Trying to execute code outside RAM or ROM at 0x8b1cec83

While assembling the asm file I am getting a warning: 组装asm文件时,我收到警告:

Warning: indirect call without '*'

compiler and assembler used: i586-gnu-{gcc/as} , Using AT&T format in asm file. 使用的编译器和汇编器: i586-gnu-{gcc/as} ,在asm文件中使用AT&T格式。

In AT&T syntax you should use mov $system_handler, %eax to load the address, what you did was loading the first dword of your function from memory. 在AT&T语法中,应使用mov $system_handler, %eax加载地址,所做的就是从内存中加载函数的第一个dword。

To fix the warning, you should of course write call *%eax , which is another peculiarity of AT&T. 要解决该警告,您当然应该编写call *%eax ,这是AT&T的另一个特点。

If you are not familiar with AT&T syntax, you can switch both gcc and gas to intel syntax. 如果您不熟悉AT&T语法,则可以将gccgas都切换为intel语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM