开发者

Having trouble calling C functions in NASM

开发者 https://www.devze.com 2023-03-01 15:19 出处:网络
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:

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 % bas开发者_Python百科e];

    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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消