gcc中调用nasm编写的函数

elftest.asm

; test source file for assembling to ELF
; build with:
;    nasm -f elf elftest.asm
;    gcc -m32 -o elftest elftest.c elftest.o
; (assuming your gcc is ELF)

; This file should test the following:
; [1] Define and export a global text-section symbol
; [2] Define and export a global data-section symbol
; [3] Define and export a global BSS-section symbol
; [4] Define a non-global text-section symbol
; [5] Define a non-global data-section symbol
; [6] Define a non-global BSS-section symbol
; [7] Define a COMMON symbol
; [8] Define a NASM local label
; [9] Reference a NASM local label
; [10] Import an external symbol
; [11] Make a PC-relative call to an external symbol
; [12] Reference a text-section symbol in the text section
; [13] Reference a data-section symbol in the text section
; [14] Reference a BSS-section symbol in the text section
; [15] Reference a text-section symbol in the data section
; [16] Reference a data-section symbol in the data section
; [17] Reference a BSS-section symbol in the data section
; [18] Define a non-global rodata-section symbol

	  BITS 32
	  GLOBAL lrotate	; [1]
	  GLOBAL greet		; [1]
	  GLOBAL asmstr		; [2]
	  GLOBAL textptr	; [2]
	  GLOBAL selfptr	; [2]
	  GLOBAL integer	; [3]
	  EXTERN printf		; [10]
	  COMMON commvar 4	; [7]

	  SECTION .text

; prototype: long lrotate(long x, int num);
lrotate:			; [1]
	  push ebp
	  mov ebp,esp
	  mov eax,[ebp+8]
	  mov ecx,[ebp+12]
.label	  rol eax,1		; [4] [8]
	  loop .label		; [9] [12]
	  mov esp,ebp
	  pop ebp
	  ret

; prototype: void greet(void);
greet	  mov eax,[integer]	; [14]
	  inc eax
	  mov [localint],eax	; [14]
	  push dword [commvar]
	  mov eax,[localptr]	; [13]
	  push dword [eax]
	  push dword [integer]	; [1] [14]
	  push dword printfstr	; [13]
	  call printf		; [11]
	  add esp,16
	  ret

	  SECTION .data

; a string
asmstr	  db 'hello, world', 0	; [2]

; a string for Printf
printfstr db "integer==%d, localint==%d, commvar=%d"
	  db 10, 0

; some pointers
localptr  dd localint		; [5] [17]
textptr	  dd greet		; [15]
selfptr	  dd selfptr		; [16]

	  SECTION .bss

; an integer
integer	  resd 1		; [3]

; a local integer
localint  resd 1		; [6]

	  SECTION .rodata
readonly  dd readonly		; [18]

c代码elftest.c

/*
 * test source file for assembling to ELF
 * build with:
 *    nasm -f elf elftest.asm
 *    gcc -m32 -o elftest elftest.c elftest.o
 * (assuming your gcc is ELF)
 */

#include <stdio.h>
#include <inttypes.h>

extern int lrotate(int32_t, int);
extern void greet(void);
extern int8_t asmstr[];
extern void *selfptr;
extern void *textptr;
extern int integer, commvar;

int main(void)
{

    printf("Testing lrotate: should get 0x00400000, 0x00000001\n");
    printf("lrotate(0x00040000, 4) = 0x%08x\n", lrotate(0x40000, 4));
    printf("lrotate(0x00040000, 14) = 0x%08x\n", lrotate(0x40000, 14));

    printf("This string should read `hello, world': `%s'\n", asmstr);

    printf("The integers here should be 1234, 1235 and 4321:\n");
    integer = 1234;
    commvar = 4321;
    greet();

    printf("These pointers should be equal: %p and %p\n", &greet, textptr);

    printf("So should these: %p and %p\n", selfptr, &selfptr);

    return 0;
}

编译运行

$ nasm -f elf32 elftest.asm
$ gcc -m32 -o elftest elftest.c elftest.o
$ ./elftest 
Testing lrotate: should get 0x00400000, 0x00000001
lrotate(0x00040000, 4) = 0x00400000
lrotate(0x00040000, 14) = 0x00000001
This string should read `hello, world': `hello, world'
The integers here should be 1234, 1235 and 4321:
integer==1234, localint==1235, commvar=4321
These pointers should be equal: 0x8048541 and 0x8048541
So should these: 0x804a05c and 0x804a05c

 

转载于:https://my.oschina.net/u/2245781/blog/1036224

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值