linux - NASM scanf 未定义引用 (LINUX)

标签 linux assembly nasm

我是 assembly 新手。而且我的代码有问题。我正在尝试使用 scanf 创建简单的程序。

这是代码:

global main
extern printf
extern scanf

section .text

section .data
 message: db "The result is = %d", 10, 0
 request: db "Enter the number: ", 0
 integer1: times 4 db 0 ; 32-bits integer = 4 bytes
 formatin: db "%d", 0

main:
 ;  Ask for an integer
 push request
 call printf
 add esp, 4    ; remove the parameter

 push integer1 ; address of integer1, where the input is going to be stored
 push formatin ; arguments are right to left (first  parameter)
 call scanf
 add esp, 8    ; remove the parameters

 ; Move the value under the address integer1 to EAX
 mov eax, [integer1]

 ; Print out the content of eax register
 push rax
 push message
 call printf
 add esp, 8

 ;  Linux terminate the app
 MOV AL, 1
 MOV EBX, 0 
 INT 80h 

我编译它:

nasm -f elf64 -o program.o program.asm

和:

ld -o program program.o

但是当我尝试 ld 时出现错误:

program.o: In function `main':
program.asm:(.data+0x34): undefined reference to `printf'
program.asm:(.data+0x46): undefined reference to `scanf'
program.asm:(.data+0x5b): undefined reference to `printf'

我在 64 位 Linux 上工作。

感谢您的帮助。

最佳答案

您没有使用 ld 命令链接任何库。 scanfprintf 是在 C 库中定义的,因此您可以与之链接:

ld -o program program.o -lc

或者您可以使用其他定义这些函数的库(如果可用)。

关于linux - NASM scanf 未定义引用 (LINUX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33723751/

相关文章:

c - 如何在 C 中实现类似 nasm 的结构?

mysql - 从linux终端插入mysql

java.lang.UnsupportedClassVersionError : myApp : Unsupported major. 次要版本 51.0(无法加载类 myApp)在 Linux 上

c - RET 时出现段错误

程序集引导加载程序未跳转到内核

assembly - 计算机如何求模数?

汇编代码未按预期工作

尝试在 Assembly 中发表评论时 Emacs 的怪异

c - 使用 2 个线程 ID 的合并排序

c - 内存读/写操作中的总线错误