netbeans c/c++ (or gcc) & nasm co-work
2016-01-08 12:52
295 查看
1. assemble asm to obj
run: nasm -felf64 maxofthree.asm will generate maxofthree.o
2.
run: ar -r libmyasm.a maxofthree.o generate libmyasm.a (if use gcc, not Netbeans, this step is not needed.)
3.
Netbeans:
right-click project->properties->Build->Linker->Libraries->Add Library File(Not "Add Library"), Select File type "Static Library(.a)", add libmyasm.a.
(If use gcc, run: gcc maxofthree.o callmaxofthree.c will generate a.out, run it : ./a.out)
Now , it works!!!
run: nasm -felf64 maxofthree.asm will generate maxofthree.o
2.
run: ar -r libmyasm.a maxofthree.o generate libmyasm.a (if use gcc, not Netbeans, this step is not needed.)
3.
Netbeans:
right-click project->properties->Build->Linker->Libraries->Add Library File(Not "Add Library"), Select File type "Static Library(.a)", add libmyasm.a.
(If use gcc, run: gcc maxofthree.o callmaxofthree.c will generate a.out, run it : ./a.out)
Now , it works!!!
; ----------------------------------------------------------------------------- ; A 64-bit function that returns the maximum value of its three 64-bit integer ; arguments.The function has signature: ; ;int64_t maxofthree(int64_t x, int64_t y, int64_t z) ; ; Note that the parameters have already been passed in rdi, rsi, and rdx. ; We just have to return the value in rax. ; ----------------------------------------------------------------------------- global maxofthree section .text maxofthree: mov rax, rdi ; result (rax) initially holds x cmp rax, rsi ; is x less than y? cmovl rax, rsi ; if so, set result to y cmp rax, rdx ; is max(x,y) less than z? cmovl rax, rdx ; if so, set result to z ret ; the max will be in rax
/* * A small program that illustrates how to call the maxofthree function we wrote in * assembly language. */ #include <stdio.h> #include <inttypes.h> #include <sys/time.h> int64_t maxofthree(int64_t, int64_t, int64_t); int64_t maxofthree_c(int64_t a, int64_t b, int64_t c) { int64_t tmp = (a > b ? a : b); return (tmp > c ? tmp : c); } static int count = 100000000; static int64_t a = -10330000000, b = 2003000033, c = 100000003330000; void asm_fun() { struct timeval start; struct timeval end; unsigned long timer; int i = 0; gettimeofday(&start,NULL); for( i = 0; i < count; i++) { maxofthree(a, b, c); } gettimeofday(&end,NULL); timer = 1000000 * (end.tv_sec-start.tv_sec)+ end.tv_usec-start.tv_usec; printf("comparing %d times (assembly) costs %ld us\n", count, timer); } void c_fun() { struct timeval start; struct timeval end; unsigned long timer; int i = 0; gettimeofday(&start,NULL); for(i = 0; i < count; i++) { maxofthree_c(a + i, b - i, c + i); } gettimeofday(&end,NULL); timer = 1000000 * (end.tv_sec-start.tv_sec)+ end.tv_usec-start.tv_usec; printf("comparing %d times (c) costs %ld us\n", count, timer); } int main() { asm_fun(); c_fun(); asm_fun(); c_fun(); return 0; }
相关文章推荐
- C语言学习之关键字第九讲
- C语言tips不完全汇总
- c++ new
- C++处理一个动态规划的问题
- c++实现gray code(格雷码)
- C++ STL之stack
- c语言位域
- c++ 函数调用在进入下一个循环的时候会再次初始化参数,将函数体直接写进去就正常
- c++中string类型用下标初始化后str.size()为0 输出string值为空
- C++中尖括号和引号的区别---转载
- C++中使用array报错 requires compiler and library surpport for the ISO c++ 2011 standard
- C++中getline被跳过
- c++11move语意的便捷理解
- 指向函数的指针数组
- C++11中的右值引用及move语义编程
- #if defined和#if !defined(c语言的宏定义)
- C++开发人脸性别识别——通过SVM实现性别识别
- 【转】C++成员变量的初始化顺序问题
- C++入门(10):高级OOP技术:静态方法和属性
- 实例15_C语言绘制万年历