Hello guys,

I am new on these forums and wanted to ask something about assembling on DOS.
I started lately to learn 16-bit Assembly and thought it would be a good base to start from, and then learn by myself to assemble in 32-bit and even 64-bit.
The reason I am starting to assemble is because I am taking soon a course of Reverse Engineering and I already know assembler, just want to make it better.

I got an advice to practice using MASM, after reading about it some I figured it wouldn't work on 32-bit (and I have 64-bit windows 7 so I installed VirtualBox to have an XP with 32-bit). Then, I read that it is recommended to use DosBox to assemble and practice, and using MASM on it.

Now, what I want to ask is, how am I exactly installing MASM on DosBox? or how do I even use it on that? I really am confused since the only time I used DOS was maybe when I was really really young and it wasn't for purpose of programming.

Any assistance would be gladly appreciated :)

p1Mp.
Posted on 2012-03-09 03:43:40 by p1Mp
MASM has been a 32-bit Windows application for a long time now. There is also a 64-bit version of MASM.
If you want to use MASM in Dosbox, I think you'll specifically need to look for a very old version of MASM, because as far as I know, the newer versions will not work.
What I normally do is build DOS applications with the 32-bit Windows version, then use an older version of LINK.EXE (which is also 32-bit Windows) which can still output DOS executables.
Then I run the built executable in Dosbox.
Posted on 2012-03-09 04:14:26 by Scali
The thing is that the book I'm reading from and practicing ASM is working on 16-bit, probably referring to DOS. Do you mind then please explaining to me how to work on Dos so I can assemble on 16-bit? Because it might not work (the way I will make the code) if I work on 32-bit or 64-bit platforms.

Note: Consider that I am purely new to this and I know nothing about DOS applications or anything like this, that means I'm confused of every 2nd work you say as hell :)
Posted on 2012-03-09 04:19:25 by p1Mp
Just because MASM is a 32-bit Windows application doesn't mean it can't produce 16-bit DOS (or Win16) code.
And just because your program runs in DOS doesn't mean it has to be built from DOS.

