How To Use The DEBUG Command To Assemble COM Files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nitt
Posts: 218
Joined: 22 Apr 2011 02:43

How To Use The DEBUG Command To Assemble COM Files

#1 Post by nitt » 28 Aug 2011 20:13

What Is DEBUG?

DEBUG is a 16bit application that comes with most 32bit distributions of Windows. It was removed on 64bit because they can't run it.

DEBUG was written by Tim Paterson, the guy who wrote most of the stuff in your computer. It was meant to be able to assemble, disassemble, and hexdump.

Can This Be Used On 64bit Operating Systems

Why yes, actually. It can.

Since a 64bit OS can't run 16bit applications on its own, they removed it from all newer Windows distributions. So in order to get it, download it from here.

You may notice it doesn't work, and gives you an "unsupported 16bit error". Now you need to go to DOSBox.COM and download that software to emulate DEBUG.

Any Downsides To Emulating It?

Again, yes. But not much. The only downside is that you cannot compile codes from files, you have to feed it all into the console.

To read your code from a file, use this command:

Code: Select all

debug < file.asm


How Does DEBUG Work?

Well, time to give you a rundown. Let's start with a screenshot.

Image


You may notice that none of the actual codes are highlighted and defined with the key,

Why is this?

It's like this because before I can go over basic Assembly, I have to explain the software.

Explanation of Image

First notice the "X" and "Y" coordinates. Now imagine 2D graphs, like back in middle school. You know how you have the X and Y axis, right? Well, same with data. It's stored at two points, which we can think of as a graph and like "X" and "Y" are the points. The first point, the X, we really don't have to worry about. You can just forget about that. The "Y" is what is important, though.

What is the "-a" for? The "a" character command allows you to jump to certain positions in data and start coding Assembly. If you type "a" without a number after it, it will take you to the last place you were.

Since Assembly codes start at 100, that's where it first takes you. So when you first start writing the codes, you want to write at 100. That's where your code always starts. Except the "X" may be a little different, don't bother with that, though. Just forget it exists. Notice how when I typed in "a 135" it took me to the location in data where "Y=135"? You can see the "135" in the same location as the "100" is that has the box around it.

Now what the heck do I mean by "ending Y-coordinate data" in the image? Well, that's simply where you code ends. You eventually reach the end where you are finished your code, so this differs depending on how long your code is. Mine was short, so the code only ended at "0142", as seen in the image.

So what's this whole "-h" thing?!

The "h" character command is used to subtract hex values. All values in Assembly are in hex, so you will run into points where you need to Google a hex table, but you do not need that here. Because this doesn't go that in-depth. PM me for some example codes later, and I'll make some.

NOW WHY ARE WE SUBTRACTING HEXES?!

This one's easy. Remember how I said you have positions in data? Well, these are hexes. We start at 0100, and in this example, we ended up ending at "0142". So what we did was took the end value, subtracted it from the starting value, and received our "total program size". This is the range of bytes (in hex) our application covered, so that's the size of our application. We must do this to get the size, because DEBUG doesn't know how big the file it will write should be. If we write the file too small, it won't be able to hold all of our data and thus, not work. If we make it too large, it will just take up more space than it needs to.

As you see, I used the "rcx" DEBUG command to define the file size as "42" (I didn't plan this, promise). "42" just happened to be how big my file should be. Just remember that you don't always need 0's. Such as, I didn't need to type in "0042", I just used "42" and it understood.

Now finally, I just used the "w" character key to write the file. And this file is a basic

Code: Select all

Hello, World!
Press any key to continue . . .


file.

Now About Assembly?

So, here I'm going to give you a rundown on basic Assembly. Again, if you want to know more advanced, PM me and I'll tell. If you are better at watching, then watch my video on YouTube, although some things are slightly inaccurate, and I've been trying to add annotations to fix things.

Here are the commands in Assembly I will be discussing:

Code: Select all

mov
int
jmp
db


And here are the registers I will quickly explain:

Code: Select all

ah
dx


Now, let's talk about registers. Basically, they are things you move data into. Not that complex.

"ah" is kinda like your command register. You move command values into there. We move data into registers with the "mov" command. It goes like "mov {register}, {value}". As you can see, I use the "mov" command 3 times. First time, I move "09" into the "ah" register. This represents the "display string" function.

Now you also can see that I moved "102" into "dx". Well, what's "dx"? The "dx" register is kinda like your value caller. Anytime you use a command register, such as "ah" or "ax", sometimes the command requires a variable. In this case, the command requires a string. So it will search the "dx" register for the string.

See the line where it says the value is "102"? And how I used the "db" command? That means "define bytes", this defines that section of data as some bytes. And we made a string. Strings MUST end with a dollar sign ($), and you can combine multiple strings with commas. I used commas to combine it with "0A", and "0D". "0A" is just a new line and "0D" moves the line to the left of the console, to just make sure everything is lines up correctly.

Also notice how right after 102 is 131. This is because the string took up 29 (hex) bytes of data. So what's "jmp"? "jmp" is used to jump to specific places in data. So right at 100, where our code starts, I told it to jump all the way down to 135. Now why would I do a crazy thing like that? Well, because making it jump from 100 to 135, that left a space of 134 (hex) bytes open. So I used that space to input my string. This is kind of an example of how to create an empty space that you can use like a variable.

So what's "int"? "int" just means "interrupt" (not integer). This interrupts your program and runs a command. The 21st interrupt (int 21) interrupts your command and runs "ah" or "ax". And since we defined "ah" as "09", it now knows it should display a string. And since I defined "dx" as "102", it now knows where, in data, it should find the string.

So "int 20"? That one's simple. "int 20" just terminates your code. Now why would be put a terminator at the end? Because your code won't terminate on itself, and it won't know how, so it will crash. This is just so it closes properly.

Oh yeah, and "mov ah, 08". What's that? Well, like I said, "ah" is your command register. You have two commands for reading keys: "08" and "01". "01" will display the key you type, "08" will hide the key you type. We are using this command (then an "int 21" to execute it) so we can just pause for user input, but we don't care the key they press so we don't do anything with it. Retrieving keys and acting on them requires the knowledge of conditional jumps, flags, comparisons, and some other keys, which I am not going to explain here.


And that's about it. Again, I know a lot more about this. Some commands and registers I didn't go over were "jne", "je", "cmp", "ret", "al", "cx", "bx", I didn't go over tags and a lot more. This gets a lot more complex.


If you are running a 32bit OS, you can use the "debug < file.asm" command to run other people's codes. Here is my Assembly code, or as I like to call "DASM" (because there are many different kinds of assemblers; such as NASM, FASM, MASM, etc... and since this is the DEBUG Assembler, I think of it as "DASM"):

Code: Select all

a 100
jmp 140
db 'Hello, World!', 0D, 0A, 'Press any key to continue . . .$'

a 140
mov ah, 09
mov dx, 102
int 21
mov ah, 08
int 21
int 20

rcx
50
n hello.com
w
q



Just copy the code (including the line after the "q"), and paste it in Notepad. Now save the file as "file.asm", and to your desktop. Now start CMD (start -> run -> cmd) and type in "cd desktop", press enter, then type in "debug < file.asm" to assemble the code to a "hello.com" file.


This tutorial will not have you completely understanding. It's meant to allow you to know what's going on, so if you play around with DEBUG and start to understand it, you can refer back to this knowledge and it will be easy to make sense of.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How To Use The DEBUG Command To Assemble COM Files

#2 Post by Ed Dyreen » 03 Sep 2011 03:50

'
Bookmarked 8)

Post Reply