The MASM Forum Archive 2004 to 2012

Miscellaneous Forums => 16 bit DOS Programming => Topic started by: cqzhy1986 on February 26, 2009, 08:59:35 AM

Title: How to generate a .COM file?
Post by: cqzhy1986 on February 26, 2009, 08:59:35 AM
Suppose I have a .ASM file, how could I generate a .COM file that can run under real mode in MS-DOS 6.22 ?
I have MASM32 in windows, also have a MASM611 in DOS.

So I need a way using either of the two MASM versions to generate a .COM file.

Thank you~ :boohoo:
Title: Re: How to generate a .COM file?
Post by: donkey on February 26, 2009, 09:22:05 AM
Start with ORG 100h then move to the 16 bit assembler section of the board...

16 bit (http://www.masm32.com/board/index.php?board=24.0)
Title: Re: How to generate a .COM file?
Post by: cqzhy1986 on February 26, 2009, 10:18:16 AM
Quote from: donkey on February 26, 2009, 09:22:05 AM
Start with ORG 100h then move to the 16 bit assembler section of the board...

16 bit (http://www.masm32.com/board/index.php?board=24.0)
Thanks a lot !
Title: Re: How to generate a .COM file?
Post by: jj2007 on February 26, 2009, 11:34:18 AM
This still works under Win XP from a command prompt.

.model tiny

.code
mov ah, 09h ; write string to STDOUT
mov dx, offset 82h ; get command line
int 21h ; show it... ;-)
ret
end


Save as myfile.asm and build with buildcom.bat:


@echo off
\masm32\bin\ml /c myfile.asm
\masm32\bin\link16 /tiny myfile.obj, myfile.com,,,,
echo.
myfile.com Hello World, how are you?$
echo.
echo.
pause


The linker needs the 4 trailing commas, and the command line must end with the dollar sign.
Title: Re: How to generate a .COM file?
Post by: cqzhy1986 on February 26, 2009, 04:25:47 PM
Quote from: jj2007 on February 26, 2009, 11:34:18 AM
This still works under Win XP from a command prompt.

.model tiny

.code
mov ah, 09h ; write string to STDOUT
mov dx, offset 82h ; get command line
int 21h ; show it... ;-)
ret
end


Save as myfile.asm and build with buildcom.bat:


@echo off
\masm32\bin\ml /c myfile.asm
\masm32\bin\link16 /tiny myfile.obj, myfile.com,,,,
echo.
myfile.com Hello World, how are you?$
echo.
echo.
pause


The linker needs the 4 trailing commas, and the command line must end with the dollar sign.
Thank you, let me try it.
Title: Re: How to generate a .COM file?
Post by: cqzhy1986 on February 26, 2009, 04:37:04 PM
I am not using MASM32 in winxp but using MASM611 in MSDOS.

My code:

.model tiny

.code
    mov ax, 10h
    mov bx, 20h
    add ax, bx
end

And I compile and link it with the following command:
ml /c test.asm
link /tiny test.obj, test.com,,,,

But I got a warning:
LINK: warning L4055: start address not equal to 0x100 for /TINY

I edited the code to:

.model tiny

.code
    org 100h
    mov ax, 10h
    mov bx, 20h
    add ax, bx
end

And also got the same warning.
What is wrong with my code?  :dazzled:
Title: Re: How to generate a .COM file?
Post by: MichaelW on February 26, 2009, 06:37:00 PM
To eliminate the warning, specify the program entry point:

.model tiny
.code
  start:
    org 100h
    mov ax, 10h
    mov bx, 20h
    add ax, bx
end start


Also, you can terminate the linker command line with ";" to tell the linker to use the defaults for the remaining parameters:

link /tiny test.obj, test.com;

Title: Re: How to generate a .COM file?
Post by: jj2007 on February 26, 2009, 08:19:11 PM
Quote from: MichaelW on February 26, 2009, 06:37:00 PM
To eliminate the warning, specify the program entry point:
LINK: warning L4099: you just bloated your code by a factor 33
:bg

Quote
Also, you can terminate the linker command line with ";" to tell the linker to use the defaults for the remaining parameters:

link /tiny test.obj, test.com;


Good to know, thanxalot!
Title: Re: How to generate a .COM file?
Post by: cqzhy1986 on February 27, 2009, 05:15:14 AM
Quote from: MichaelW on February 26, 2009, 06:37:00 PM
To eliminate the warning, specify the program entry point:

.model tiny
.code
  start:
    org 100h
    mov ax, 10h
    mov bx, 20h
    add ax, bx
end start


Thank you! This works quite well!  :U
Title: Re: How to generate a .COM file?
Post by: japheth on February 27, 2009, 07:52:27 AM
Quote from: Cao Huanyin on February 27, 2009, 05:15:14 AM
Quote from: MichaelW on February 26, 2009, 06:37:00 PM
To eliminate the warning, specify the program entry point:

.model tiny
.code
  start:
    org 100h
    mov ax, 10h
    mov bx, 20h
    add ax, bx
end start


Thank you! This works quite well!  :U

It will work, but it works "by chance" and the result is bloated by 256 x'00' bytes. This is better:

.model tiny
.code
    org 100h
start:
    mov ax, 10h
    mov bx, 20h
    add ax, bx
end start

Title: Re: How to generate a .COM file?
Post by: cqzhy1986 on February 27, 2009, 08:24:38 AM
I don't understand what is the effect of "org 100h".
I know DOS will load .COM codes into PSP:0100H, but while I open the .COM file with ultraedit, I see the binary starts from 00h and there is no information there about "org 100h" at all.

