在 NASM 中调用 C 函数时遇到问题
我正在创建一个 NASM 程序,但我在 NASM 代码中调用 C 函数来简化我的生活。但我收到未定义的参考错误。我做错了什么?下面是代码:
我用来编译的命令行命令
nasm -fwin32 calculator.asm
gcc - Wall -c print.c -o print.obj
$ ld calculator.obj print.obj -o calculator.exe
calculator.obj:calculator.asm:(.text+0x6): undefined reference to `print'
calculator.obj:calculator.asm:(.text+0x15): undefined reference to `sum'
calculator.obj:calculator.asm:(.text+0x24): undefined reference to `int2string'
calculator.obj:calculator.asm:(.text+0x2a): undefined reference to `print'
calculator.obj:calculator.asm:(.text+0x2f): undefined reference to `ps'
print.obj:print.c:(.text+0xd): undefined reference to `printf'
print.obj:print.c:(.text+0x20): undefined reference to `atoi'
print.obj:print.c:(.text+0x8b): undefined reference to `printf'
print.obj:print.c:(.text+0xbe): undefined reference to `system'
平台
Windows 7 64位,但我正在32位编译。这应该不是问题;我认为。
C 代码
#include <stdio.h>
#include <stdlib.h>
//Prototypes
void _print(char* string);
int _string2int(char* string);
char* _int2string(int i);
int _sum(int x, int y);
int _sub(int x, int y);
int _divide(int x, int y);
int _multiply(int x, int y);
void _pause();
char* itoa(int val, int base);
void print(char* string)
{
printf(string);
}
int string2int(char* string)
{
return atoi(string);
}
char* int2string(int i)
{
return itoa(i, 10);
}
int subtract(int x, int y)
{
return x + y;
}
int sub(int x, int y)
{
return x - y;
}
int divide(int x, int y)
{
if (y != 0)
{
return (x / y);
}
else if (y != 0)
{
printf("Infinity!");
return 0;
}
return 0;
}
int multiply(int x, int y)
{
return x * y;
}
void ps()
{
system("Pause");
}
char* itoa(int val, int base){
static char buf[32] = {0};
int i = 30;
for(; val && i ; --i, val /= base)
buf[i] = "0123456789abcdef"[val % base];
return &buf[i+1];
}
NASM 代码
global _main
extern print
extern sum
extern subtract
extern divide
extern string2int
extern int2string
extern ps
section .data
;Constant
message: db 'The sum of 2 + 2 is ', 0
section .bss
;Variables
answer: resb 255
stranswer: resb 255
section .text
_main:
;Call the print function and print the variable named message.
push message
call print
;Add 2 and 2 and the return value gets placed in the eax register.
push 2
push 2
call sum
mov [answer], eax
push answer
call int2string
push eax
call print
;Pauses the console window.
call ps
结论:很抱歉这篇文章很长
I am creating a NASM program but I am calling C function in my NASM code to simplify my life. But I get undefined reference errors. What have I done wrong? Here is the code below:
Command line commands that I used to compile
nasm -fwin32 calculator.asm
gcc - Wall -c print.c -o print.obj
$ ld calculator.obj print.obj -o calculator.exe
calculator.obj:calculator.asm:(.text+0x6): undefined reference to `print'
calculator.obj:calculator.asm:(.text+0x15): undefined reference to `sum'
calculator.obj:calculator.asm:(.text+0x24): undefined reference to `int2string'
calculator.obj:calculator.asm:(.text+0x2a): undefined reference to `print'
calculator.obj:calculator.asm:(.text+0x2f): undefined reference to `ps'
print.obj:print.c:(.text+0xd): undefined reference to `printf'
print.obj:print.c:(.text+0x20): undefined reference to `atoi'
print.obj:print.c:(.text+0x8b): undefined reference to `printf'
print.obj:print.c:(.text+0xbe): undefined reference to `system'
Platform
Windows 7 64-bit but I am compiling in 32-bit. Which shouldn't be problem; I think.
C Code
#include <stdio.h>
#include <stdlib.h>
//Prototypes
void _print(char* string);
int _string2int(char* string);
char* _int2string(int i);
int _sum(int x, int y);
int _sub(int x, int y);
int _divide(int x, int y);
int _multiply(int x, int y);
void _pause();
char* itoa(int val, int base);
void print(char* string)
{
printf(string);
}
int string2int(char* string)
{
return atoi(string);
}
char* int2string(int i)
{
return itoa(i, 10);
}
int subtract(int x, int y)
{
return x + y;
}
int sub(int x, int y)
{
return x - y;
}
int divide(int x, int y)
{
if (y != 0)
{
return (x / y);
}
else if (y != 0)
{
printf("Infinity!");
return 0;
}
return 0;
}
int multiply(int x, int y)
{
return x * y;
}
void ps()
{
system("Pause");
}
char* itoa(int val, int base){
static char buf[32] = {0};
int i = 30;
for(; val && i ; --i, val /= base)
buf[i] = "0123456789abcdef"[val % base];
return &buf[i+1];
}
NASM Code
global _main
extern print
extern sum
extern subtract
extern divide
extern string2int
extern int2string
extern ps
section .data
;Constant
message: db 'The sum of 2 + 2 is ', 0
section .bss
;Variables
answer: resb 255
stranswer: resb 255
section .text
_main:
;Call the print function and print the variable named message.
push message
call print
;Add 2 and 2 and the return value gets placed in the eax register.
push 2
push 2
call sum
mov [answer], eax
push answer
call int2string
push eax
call print
;Pauses the console window.
call ps
Conclusion: Sorry for the long post
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 ps 函数未声明且 sum 未定义。有两个函数sub和subtract。我猜你希望其中之一是总和。最后四个是由于没有链接 libc 造成的。
Your ps function is not declared and sum is not defined. There are two functions sub and subtract. I guess you wanted one of them to be sum. The last four are due to not linking libc.