0

In a course about C/C++ , I have the following assembly code named hello-x64.S :

.text
    .global _start

_start:

        movl    $len,%edx
        movl    $msg,%ecx
        movl    $1,%ebx
        movl    $4,%eax
        int     $0x80

        movl    $0,%ebx
        movl    $1,%eax
        int     $0x80

.data
msg:
        .ascii  "Bonjour le monde!\n"
        len = . - msg

I have executed as indicated with the lines :

as -o hello-x64.o hello-x64.S
ld -o hello-x64 hello-x64.o
./hello-x64

But in my case, I have the following error message. Segmentation fault (core dumped) Could someone explain to me why , please ?

Thank you

Jester
  • 56,577
  • 4
  • 81
  • 125
Dev
  • 463
  • 3
  • 12
  • 3
    Is "your architecture" the linux in windows thing by any chance? That is 32 bit code but should accidentally work in a true linux. It does not work in the windows emulation thing or on macos. – Jester Feb 16 '20 at 16:27
  • 1
    This is missing info about what platform. So it needs to be closed as lacking details along with the [mcve]. But since it's probably what Jester suggested, closing it as a dup. If further details (like being on MacOS) mean it's something different, we can re-open. – Peter Cordes Feb 16 '20 at 16:37
  • I use WSL (Linux in Windows) on x86-64 pc – Dev Feb 16 '20 at 16:46
  • 1
    WSL does not support 32 bit code which this one is. Use a proper 64 bit version. – Jester Feb 16 '20 at 16:50
  • I have 3 questions : 1) do you mean I should use a true LInux 64 bits ? 2) when you read my code, How do you recognize that this is 32 bit code 3) Can I adapt this code to make it 64 bits ? – Dev Feb 16 '20 at 16:58
  • 1
    1) if you do it will work, but it's not recommended. See the question marked as duplicate 2) `int $0x80` is the 32 bit system call 3) yes, although that would mean changing all of the instructions. Depends whether you call that adapting or not. Anyway, there are plenty of 64 bit [hello world examples](https://stackoverflow.com/q/19743373/547981) (although that one is nasm syntax but you get the idea). – Jester Feb 16 '20 at 17:30
  • You wrote "WSL does not support 32 bit code which this one is. Use a proper 64 bit version." So, what do you mean precisely by "use a proper 64 bit version" ? please – Dev Feb 16 '20 at 18:00
  • 1
    I have linked you to a 64 bit version. You can also find other examples. – Jester Feb 16 '20 at 18:00
  • another question please : When you write about executing the code on a 64 bit architecture, " if you do it will work, but it's not recommended. ", so, what is recommended ? – Dev Feb 16 '20 at 18:18
  • And a last question : does an emulator like QEMU on windows 64 bits can enable me to simulate LINUX 32 or 64 bits ? – Dev Feb 16 '20 at 18:34
  • 1
    It's recommended to write 64 bit code if you intend to create a 64 bit program. Yes, `qemu` can simulate 32 or 64 bit, and a simulated 64 bit linux has 32 bit compatibility, but for that you should create a 32 bit program (e.g. `gcc -m32` or `as --32`, `ld -melf_i386`). This only works on linux as WSL does not support 32 bit. – Jester Feb 16 '20 at 18:40
  • Thank you. I decided to update my version of VirtualBox on Windows, to use Ubuntu – Dev Feb 16 '20 at 19:59
  • Will it work with VirtualBox and Ubuntu on it ? because I am reinstalling everything , based on that choice – Dev Feb 16 '20 at 20:00
  • Yes, it should work with VirtualBox and ubuntu. If you install 32 bit ubuntu it will work as-is. If you install 64 bit it will still work, but that's not recommended. In that case install the 32 bit toolchain and compile to a 32 bit program. – Jester Feb 16 '20 at 23:38

0 Answers0