2

In my high-school homework I have to write a program, that uses DOS Interrupts to input and output strings instead of std printf/scanf But when I attempting to run this program:

format ELF
use16
section '.data' writeable
    msg db 'Hello, world!', 0


section '.text' executable
public _main
_main:
   mov ebp, esp; for correct debugging
   mov   ah, 1
   int   21h 
   mov  ah,4Ch
   int   21h
   xor eax, eax
ret

It's just crashing. I attached debugger and found out that it crashes on this line: int 21h. I absolutely have no ideas about why it happens.
I use FASM, SASM IDE and Windows XP SP3 x32

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Pavel Shishmarev
  • 1,335
  • 9
  • 24
  • 5
    That's wrong on so many levels. For starters it uses `ELF` which not windows. I am surprised you even got it running somehow. Then, it's using `int 21h` which is a DOS interrupt. There may be a way to get windows to give you a DOS compatible environment (or not). Finally, why are you still using XP? – Jester Feb 26 '18 at 14:56
  • I'm using Linux, but homework requires windows enviroment, so I use VM. – Pavel Shishmarev Feb 26 '18 at 15:15
  • But other programs(without using interrupts) successfully compiles and works under windows with ELF executable format. And IDE always generates this header on Windows. And I tried to replace it with PE or MZ, same result -- SIGSEGV on int 21h. – Pavel Shishmarev Feb 26 '18 at 15:15
  • 2
    If you are using linux and want to run DOS stuff, use `dosbox`. – Jester Feb 26 '18 at 15:20
  • i'm confused. Are you using Windows as the host environment but you are running FASM, SASM IDE inside a Virtual machine running Linux? or is that the other way around? – Michael Petch Feb 26 '18 at 15:21
  • Linux Mint is a host. WinXP is a guest. SASM under guest. SASM is just IDE for Flat Assembly. I'm very sorry for my bad english. – Pavel Shishmarev Feb 26 '18 at 15:23
  • I know. I have it on Linux too, but this task is about DOS Interrupts, thats why I use it under Win. – Pavel Shishmarev Feb 26 '18 at 15:26
  • 5
    @Jester . The reason ELF works here is because he uses `format ELF` and not `format ELF executable`. Without executable fasm will output an elf object. Under Windows SASM it will link that ELF object to a PE32 executable with a MingGW version of GCC That version of GCC and LD understand both ELF and PE formats, so it can use ELF objects to produce a final PE32 executable. Of course using DOS interrupts in a windows program will not work. – Michael Petch Feb 26 '18 at 16:01
  • 4
    That is just sick :D – Jester Feb 26 '18 at 16:04

1 Answers1

5

When using SASM IDE and you use format ELFin your assembly code, FASM will assemble the file to an ELF object (.o file) and then use (by default) a MinGW version of GCC and LD to link that ELF object to a Windows executable (PE32). These executables run as native Windows programs, not DOS. You can not use DOS interrupts inside a Windows PE32 executable since the DOS interrupts do not exist in that environment. The end result is that it crashes on the int 21h.

If you want to create a DOS executable that can run in 32-bit Windows XP you could do this:

format MZ                   ; DOS executable format
stack 100h

entry code:main             ; Entry point is label main in code segment

segment text
msg db 'Hello, world!$'     ; DOS needs $ terminated string

segment code
main:
    mov   ax, text
    mov   ds, ax            ; set up the DS register and point it at
                            ; text segment containing our data
    mov   dx, msg
    mov   ah, 9
    int   21h               ; Write msg to standard output

    mov   ah, 4Ch
    int   21h               ; Exit DOS program

This will generate a DOS program with an exe extension. Unfortunately you can not use SASM IDE to debug or run a DOS program. you can run the generated program from the 32-bit Windows XP command line. 32-bit versions of Windows run DOS programs inside the NTVDM (virtual DOS machine).

Michael Petch
  • 46,082
  • 8
  • 107
  • 198