简体   繁体   中英

Assembly: data segment when called from C or created as an independent program

I'm confused about this; I don't think that there should be any difference in both cases , the program ends up as exe file. Please help if you think a differ....

Let me clarify my question: Is there is a difference in the data segment definition or handling
between when I create an assembly program 'stand alone' and when I call for an assembly routine from a C program?

Who is defining the location or size of the date segment in both cases? Is this the compiler, or the operating system? And how the value of the data segment determined in both cases?

Depend of operating system!

If we are looking for windows operating system under IA32 then API reserve virtual memory address space of some application and:

  • CS segment, point to the start of program or code memory.
  • DS segment, point to the start of variable or data memory.
  • SS segment, point to the start of stack memory and is the same as DS.
  • ES as extra segment normally is in use for string transfer instructions (lodsb, stosw, ...) and is the same as DS.
  • FS as another extra segment point on OS kernel data like Win32 Thread Information Block .
  • GS as another extra segment is 0 as beginning of allocated virtual memory address space of loaded application.

Exsample of accessing 'Win32 Thread Information Block' via FS segment:

function GetThreadId: integer;
//result := GetCurrentThreadId;
asm
  mov   eax, fs:[$18]      //eax := thread information block
  mov   eax, [eax + $24]   //eax := thread id
end; { GetThreadId }

Sheck also: x86 memory segmentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM