1

Today, I have learned that the SEG operator in MASM by default returns the address of the GROUP and not the address of the SEGMENT of the expression in question, and that there are options and methods to override that.

Since I am currently doing a complex project in Open Watcom 1.9 / 16 bit DOS where C and assembly (inline and standalone) are mixed (actually, DOS is only needed for startup, then my own MINI-OS takes control), and since I know that WASM is somewhat MASM compatible, I have the following question:

When doing inline assembly and taking the segment of a variable, does the SEG operator return the GROUP or the SEGMENT which the variable is in?

Suppose there is a GROUP named MY_GROUP, a SEGMENT named MY_SEG which belongs to MY_GROUP, and a variable / label named MY_VAR which is placed in MY_SEG.

Then, if I do

_asm {
MOV AX, SEG MY_VAR
}

which value is loaded into AX? Is it the address of MY_GROUP or the address of MY_SEG (given that they are different)?

I did not find any command line switch which relates to that problem in inline assembly. I then tried the MASM syntax:

_asm {
MOV AX, SEG MY_GROUP:MY_VAR
MOV AX, SEG MY_SEG:MY_VAR
}

Both of the lines above lead to the following error: "Only segment or group label is allowed".

Please note that my problem only relates to inline assembly. Actually, I am using JWASM for the standalone assembly modules, and the syntax above works well and with the expected results there.

Could anybody tell me what the inline assembler does in this situation, and if there are means how I could control its respective behavior?

Thank you very much!

Binarus
  • 4,005
  • 3
  • 25
  • 41

1 Answers1

1

I don't think there's any way to convince the OpenWatcom compiler to emit a group based segment relocation. Part of the problem is that there's no way to declare or define the group so that you can refer to it in the inline assembly.

However, it appears the OpenWatcom linker will ignore the fact that the relocations are segment based and instead use the group the segment belongs to as the base. So assuming you are using wlink then in your first example AX would be loaded with a segment value that points to the start of MY_GROUP. On the other hand, if you use Microsoft's segmented linker it then AX will contain a segment value that points to MY_SEG.

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
  • Yes, I am using wlink. Does the linker fix OFFSET as well, i.e. if I do MOV AX, OFFSET MY_VAR, does the compiler emit the offset relative to the segment (and not the group), and does the linker convert it to an offset relative to the group (as with SEG)?. Otherwise, SEG and OFFSET would not be consistent ... – Binarus Jun 15 '15 at 06:38
  • It probably works the same way, but I didn't test that. – Ross Ridge Jun 15 '15 at 06:49
  • Obviously, we can't be sure how the inline assembler works, and it's documented nowhere. Japheth's website, OpenWatcom's website and news server are currently down so I can't ask any questions there. Thus, I will move all definitions of global variables which are shared between C and assembly out of the C modules into standalone assembly modules so that I can control in which segment and group they really end up, and I will convert every piece of inline assembly which accesses these variables into standalone assembly where I can control (by directives and notation) how SEG and OFFSET work. – Binarus Jun 16 '15 at 08:09
  • @user3239842v Yah, that's the safest thing to do. I checked and OFFSET is handled the same way by wlink. – Ross Ridge Jun 16 '15 at 08:20