News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

How to generate a .COM file?

Started by cqzhy1986, February 26, 2009, 08:59:35 AM

Previous topic - Next topic

cqzhy1986

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:

donkey

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

16 bit
"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

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
Thanks a lot !

jj2007

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.

cqzhy1986

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.

cqzhy1986

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:

MichaelW

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;

eschew obfuscation

jj2007

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!

cqzhy1986

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

japheth

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


cqzhy1986

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



sinsi

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...
Light travels faster than sound, that's why some people seem bright until you hear them.

jj2007

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.

sinsi

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".
Light travels faster than sound, that's why some people seem bright until you hear them.

MichaelW

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.
eschew obfuscation