Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 727
Muthu Ganapathy Nathan Avatar asked Mar 16 '11 18:03

Muthu Ganapathy Nathan


People also ask

Is the data segment executable?

The data segment is an area of memory that can be both read and written by the program; that is, it contains global variables. Similarly to the text segment, a portion of the executable file is copied to the data segment when a program is about to be executed. This portion is called the initialized data.

What is data segment and code segment?

Each segment is used to contain a specific type of data. One segment is used to contain instruction codes, another segment stores the data elements, and a third segment keeps the program stack.

What is a data segment used for?

Data Segmentation is the process of taking the data you hold and dividing it up and grouping similar data together based on the chosen parameters so that you can use it more efficiently within marketing and operations. Examples of Data Segmentation could be: Gender.

What is data segment in compiler?

A data segment is a portion of the virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer. Note that, the data segment is not read-only, since the values of the variables can be altered at run time.


2 Answers

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.

like image 73
Joe Phillips Avatar answered Sep 24 '22 23:09

Joe Phillips


Mov ax,@data is way of loading starting address of data segment in ax. then by using mov ds,ax data segment gets initialized. this instruction is used in tasm assembler.

like image 25
Nikhil Kulkarni Avatar answered Sep 26 '22 23:09

Nikhil Kulkarni