I mean, "org 100h" is not translated into binary and stored in .COM file, then why should I write it in .asm?

Quote from: japheth on February 27, 2009, 07:52:27 AM
Quote from: Cao Huanyin on February 27, 2009, 05:15:14 AM
Quote from: MichaelW on February 26, 2009, 06:37:00 PM
To eliminate the warning, specify the program entry point:

.model tiny
.code
  start:
    org 100h
    mov ax, 10h
    mov bx, 20h
    add ax, bx
end start


Thank you! This works quite well!  :U

It will work, but it works "by chance" and the result is bloated by 256 x'00' bytes. This is better:

.model tiny
.code
    org 100h
start:
    mov ax, 10h
    mov bx, 20h
    add ax, bx
end start


Title: Re: How to generate a .COM file?
Post by: sinsi on February 27, 2009, 08:37:39 AM
Quote from: Cao Huanyin on February 27, 2009, 08:24:38 AM
I mean, "org 100h" is not translated into binary and stored in .COM file, then why should I write it in .asm?
Because any file with .com is expected to be a 'com file' and loaded at segment:0100h and run from segment:0100h...
Title: Re: How to generate a .COM file?
Post by: jj2007 on February 27, 2009, 08:51:33 AM
Quote from: sinsi on February 27, 2009, 08:37:39 AM
Quote from: Cao Huanyin on February 27, 2009, 08:24:38 AM
I mean, "org 100h" is not translated into binary and stored in .COM file, then why should I write it in .asm?
Because any file with .com is expected to be a 'com file' and loaded at segment:0100h and run from segment:0100h...

That's why the linker issues a warning. However, Cao has a point here, too, because in effect the OS does not see any difference. You cannot squeeze instructions for the OS into 8 bytes of code :wink

By the way: There should be a ret at the end of your code. At least on my machine (Win XP), I see nasty boxes.
Title: Re: How to generate a .COM file?
Post by: sinsi on February 27, 2009, 09:30:46 AM
Quote from: jj2007 on February 27, 2009, 08:51:33 AM
You cannot squeeze instructions for the OS into 8 bytes of code :wink
1 byte
ret
Where did 8 bytes come from?

One exception in MS-DOS (5/6/7?) is command.com - this is actually an exe but the loader recognises "MZ".
Title: Re: How to generate a .COM file?
Post by: MichaelW on February 27, 2009, 12:15:48 PM
Quote from: japheth on February 27, 2009, 07:52:27 AM
It will work, but it works "by chance" and the result is bloated by 256 x'00' bytes. This is better:

.model tiny
.code
    org 100h
start:
    mov ax, 10h
    mov bx, 20h
    add ax, bx
end start


Thanks for the correction. Beginners have enough problems without bad examples.

Quote from: Cao Huanyin on February 27, 2009, 08:24:38 AM
I don't understand what is the effect of "org 100h".
I know DOS will load .COM codes into PSP:0100H, but while I open the .COM file with ultraedit, I see the binary starts from 00h and there is no information there about "org 100h" at all.

I mean, "org 100h" is not translated into binary and stored in .COM file, then why should I write it in .asm?

The org directive sets the value of a "location counter" that the assembler uses to assign addresses to code and data labels. The location counter by default starts at zero, so for this code:

.model small
.code
  start:
    jmp L2      ; assembles to 2 bytes
  L1:
    mov ax, 1   ; assembles to 3 bytes
  L2:
end start


The start label would be assigned an offset address of 0, L1 an offset address of 2, and L2 and offset address of 5. If you added an ORG 100h directive:

.model small
.code
    org 100h
start:
    jmp L2      ; assembles to 2 bytes
  L1:
    mov ax, 1   ; assembles to 3 bytes
  L2:
end start


The start label would be assigned an offset address of 100h, L1 an offset address of 102h, and L2 and offset address of 105h.

DOS programs are preceded in memory by a 256-byte Program Segment Prefix (PSP) that contains, among other things, the program command line and various data that DOS uses to manage the program and provide services to it. An EXE file loads in its own segment, immediately above the PSP segment. A COM file loads in the same segment as the PSP, immediately above the PSP. Because the PSP starts at offset 0 in the segment and is 100h bytes long, the offset address of the start of the actual program is 100h. So basically, the ORG 100h directive ensures that the offset address at the start of the program matches the load address. For reasons that I don't have time to get into right now, a mismatch is unlikely to have any effect on code that does not reference data, but it will have an effect on code that does.
Title: Re: How to generate a .COM file?
Post by: cqzhy1986 on February 27, 2009, 03:40:36 PM
It's very kind of all of you  :U I have learnt a lot.
Title: Re: How to generate a .COM file?
Post by: jj2007 on February 27, 2009, 05:17:27 PM
Quote from: sinsi on February 27, 2009, 09:30:46 AM
Where did 8 bytes come from?
The Hello World cheat - the smallest executable that can say Hello World:
.model tiny

.code
org 100h
start: mov ah, 09h ; write string to STDOUT
mov dx, offset 82h ; get command line
int 21h ; show it... ;-)
ret
end start


"Cheat" because you have to pass the Hello World via the command line (with a trailing $) :bg
Title: Re: How to generate a .COM file?
Post by: herge on March 03, 2009, 06:23:25 PM

Hi jj2007:

Would it be a good idea to put a
  db "$",0
after the ret instruction on the off
chance they don't put a $ at the
end of the command line.
I suspect it might be a run away
looking for a $ that's not there.

Regards herge
Title: Re: How to generate a .COM file?
Post by: Vortex on March 03, 2009, 06:53:42 PM
Cao Huanyin,

You can use the free linker from Digital Mars to create .COM files.