我是 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 命令链接任何库。 scanf
和 printf
是在 C 库中定义的,因此您可以与之链接:
ld -o program program.o -lc
或者您可以使用其他定义这些函数的库(如果可用)。
关于linux - NASM scanf 未定义引用 (LINUX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33723751/