5

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

BinaryTox1n
  • 3,486
  • 1
  • 36
  • 44
Muthu Ganapathy Nathan
  • 3,199
  • 16
  • 47
  • 77

2 Answers2

7

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.

Joe Phillips
  • 49,743
  • 32
  • 103
  • 159
  • Phillips:Then Can i give as MOV DS,@DATA? Is it Legal? And MOV AX,[5000h] is not working in my assembler Please say about this too... – Muthu Ganapathy Nathan Mar 17 '11 at 01:10
  • I believe `MOV DS,@DATA` would be legal but I'm not sure. It depends on the assembler you're using I think. I believe you're using Intel syntax which I'm not familiar with unfortunately. I don't really see anything wrong with `MOV AX,[5000h]` but you could try `MOV AX,5000h` instead. – Joe Phillips Mar 17 '11 at 01:13
  • Phillips:Sir, if I do MOV AX,[5000h] the value in [5000] will be copied. But if I do MOV AX,5000h the 5000 will be copied. There is lot of difference here. Am I right sir..? – Muthu Ganapathy Nathan Mar 17 '11 at 01:17
  • Yes, you are correct. I believe what you are curious about is something called "addressing modes". There are different kinds. Again, I come from a motorola background so I know very little about how Intel addressing modes work. There is tons of information on google :) – Joe Phillips Mar 17 '11 at 01:31
  • 4
    The `@data` will cause the linker to emit a relocation entry in the DOS EXE header. `@data` is the segment value of the `data` section. The linker will emit a segment value relative to its position in the executable as a place holder, but that will be modified (via the relocation table in the EXE header) by the DOS loader by adding the segment where the DOS executable was physically loaded into memory with the segment value emitted by the linker. Effectively `@data` is only fully known at runtime. – Michael Petch Aug 19 '16 at 17:12
3

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.

Florent
  • 12,310
  • 10
  • 49
  • 58