2

I'm just confused why we should initialize the DS register going with all this :

data segment
msg1 db 10,13, "saisir le premier digit:$"
data ends
mov ax,data
mov ds,ax` 

when the first variable is stored its stored on data segment so isn't already ds=data why we should initialize ds=data if they already the same

  • 2
    Is this a DOS `.EXE` program. If so by default when the DOS loader loads an executable (`.EXE`) it sets _DS_ (and _ES_) to the segment where the [PSP](https://en.wikipedia.org/wiki/Program_Segment_Prefix) is. DS:0 and ES:0 point to the base of the PSP. You need to explicitly set _DS_ to the segment containing the data. – Michael Petch Mar 05 '17 at 19:11
  • Appears this question may be closely related to http://stackoverflow.com/questions/3715618/how-does-dos-load-a-program-into-memory – Michael Petch Mar 05 '17 at 19:12

1 Answers1

1

when the first variable is stored its stored on data segment

I think there's a misconception. You need to make a distinction between the compile-time and the run-time.

It was your compiler (assembler) that put the msg1 text in the data section of your program. We don't actually call this storing a variable and furthermore there's no particular setting of the DS segment register involved.

However at run-time, when you want to retrieve or store these variables, the DS segment register needs to point at the data section. Since at program start this is not the case, it's up to you to do that explicitly.

1 data segment
2 msg1 db 10,13, "saisir le premier digit:$"
3 data ends
4 mov ax,data
5 mov ds,ax

Do note that the execution didn't start at the 1st line of this snippet of code, but rather at the 4th! That's another way to see that there wasn't any prior store on the data segment.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76