0

I am using cygwin to compile and link assembly. First problem, when I enter ld command : ld -m elf_i386 helloworld.o -o helloworld I get the following error: ld: unrecognised emulation mode: elf_i386 Supported emulations: i386pe i386pep Second problem, when I run the program I get no output and I get segmentation fault

   SECTION .data
msg     db      'Hello World!', 0Ah

SECTION .text
    global _start
        _start:
            mov edx , 13
            mov ecx , msg
            mov ebx , 1
            mov eax , 4 
            int 80h

            mov ebx , 0
            mov eax , 1
            int 80h
Connell.O'Donnell
  • 3,603
  • 11
  • 27
  • 61
  • 1
    ELF is a Linux format. Cygwin is running windows so the format is COFF – matzeri Feb 24 '21 at 18:48
  • 1
    Also, I don't believe `int 80h` works under cygwin: https://stackoverflow.com/a/37013302/2189500 – David Wohlferd Feb 24 '21 at 19:23
  • so what is the replacment for int 80h – Abdelrahman Yehia Feb 24 '21 at 23:35
  • There is no direct analog to calling interrupts under Windows (https://stackoverflow.com/a/21074450/2189500). Instead, you call OS functions (via DLLs) that make the appropriate low-level calls for you. There are a number of SO questions about this (https://stackoverflow.com/a/64414459/2189500). – David Wohlferd Feb 24 '21 at 23:53
  • i cant understand what to do can you make it more clear – Abdelrahman Yehia Feb 25 '21 at 00:15
  • 2
    If you were writing this code in C, you'd write this as `fwrite("Hello World!\n", 13, 1, stdout);`. On Linux, the code for the fwrite function would eventually call sys_write (which is what you are doing above). The Windows implementation of fwrite eventually calls WriteFile. Windows doesn't document how WriteFile works internally. It may call interrupts, syscall or whatever. This means that in linux, you can "shortcut" and call the low level stuff directly. But in Windows, you MUST call a function in a library, there is no shortcut. The link I gave you shows how to call printf. – David Wohlferd Feb 25 '21 at 01:30
  • I still can't understand , can you provide me an actual code like my code but for windows – Abdelrahman Yehia Feb 27 '21 at 15:06
  • 1
    I already did (it's the last link above). It includes the code plus the command lines to build it. It's a bit different than your code since a) it's 64bit instead of 32bit and b) it uses (the more flexible) printf instead of fwrite. It does assume that you have the Windows development tools installed, but you'll need that for almost any Windows program you plan to write. You can get the tools by installing the free (community) version of Visual Studio. I'm not sure where you got this code, but you might want to find a different tutorial for learning programming in Windows, or use Linux. – David Wohlferd Feb 27 '21 at 20:35
  • i got this error : undefined reference to 'printf' – Abdelrahman Yehia Feb 28 '21 at 16:08

0 Answers0