The MASM Forum Archive 2004 to 2012
Welcome, Guest. Please login or register.
October 01, 2020, 04:03:21 PM

Login with username, password and session length
Search:     Advanced search
128553 Posts in 15254 Topics by 684 Members
Latest Member: mottt
* Home Help Search Login Register
+  The MASM Forum Archive 2004 to 2012
|-+  Miscellaneous Forums
| |-+  16 bit DOS Programming
| | |-+  How to generate a .COM file?
« previous next »
Pages: [1] 2 Print
Author Topic: How to generate a .COM file?  (Read 8864 times)
cqzhy1986
Guest


Email
How to generate a .COM file?
« 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
Logged
donkey
Member
*****
Posts: 3789


ASS-embler


Re: How to generate a .COM file?
« Reply #1 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
Logged

"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable
cqzhy1986
Guest


Email
Re: How to generate a .COM file?
« Reply #2 on: February 26, 2009, 10:18:16 AM »

Start with ORG 100h then move to the 16 bit assembler section of the board...

16 bit
Thanks a lot !
Logged
jj2007
Member
*****
Gender: Male
Posts: 6011



Re: How to generate a .COM file?
« Reply #3 on: February 26, 2009, 11:34:18 AM »

This still works under Win XP from a command prompt.

Code:
.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:

Code:
@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.
Logged

cqzhy1986
Guest


Email
Re: How to generate a .COM file?
« Reply #4 on: February 26, 2009, 04:25:47 PM »

This still works under Win XP from a command prompt.

Code:
.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:

Code:
@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.
Logged
cqzhy1986
Guest


Email
Re: How to generate a .COM file?
« Reply #5 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
Logged
MichaelW
Global Moderator
Member
*****
Gender: Male
Posts: 5161


Re: How to generate a .COM file?
« Reply #6 on: February 26, 2009, 06:37:00 PM »

To eliminate the warning, specify the program entry point:
Code:
.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;

Logged

eschew obfuscation
jj2007
Member
*****
Gender: Male
Posts: 6011



Re: How to generate a .COM file?
« Reply #7 on: February 26, 2009, 08:19:11 PM »

To eliminate the warning, specify the program entry point:
LINK: warning L4099: you just bloated your code by a factor 33
 BigGrin

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!
Logged

cqzhy1986
Guest


Email
Re: How to generate a .COM file?
« Reply #8 on: February 27, 2009, 05:15:14 AM »

To eliminate the warning, specify the program entry point:
Code:
.model tiny
.code
  start:
    org 100h
    mov ax, 10h
    mov bx, 20h
    add ax, bx
end start

Thank you! This works quite well!  ThumbsUp
Logged
japheth
Guest


Email
Re: How to generate a .COM file?
« Reply #9 on: February 27, 2009, 07:52:27 AM »

To eliminate the warning, specify the program entry point:
Code:
.model tiny
.code
  start:
    org 100h
    mov ax, 10h
    mov bx, 20h
    add ax, bx
end start

Thank you! This works quite well!  ThumbsUp

It will work, but it works "by chance" and the result is bloated by 256 x'00' bytes. This is better:
Code:
.model tiny
.code
    org 100h
start:
    mov ax, 10h
    mov bx, 20h
    add ax, bx
end start
Logged
cqzhy1986
Guest


Email
Re: How to generate a .COM file?
« Reply #10 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?

To eliminate the warning, specify the program entry point:
Code:
.model tiny
.code
  start:
    org 100h
    mov ax, 10h
    mov bx, 20h
    add ax, bx
end start

Thank you! This works quite well!  ThumbsUp

It will work, but it works "by chance" and the result is bloated by 256 x'00' bytes. This is better:
Code:
.model tiny
.code
    org 100h
start:
    mov ax, 10h
    mov bx, 20h
    add ax, bx
end start

Logged
sinsi
Member
*****
Gender: Male
Posts: 1758


RIP Bodie 1999-2011


Re: How to generate a .COM file?
« Reply #11 on: February 27, 2009, 08:37:39 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...
Logged

Light travels faster than sound, that's why some people seem bright until you hear them.
jj2007
Member
*****
Gender: Male
Posts: 6011



Re: How to generate a .COM file?
« Reply #12 on: February 27, 2009, 08:51:33 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.
Logged

sinsi
Member
*****
Gender: Male
Posts: 1758


RIP Bodie 1999-2011


Re: How to generate a .COM file?
« Reply #13 on: February 27, 2009, 09:30:46 AM »

You cannot squeeze instructions for the OS into 8 bytes of code wink
1 byte
Code:
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".
Logged

Light travels faster than sound, that's why some people seem bright until you hear them.
MichaelW
Global Moderator
Member
*****
Gender: Male
Posts: 5161


Re: How to generate a .COM file?
« Reply #14 on: February 27, 2009, 12:15:48 PM »

It will work, but it works "by chance" and the result is bloated by 256 x'00' bytes. This is better:
Code:
.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.
 
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:
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:
Code:
.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.
Logged

eschew obfuscation
Pages: [1] 2 Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP The MASM Forum Archive 2004 to 2012 | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.
Valid XHTML 1.0! Valid CSS!