【问题标题】:I keep getting an error when using nasm on assembly language x86 on Ubuntu [duplicate]在 Ubuntu 的汇编语言 x86 上使用 nasm 时,我不断收到错误 [重复]
【发布时间】:2017-03-08 03:48:15
【问题描述】:
extern putchar, getchar, printf
global main
SECTION .data
fmt: db “characters = %d", 10,0
SECTION .bss
SECTION .text
global main
main:
xor eax, eax
xor ebx, ebx
start:
call getchar
cmp eax, -1   
jle exit
inc ebx;
cmp eax, "A"   
jl print
cmp eax, "z"
jg print
cmp eax, "Z"
jle rotup
cmp eax, "a"
jge rotlow
jmp print
rotup:
cmp eax, "M"
jle add13
sub eax, 13
jmp print
rotlow:
cmp eax, "m"
jle add13
sub eax, 13
jmp print
add13:
add eax,13
jmp print
print:
push eax;
call putchar
add esp,4
jmp start
exit:
push ebx
push fmt
call printf
add esp,8
ret

好的。所以我一直在使用这个程序来为我的一个课程运行 Vigenere Cipher,当我尝试运行 nasm 时,它在第一部分运行良好

nasm -f elf cipher.asm

但是当我尝试时

ld -o cipher cipher.o 

继续它给了我

ciper.o: In function 'start':
cipher.asm:(.text+0x5): undefined reference to 'getchar'
cipher.o: In function 'print':
cipher.asm:(.text+0x40): undefined reference to 'putchar'
cipher.o: IN function 'exit':
cipher.asm:(.text+0x50):undefined reference to 'printf'

我不知道为什么它给了我这个,我认为这就是 extern 的重点。另外,如果您想知道我在使用什么我在 64 位上运行 Ubuntu。 我试过阅读如何解决这个问题,但我似乎找不到对我有帮助的。

编辑:现在我正在尝试使用 gcc 而不是 ld 来链接但是当我使用时

gcc -o output cipher.o

或类似的东西给我

/usr/bin/ld: i386 architecture of input file 'cipher.o' is incompatible with i386:x86-64 
output

它仍然给了我未定义的引用,我不知道该怎么做或如何链接它,所以我可以在代码中使用 C 函数。

【问题讨论】:

    标签: ubuntu assembly nasm x86-64 ld


    【解决方案1】:

    我看到两个问题:

    1. 看起来您正在调用 C 库函数 getcharputcharprintf,但您没有在 ld 命令行上链接 C 库。

    2. C 编译器倾向于在符号名称前加上下划线。大多数汇编程序不这样做,因此您可能需要调用_getchar_putchar_printf

    【讨论】:

    • 我尝试过使用 _getchar、_putchar 和 _printf,但它仍然告诉我同样的事情,但只是将 _ 包含在错误中
    • 我如何在 C 库中链接到 'ld' 命令行
    • Linux ELF 系统不会破坏带有前导 _ 的名称。不过,您可能对缺少的-lc 是正确的。
    • 那么我的 ld 命令行应该是什么样子?
    猜你喜欢
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    相关资源
    最近更新 更多