CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2008
    Location
    Oregon, USA
    Posts
    63

    Linked program wont run.

    I am trying to use nasm to compile assembly code into an object file, and then link the object file to an EXE. My first attempt was to use the following command lines:

    Code:
    nasm -f coff -o hello.o hello.asm
    ld -o hello.exe hello.o
    Where "ld" is the linker program that comes with the MinGW compiler tools. The program compiles fine, and seems to link fine, but when I try to run the program Windows XP (32-bit) gives me an error saying the program has encountered a problem and needs to close.

    I have tried using the "win32" format on nasm, but I get the same result.

    Here is my code (which tries to print a string and exit):

    Code:
    section .text
    global start ;I've tried omitting this, I've also tried underscores and replacing start with "main"
    
    start:  ;this has always matched the label given by global
    mov ah, 0x09 ;print string
    mov edx, my_str
    int 21h
    
    mov ax, 0x4C00 ;quit
    int 21h
    
    section .data
    my_str: db "Hello World!", 13, 10, '$'
    Here is a summary of my environment:
    nasm (compile ASM, tried win32 and coff outputs)
    MinGW linker (linking object files, I haven't tried other linkers)
    Windows XP 32-bit (host operating system)

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Linked program wont run.

    You know, I don't have experience with NASM, but I'll try to help you, though.

    This obviously is a DOS program, so you should run it from a command prompt. It should run then, if you change the register used to load the string pointer to from EDX to DX, as it's for a 16-bit environment. (It might even run without that change, as operand size overrides are supported it the real mode that this is to be run in and the most significant 16 bits of EDX that are not used by the DOS function call are all zeroes anyway. But you still should change it, though.)

    In addition you have to build a 16-bit EXE file from the object file. I suppose Windows would mistake it to be a Windows program if you link it to the PE format normally used nowadays. My MASM has a special tool named link16 for that purpose; I don't know how to tell the MinGW linker to do that.

    Moreover, programs like that are usually placed in COM files (tiny memory model) instead of EXEs. When I wanted to build such a file in old DOS days, I used a tool named exe2bin to convert the EXE output by the linker to COM format. The link16 I have seems to have an option named /tiny for that purpose, but I didn't try it. To be able to convert the program to the COM format, it would need an additional org 0100h directive before the start: label (again at least in MASM...).

    If you want to keep the program in EXE format, access to the string might be problematic the way it is now, as I'm not sure how the DS register would be set up in this case. I would recommend anyway for use in an EXE file to put the string data into a separate (default) data segment. At least that's cleaner style (which I must admit isn't really that important for a "hello world".)

    HTH

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured