Asm and Windows ----Linking lib GP fault problem---- 
Author Message
 Asm and Windows ----Linking lib GP fault problem----

   Hello ASM Gurus,

I am novice in Windows ASM and practically
all Programming. However I am very intrested
just for fun.
So here is my silly problem with windows asm
and library.
I get GP every time if I dont copy cs to ds in
my library, is it right thing to do and if so,
why assembler is not taking care of it with these
definitions? Anyway it builds Prolog/Epilog, right?

I am using Borland TASM 5.0

Here are the codes; LIB fully and EXE source parts
what I tought important. Rest of exe is from TASM 5.0
example WAP, just added library call...

----------This is MyLib.lib---------------------------------------------

locals
jumps
.model large, WINDOWS Pascal

include windows.inc

PUBLIC int21Beeb

extrn DOS3CALL:PROC

.data
.code
.286

int21Beep   PROC
     push cs       ;without this I always got GP fault...
            pop  ds       ;is this safe in 16 bit windows?
     mov  ah, 02h
     mov  dl, 07h  ;^G
     call DOS3CALL

int21Beep   ENDP
ENDS
END
----------MyLib.lib End---------------------------------------------------

----------This is MyExe.exe-----------------------------------------------
locals
jumps
.model large, WINDOWS PASCAL
include windows.inc
extrn   int21Beep:PROC               ;in MyLib.lib, just experiment
extrn   DOS3CALL:PROC
extrn   BEGINPAINT:PROC
extrn   CREATEWINDOW:PROC
...
etc
...
wmrbuttondown:
       call    int21Beep    ;in MyLib.lib, just experiment

      ;mov  ah, 02h         ;when I compile function here
      ;mov  dl, 07h    ;not in library it works fine...
      ;call DOS3CALL        ;
...
etc
...
----------MyExe.exe End-----------------------------------------------------

So once again before further building my libraries,
is it safe and right to copy CS to DS in 16 bit Windows
library function?
Still I dont understand this, because this is not DLL.
Linker puts code from lib to different segment or what!

Thanks in advance,

Gary



Sat, 14 Apr 2001 03:00:00 GMT  
 Asm and Windows ----Linking lib GP fault problem----

Quote:

>    Hello ASM Gurus,

> I am novice in Windows ASM and practically
> all Programming. However I am very intrested
> just for fun.
> So here is my silly problem with windows asm
> and library.
> I get GP every time if I dont copy cs to ds in
> my library, is it right thing to do and if so,
> why assembler is not taking care of it with these
> definitions? Anyway it builds Prolog/Epilog, right?

[snip-code]

Quote:
> So once again before further building my libraries,
> is it safe and right to copy CS to DS in 16 bit Windows
> library function?
> Still I dont understand this, because this is not DLL.
> Linker puts code from lib to different segment or what!

Large model programs have more than one data segment: one so called far
data segment for each of the modules (ie. your lib has one of its own,
also, your exe has one of its own) and a default data segment (in
DGROUP). In the code you didn't show whether the exe loads the DS with a
proper value or not (which it should, in the beginning of the program).
Also, you should set up your stack (SS:SP), too, in the beginning of the
program. I'm not sure what the model large, WINDOWS, PASCAL does, but
basically you should be able to write functions/procedures without them,
anyway. Just take care that you won't corrupt the stack. If you use
Pascal calling convention, your called-function should clean up the
possible parameters (hence, you'll have to know how many bytes of data
your function gets as parameters, then you do a ret n where n = number
of bytes to clean up). If you use C calling convention, it's the
responsibility of the caller to do that.

Changing a segment register (any one of them) shouldn't be a problem if
you

1) save the segment register before changing it
2) change and use it with a new value
3) restore the original value when done

This is done automatically for CS, but for DS or ES (and the SI and DI
registers that are often used by HLL compilers for register variables)
you'll have to do it yourself (the prolog/epilog doesn't usually take
care of that). Actually, the prolog/epilog is nothing more than a stack
frame for accessing local variables/function parameters, similar to
this:

        push    bp
        mov     bp,sp
        ; the function code using bp[-n] for local vars, bp[+n] for
        ; function parameters
        pop     bp
        ret

You can see if your dataseg problem vanishes, if you compile/link your
program using small memory model (one data and one code segment) instead
of large (>= 1 data and >= 1 code segment).

        AriL
--
Humans may send email (if absolutely necessary) to the
obvious non-spam address.



Sun, 15 Apr 2001 03:00:00 GMT  
 Asm and Windows ----Linking lib GP fault problem----

: So once again before further building my libraries,
: is it safe and right to copy CS to DS in 16 bit Windows
: library function?
: Still I dont understand this, because this is not DLL.
: Linker puts code from lib to different segment or what!

No, it's not safe.  The segment registers contain *selectors* instead
of segment values.  The selector in CS defines a not-writable segment,
and Win16 runs in protected mode.  The selector in DS is the default
for most memory accesses. So you'll get an exception for attempting to
store (write) data into memory:

        mov     [bx],ax         ; [bx] is the same as DS:[bx]
        mov     memvar,ax       ; DS is default "segment"

--
Henry S. Takeuchi

Seattle, Washington (USA)



Sun, 15 Apr 2001 03:00:00 GMT  
 Asm and Windows ----Linking lib GP fault problem----

Quote:

> So once again before further building my libraries,
> > is it safe and right to copy CS to DS in 16 bit Windows
> > library function?

you shouldn't have needed to, if DS got set up properly at the beginning ofthe
program.

Quote:
> program. I'm not sure what the model large, WINDOWS, PASCAL does, but
> basically you should be able to write functions/procedures without them,

yes you can, BUT, it does three things.  Forces case insensitivity on
publics and externs if you happen to assemble with the case sensitive swith;
and initializes the extended call syntax to use the proper pascal calling
convention.
That extended call syntax is very useful for windows programs, I recommend
TASM users use it.  Finally, if you speciy arguments to a procedure it turns on
the pascal calling convention and fixes the procedural RET the way it should be.

--
---------------------------------------------------------------

http://www.ladsoft.com                                     (computer page)
http://www.geocities.com/Area51/Station/5196/index.html    (home page)
http://www.geocities.com/Area51/Station/5196/ttc.html      (tao te ching)



Mon, 16 Apr 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. GP Fault using collect:

2. Q: GP Fault when compiling with TC 3.0

3. Linking sqldedev.lib, desktopenginedeploy.lib to my app: unsupported recordtype 21

4. Static vs Shared: Linking tcl83s.lib vs tcl83.lib

5. Statically Linking tcl.lib and tk.lib into a standalone .exe file

6. Long File Names - problem linking dblfn.lib

7. Link Problems with ext.LIB (CW4/CW5EE)

8. strange Link problem (win32.lib)

9. problems linking .LIB

10. Alsys linking with metaware C and DVX socket lib problems

11. Tcl83.lib link problem

12. asm and high-level linking, stack problem, please help

 

 
Powered by phpBB® Forum Software