NASM 2 lines of db (initialized data) seemingly not working

拜拜、爱过 提交于 2019-12-02 09:49:37

问题


I have the following x86-64 code, which I can run on OSX Yosemite:

global _main
extern _exit
extern _puts

DEFAULT REL

section .data

putsmsg:    db      'Puts message...',0
another:    db      0

section .text

_main:
    push        rbp
    mov         rbp, rsp

    ; print a string using PUTS
    lea         rdi, [putsmsg]
    call        _puts

    ; call EXIT(0) c function
    mov         rdi, 0
    call        _exit

I compile, link, and run as follows (where the source is a.asm):

nasm -f macho64 a.asm ; gcc a.o -o a.bin ;./a.bin

It does not print the message 'Puts message...', whereas it does print the message if I simply comment out the line containing the label 'another'. What is going wrong here? Why doesn't it print the correct string when I have the other initialized data line?


回答1:


Works for me on GNU/Linux, with yasm. (After removing the _ prefix from function names.)

$ yasm -felf64 puts.asm
$ gcc -o put puts.o
$ ./puts
Puts message...

That's with the 2nd db line still present:

another:    db      0

There was another question about almost exactly this problem (a message not printing when there was more data following the string) on OS X, within the past couple weeks. The OP of that question found that yasm on OS X worked for him. Maybe there's a bug in nasm for OS X?

I eventually found the post I was talking about: Position Independent Code pointing to wrong address. See the discussion in comments on my answer. The OP found his code worked with yasm, but wasn't able to tell if there was a bug in nasm or if he was just using it wrong.



来源:https://stackoverflow.com/questions/31795979/nasm-2-lines-of-db-initialized-data-seemingly-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!