Dear Programmers, Hi

I wrote a simple asm program to output a char on STDOUT in windows (MS-DOS)

Here is my code:
.STACK 256 ;Reserve 256-Bytes
char BYTE "a"

MOV AH, 02h ;Sub-function 02h -> Output STDOUT
MOV DL, char ;Char to output
INT 21h

MOV AH, 4Ch ;Return 0
MOV AL, 00h
INT 21h

END _main

In DOS I assemble it with ml
ml /c /coff output_char.asm

and link it
link /subsystem:console /entry:main /out:output_char.exe output_char.obj kernel32.lib
It assemble fine and i get the exe file but when i run it, it gives me
output_char.exe has encountered a problem and needs to be close....

Can you guys help me to understand what is wrong in my code (This is my first asm program)?


Recommended Answers

All 10 Replies

I would assume the .386 directive means to generate
32-bit code. And your calling 16-bit Dos interrupts???
I don't know what else is wrong, I do not use MASM.

I would assume the .386 directive means to generate
32-bit code. And your calling 16-bit Dos interrupts???
I don't know what else is wrong, I do not use MASM.

I do not know if the above is right....

Thanks for your reply. (I think .386 -> 80386 which is a 32-bits)

So now my question is how can I forced it to be 16-bits? or How can i do the IO operation (For STDOUT, STDIN) in 32-bits?

Thanks again

The simple answer is you can't.
The .386 and above have a Real Mode and a Protected Modes. Win32 such as XP, Win98, etc. are Protected Mode.

DOS is Real Mode. You can't mix and match without a lot of overhead and thunking between overlapped Real Mode and Protected Mode segment/selectors

For DOS you need to build an .8086 application small model, small data, set with an org $100. (You can use other memory models but best if you keep it small!) You can selectively use 32-bit registers within 16-bt RealMode but best to avoid it.

Thanks wildgoose for your reply.

I did change to .8086 and small model, but it still gives me the same error. I did not use .DATA this time.

.8086 ;For DOS Application
.STACK 256
MOV DL, "a"
MOV AH, 02h
INT 21h

; Return 0
MOV AL, 00h
INT 21h
END _main

But I'm still getting the same error. (I might be wrong but I'm thinking that my code have the "access violation")
Can you please look at the code and see what I'm doing wrong this time?


Code worked just fine for me! I cut'n'paste your last posting into test2.asm and built it. (I did tab most stuff since its not really suppose to be left justified!

I intentionally used MASM 6.15, which uses linker Link.exe version 5.31
It was first used back in the Real Mode Days then upgraded to work with Protected Mode applications such as XP.

ml test2.asm

Then ran it!

masm32 didn't like the code at all, which is what I expected as its targeted for 32-bit code thus Protected Mode.
Same for nasm.

I went back and looked at your original post...
link /subsystem:console /entry:main /out:output_char.exe output_char.obj kernel32.lib

KERNEL32.LIB is a Win32 library! You are trying to build a 16-bit Real Mode application, not a 32-bit Protected Mode application!

You can alwasy do

ml /c output_char.asm
link output_char.obj;

Thanks again for your reply.

Sorry about indentation, when I post it message everything became left justify.

My ml and link is 9.0 since I'm using visual studio 2008 (That's what school gave it to us long time ago).

I used
ml /c output_char.asm
link output_char.obj

I got the same error.
I know that windows (Visual Studio) in every version that they release, they change some stuff ....
What do u suggest, should I change my building tools to masm32 or just stick with this one?

Thanks again

Apples and Oranges. I'm not sure if the newer version has the capability of still building REAL mode apps. (Microsoft loves making things obsolete!) You can build a Win32 application and call a system library function and run as a 32-bit console application.

HOWEVER, rummaging around doing a wildcard search for EXE's in the masm32 folder there is a ML.exe which is the older assembler, and there is a LINK16.exe as well as a LINK.EXE inside the bin folder.

And these seem to work building the DOS application.

Build from a command prompt, not from the Visual Studio GUI.

Thanks again for your help.
I will try masm32.

Just a question:
With 32-bits assembly, there is no interrupt (Windows). Am I right? or 32-bits has different interrupt for STDIN and OUT (I search Ralph Interrupt list and could not find any interrupt for that matter)

If you can, please direct me to the good 32-bit assembly and 16-bit assembly book (Im reading -> Instruction to 80x86 assembly).

Again thank you so much for your help

Win32 From Win2000 and WinXP isolated the hardware to make it unaccessible directly from an application. Access was one of the nice features of Win98.

The books available here are my favorites. I have one set for work and one set for home.

The 32/64-Bit 80x86 book is complete but has some mistakes.
My favorite is the Vector Game Math Processors book. Covers SIMD operations on MIPS, PowerPC, and 80x86. It however is out of print but available new and used from Amazon.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.