C中如何使用.asm文件

adreese 2008-03-29 12:56:30
我在源文件下 编写add_up.asm 文件,汇编的代码为
_TEXT SEGMENT BYTE PUBLIC'CODE'
_TEXT ENDS
_DATA SEGMENT WORD PUBLIC'DATA'
_DATA ENDS
CONST SEGMENT WORD PUBLIC'CONST'
CONST ENDS
_BSS SEGMENT WORD PUBLIC'BSS'
_BSS ENDS
ASSUME CS:_TEXT,DS:DGROUP;SS:DGROUP
_TEXT SEGMENT
ASSUME CS:_TEXT
PUBLIC _add_up
_add_up PROC NEAR
PUSH BP
MOV BP,SP
MOV AX,[BP+4];
ADD AX,[BP+8];
POP BP
RET
_add_up ENDP
_TEXT ENDS
END

是要实现两个数的相加,
C 的源文件代码为
#include <stdio.h>
#include <stdlib.h>

#include<stdio.h>

int add_up(int,int);
void main()
{
int a = 10,b=15,c;
c = add_up(a,b);
printf("a+b=%d\n",c);
}

编译出错,说add_up()函数没有声明,我初学内嵌汇编,不知道怎么弄。
...全文
1713 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
adreese 2008-03-29
  • 打赏
  • 举报
回复
谢谢,各位了。VC内嵌我知道方法,只是想弄清楚在TC中怎么用,还有有些代码在VC中是不能编译的,所以我就用tc了。
  • 打赏
  • 举报
回复
1:内嵌汇编,如1楼所写;
2:内联汇编,用汇编编译器将汇编源文件编译为目标文件(一般为OBJ后缀),用C编译器将C源文件也编译为目标文件,然后调用连接程序(如Link.exe)将两者的目标文件连接在一起。网上资料很多,这里就不举例了。
我啃 2008-03-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 adreese 的回复:]
楼上能给个具体的实例吗,你说内嵌汇编vc是 __asm{;code(ms style)} GCC是 asm(;code(at&t style)) 我都弄不明白,我想用TC编译器,当然我也有VC
[/Quote]
那个是我说的,TC已经不适合使用来开发了
VC用的是微软风格的汇编
GCC用的是AT&T风格的
VC:
__asm
{
mov eax,0
}
GCC:
asm(加代码);
adreese 2008-03-29
  • 打赏
  • 举报
回复
楼上能给个具体的实例吗,你说内嵌汇编vc是 __asm{;code(ms style)} GCC是 asm(;code(at&t style)) 我都弄不明白,我想用TC编译器,当然我也有VC
ttlyfast 2008-03-29
  • 打赏
  • 举报
回复

#define CALL_HOOK() asm("call _remcomHandler");

/* GDB stores segment registers in 32-bit words (that's just the way
m-i386v.h is written). So zero the appropriate areas in registers. */
#define SAVE_REGISTERS1() \
asm ("pushw %ds"); \
asm ("pushw %cs"); \
asm ("popw %ds"); \
asm ("movl %eax, _registers"); \
asm ("movl %ecx, _registers+4"); \
asm ("movl %edx, _registers+8"); \
asm ("movl %ebx, _registers+12"); \
asm ("movl %ebp, _registers+20"); \
asm ("movl %esi, _registers+24"); \
asm ("movl %edi, _registers+28"); \
asm ("movw $0, %ax"); \
asm ("popw %bx") ; /*old ds*/ \
asm ("movw %bx, _registers+48"); \
asm ("movw %ax, _registers+50"); \
asm ("movw %es, _registers+52"); \
asm ("movw %ax, _registers+54"); \
asm ("movw %fs, _registers+56"); \
asm ("movw %ax, _registers+58"); \
asm ("movw %gs, _registers+60"); \
asm ("movw %ax, _registers+62"); \
asm ("popw %bx"); /* old ip */ \
asm ("movw %bx, _registers+32"); \
asm ("movw %ax, _registers+34"); \
asm ("popw %bx"); /* old cs */ \
asm ("movw %bx, _registers+40"); \
asm ("movw %ax, _registers+42"); \
asm ("popw %bx"); /* old eflags */ \
asm ("movw %bx, _registers+36"); \
asm ("movw %ax, _registers+38"); \
/* Now that we've done the pops, we can save the stack pointer."); */ \
asm ("movw %ss, _registers+44"); /* ss */ \
asm ("movw %ax, _registers+46"); \
asm ("movl %esp, _registers+16"); /* esp */

void
handle_exception (int exceptionVector)
{
int sigval;
int addr, length, reg;
char *ptr;
int i ;
unsigned char ch ;

asm ("movw _registers+40 , %gs "); //cs
asm ("movw _registers+48 , %fs "); //ds

if( mode == BIG_MODE )
registers[PC] = (registers[CS] << 4) + registers[PC];
.......................
.......................
}


/*
* remcomHandler is a front end for handle_exception. It moves the
* stack pointer into an area reserved for debugger use.
*/
asm ("_remcomHandler:");
asm (" popl %eax"); /* pop off return address .pop ip */
asm (" popl %eax"); /* get the exception number */
asm ("mov %cs , %bx");
asm ("mov %bx , %es");
asm ("mov %bx , %ss");
asm (" movl _stackPtr, %esp"); /* move to remcom stack area */
asm (" pushl %eax"); /* push exception onto stack */
asm (" call _handle_exception"); /* this never returns */


extern void catchException4 ();
asm (".text");
asm (".globl _catchException4");
asm ("_catchException4:");
SAVE_REGISTERS1 ();
asm ("pushl $4"); /*push 4*/
CALL_HOOK ();
我啃 2008-03-29
  • 打赏
  • 举报
回复
如果要用汇编可以:
1.内嵌汇编vc是 __asm{;code(ms style)} GCC是 asm(;code(at&t style))
2.编译.inc在链接的时候指定连接然后定义头文件给C用
adreese 2008-03-29
  • 打赏
  • 举报
回复
还有上面你给的代码,在VC中编译通过,但是在TC中就不行了
adreese 2008-03-29
  • 打赏
  • 举报
回复
谢谢 * HelloDan
但是我想知道怎么使用asm文件,不想在代码中嵌入,。


HelloDan 2008-03-29
  • 打赏
  • 举报
回复

#include<stdio.h>



int main()
{
int ia=90;
int ib=23;
_asm
{
push eax
mov eax,ia
add eax,ib
mov ia,eax
pop eax
}
printf("%d\n",ia);
return 0;
}

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