So, what you can do is this:
Get MASM32 (http://www.movsd.com). Install it in Windows.
Now, if I'm not mistaken, MASM32 comes with a LINK16.EXE in the \bin directory (if not, I have a copy of it for you).
LINK16.EXE is an old linker, which still supports creating DOS EXEs and COM files.

Then you use MASM (ML.EXE) and the linker (LINK16.EXE) from a Windows command prompt to assemble and link your DOS program.
You can then set up Dosbox to mount the directory in which you built your DOS program, and run it there.

So you basically have two windows side-by-side: One Windows command prompt to build your DOS program, and Dosbox to run your program.
Posted on 2012-03-09 04:36:38 by Scali
What do you mean by building my program? don't I just write it on a .txt file and then make it .asm, and do something with it? Can you please explain to me stage-by-stage thorough?
Like, how do I use the linker and what does it do, how do I use the ML.exe and what does it do, how do I for example run a program and then see what the register contains, like, how it all works with commands and such.

By the way, do I must work with 32-bit virtual computer to assemble? cant I work with my 64-bit computer?
Posted on 2012-03-09 05:56:26 by p1Mp

What do you mean by building my program? don't I just write it on a .txt file and then make it .asm, and do something with it? Can you please explain to me stage-by-stage thorough?


Building a program is the process of converting your source code into a working binary. In this case, assembling and linking it.


Like, how do I use the linker and what does it do, how do I use the ML.exe and what does it do, how do I for example run a program and then see what the register contains, like, how it all works with commands and such.


I think you're better off reading some tutorial or such, perhaps Art of Assembly.


By the way, do I must work with 32-bit virtual computer to assemble? cant I work with my 64-bit computer?


No, 32-bit Windows applications are still supported by 64-bit Windows, so you can use 32-bit MASM and LINK16.EXE without a problem (which is why I suggested that route, I've been using that myself on 64-bit Windows).
Posted on 2012-03-09 06:13:12 by Scali
Okay thank you.
Can you still explain to me stage-by-stage to how for example build a program and check for example the registers to see if it worked well? using the MASM & Command Prompt & DosBox. and you can explain to me the linking really briefly I will understand, no need to tell me to read a tutorial when I don't even know where to look, especially when finding results of ASM in google is hard, especially about 16-bit or w.e =\
Posted on 2012-03-09 06:57:45 by p1Mp

Okay thank you.
Can you still explain to me stage-by-stage to how for example build a program and check for example the registers to see if it worked well? using the MASM & Command Prompt & DosBox. and you can explain to me the linking really briefly I will understand, no need to tell me to read a tutorial when I don't even know where to look, especially when finding results of ASM in google is hard, especially about 16-bit or w.e =\


If you want to check the registers, you need a debugger. A DOS debugger.
I suppose debug.exe included with DOS itself would be a start (note that DOS is not included with Dosbox, but you can find DOS packages for Dosbox on the web).

As for a tutorial, as I said, look into Art of Assembly, the 16-bit DOS edition.
http://homepage.mac.com/randyhyde/webster.cs.ucr.edu/www.artofasm.com/DOS/pdf/0_AoAPDF.html
It explains how to use MASM and such (see chapter 4).
It uses the CodeView debugger for DOS, not sure if that is easily available on the web. You might have more luck with Turbo Debugger perhaps. It comes with Turbo C++, Turbo Assembler or Turbo Pascal, which are still reasonably popular around the web.
Posted on 2012-03-09 08:10:41 by Scali
So can I find how to assemble with DOSBox and windows command prompt and MASM on chapter 4? That is all I want... I am getting way confused with your explanations about the DOS debugger and Turbo assembler etc =\, I do want to check the registers but can you show me how to use that dos debugger to check them after running an ASM code?
Because the way you explain it you talk as I already know the tools and programs to do it.

Thanks for your time bro, and for helping me.

EDIT: After looking at Chapter 4, man it is really really long, how can I know where I find what I need? I know already all of the things there, about registers, flags, memory, segments. It is theoretical material, where can I find how to bring that to practice...?

EDIT2: Alright so I found chapter 4.9.4 and it explains about ML, from what I understood it is checking errors, correct me if I'm wrong, and if there's an error it doesn't make an .exe file.

I am trying to assemble this code:

CODE SEGMENT
ASSUME CS:CODE, DS:CODE
START: MOV AX, CODE
      MOV DS, AX
      MOV AL, 66H
      MOV BX, 1000H
      MOV , AL
CODE ENDS
END START


(Note: I assemble this code on the 64-bit with the windows command prompt using the command  'ml /Zi prog.asm')

After trying to assemble that, I get these 2 warnings and 1 error:

LINK : warning LNK4044: unrecognized option "z2"; ignored
LINK : warning LNK4044: unrecognized option "CO:nopack"; ignored
LINK : error : Segment reference in fixup record
prog.obj : fatal error LNK1123: failure during conversion to COFF: file invalid
or corrupt


Can you tell me why is this? I believe it is a correctly written code, no?
Posted on 2012-03-09 08:19:15 by p1Mp
Geez man, some things just require effort.
Also, as I said, you need to use LINK16.EXE.
It looks like you're trying to use the regular LINK.EXE, which doesn't work for DOS (it tries to convert to COFF, which is the new object format, DOS used OBJ).
(LINK16.EXE is just an old LINK.EXE which is renamed to LINK16.EXE for the MASM32 package, to avoid confusion with the regular LINK.EXE that you'd use for Win32 applications. LINK16.EXE can be used for DOS and Win16 applications).
Posted on 2012-03-09 08:53:25 by Scali
I didn't even touch the linker =\, all I did was use the command of the ML..?

Look, all I did was going to windows command prompt, going to the /bin directory (using 'cd' command), and then when in the directory, making sure prog.asm which is the code I showed before is in the /bin directory, and then typing in the command prompt 'ml /Zi prog.asm'.
Posted on 2012-03-09 08:57:40 by p1Mp
Ah right... ML automatically starts the linker.
Which it shouldn't.
You need to use the /c switch so you can manually start the linker.
Posted on 2012-03-09 09:21:19 by Scali

Ah right... ML automatically starts the linker.
Which it shouldn't.
You need to use the /c switch so you can manually start the linker.


K, I'll try. Thanks.

Ok this is what came out:

C:\masm32\bin>ml /c prog.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

Assembling: prog.asm

C:\masm32\bin>link16 prog.asm

Microsoft (R) Segmented Executable Linker  Version 5.60.339 Dec  5 1994
Copyright (C) Microsoft Corp 1984-1993.  All rights reserved.

Run File :
List File :
Libraries [.lib]:
Definitions File :
prog.asm : fatal error L1101: invalid object module
Object file offset: 1 Record type: 43


Whats wrong now with the linker?

I tried again, thought maybe I need to use the linker on the .obj file, and this is what came out:

C:\masm32\bin>link16 prog.obj

Microsoft (R) Segmented Executable Linker  Version 5.60.339 Dec  5 1994
Copyright (C) Microsoft Corp 1984-1993.  All rights reserved.

Run File :
List File :
Libraries [.lib]:
Definitions File :
LINK : warning L4021: no stack segment

It also created a prog.exe file

And what are these fields that I left empty that needs to be filled?
Posted on 2012-03-09 09:22:13 by p1Mp
Run File should be quite obvious: the name of the binary the linker should generate (usually .exe, sometimes .com).
List File is a .map file, which will contain debugging information.
With Libraries you can specify any libraries that your code requires (the linker's main task is to combine multiple .obj files into a complete application. A library is a collection of .obj files).
A Definition file contains the names of symbols that you want to export (which mainly applies to Windows DLLs).

Basically all the default values are okay. You need to specify the Run File name, but it generally defaults to the right name anyway. The rest is optional and can be skipped.

Congratulations! You have now built PROG.EXE, which you can run in Dosbox (and it will crash, because your program does not do a clean exit).
Posted on 2012-03-09 09:36:15 by Scali

Run File should be quite obvious: the name of the binary the linker should generate (usually .exe, sometimes .com).
List File is a .map file, which will contain debugging information.
With Libraries you can specify any libraries that your code requires (the linker's main task is to combine multiple .obj files into a complete application. A library is a collection of .obj files).
A Definition file contains the names of symbols that you want to export (which mainly applies to Windows DLLs).

Basically all the default values are okay. You need to specify the Run File name, but it generally defaults to the right name anyway. The rest is optional and can be skipped.

Congratulations! You have now built PROG.EXE, which you can run in Dosbox (and it will crash, because your program does not do a clean exit).


:P

And how do I check its registers tell me plz again?! :D

By the way, I try to run it on DosBox by mounting it on c:\masm32\bin and then type prog.exe, am I doing it correct? Because it doesn't work... (Illegal command: prog.exe)

And how do I do an exit clean?
Posted on 2012-03-09 09:39:10 by p1Mp

And how do I check its registers tell me plz again?! :D


As I said, get a debugger for DOS (be it DOS's own DEBUG.EXE, Borland's Turbo Debugger, Microsoft CodeView, or whatever else), and then open PROG.EXE in your debugger and single-step through it.
Posted on 2012-03-09 09:44:01 by Scali


And how do I check its registers tell me plz again?! :D


As I said, get a debugger for DOS (be it DOS's own DEBUG.EXE, Borland's Turbo Debugger, Microsoft CodeView, or whatever else), and then open PROG.EXE in your debugger and single-step through it.


It does crash when I run the program. How do I do an exit clean then?
And can you please tell me how to use debug.exe? (Sorry im being annoying and nagging you too much, I just hope you understand that it's hard to figure out about ASM on google (the internet itself) and that is why I came to this forum, your help is really really appreciated just so you know :D , and sorry once again...)
Posted on 2012-03-09 10:02:00 by p1Mp
Art Of Assembly will tell you how to properly exit from a program.
And you should also read about debugging there.
Not sure if DEBUG.EXE is a good idea actually. Doesn't work in my Dosbox, it says "Incorrect DOS version".
Posted on 2012-03-09 10:07:57 by Scali
Can you please tell me in which chapter I find how to exit a program in a clean way?
Also, what debugger do you recommend using then?
Posted on 2012-03-09 10:16:32 by p1Mp
Argh? What is up with kids today? The Google generation?
Need instant answers for everything?

The world doesn't work that way. I've already spelled out way more than should be required (and I already suggested various debuggers).
It ends here. Just start reading Art of Assembly. You'll find what you're looking for soon enough.
Posted on 2012-03-09 11:27:17 by Scali