When Will the Code Under DATA SEGMENT execute in this code?

Muthu Ganapathy Nathan picture Muthu Ganapathy Nathan · Mar 16, 2011 · Viewed 25.9k times · Source

I am a beginner of Assembly Programming... I surfed a lot in google. There is a lot of information, but I still do not understand the following code. I would be grateful if someone could explain

MOV AX,DATA

I also don't understand when the code that is present in data segment would be executed in this program.

ASSUME CS:CODE,DS:DATA

CODE SEGMENT
 MOV AX,@DATA
 mov DS,AX
 ...
 ...
CODE ENDS


DATA SEGMENT
   ...
   ...
   ... //SOMECODE
DATA ENDS 

Also, can someone explain to me what the following instructions do?..

MOV AH , ??H ( ?? can be filled with 09,4c etc).

MOV DS,AX

MOV ES,AX

Answer

Joe Phillips picture Joe Phillips · Mar 16, 2011

Warning: I've never written 8086 assembly in my life but this is what I make of it.

MOV AX,@DATA is the first line of code that gets run. I believe @DATA is a variable that holds the value of the location in memory where the data segment lives. It moves the memory location of @DATA into the AX register (16 bit register). One thing to note here is the @ in front of the word DATA. I believe this is because @DATA is evaluated during the linking process and it will be replaced by its actual value. Notice how the other examples to not have the @ in front because they are referring to an actual memory location to begin with.

MOV DS,AX will then set that memory location as the variable DS which is a standard variable (or register in this case) for 8086 assembly. It should always point to the location of your storage where you want to keep values (the heap if you're familiar with C++ terminology).

The AX register is simply a temporarily place holder that you can load with values and perform execute commands against.

MOVE AH, ??H First of all, the AH refers to the "high" side of the AX register. The brother of this would be AL which refers to the "low" side of the AX register. This is used when you want to perform commands against 8 bits instead of 16 bits. The second part of this, the ??H as you refer to it is the value you want to store in the AH register. The H at the end means "hexadecimal". So if you have 00H that would mean zero (in hex). If you put in FFH that would be the same as 255 in decimal number system.

Back to your initial question "When Will the Code Under DATA SEGMENT execute in this code?" -- I believe you're asking when the DATA SEGMENT will be executed. This normally should not be executed because it's supposed to store data (variables) for use in your CODE SEGMENT. On some operating systems you can get around this I believe and simply JUMP or BRANCH to that section of code and treat it as a regular CODE SEGMENT. This is sometimes how stack overflows, heap overflows, (hacks), etc, all work.