1{
2    This file is part of the Free Pascal run time library.
3    Copyright (c) 1999-2000 by Jonas Maebe, member of the
4    Free Pascal development team
5
6    Processor dependent part of strings.pp, not shared with
7    sysutils unit.
8
9    See the file COPYING.FPC, included in this distribution,
10    for details about the copyright.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
16 **********************************************************************}
17
18
19{$if defined(FPC_MM_TINY)}
20  {$define FPC_X86_CODE_NEAR}
21  {$define FPC_X86_DATA_NEAR}
22{$elseif defined(FPC_MM_SMALL)}
23  {$define FPC_X86_CODE_NEAR}
24  {$define FPC_X86_DATA_NEAR}
25{$elseif defined(FPC_MM_MEDIUM)}
26  {$define FPC_X86_CODE_FAR}
27  {$define FPC_X86_DATA_NEAR}
28{$elseif defined(FPC_MM_COMPACT)}
29  {$define FPC_X86_CODE_NEAR}
30  {$define FPC_X86_DATA_FAR}
31{$elseif defined(FPC_MM_LARGE)}
32  {$define FPC_X86_CODE_FAR}
33  {$define FPC_X86_DATA_FAR}
34{$elseif defined(FPC_MM_HUGE)}
35  {$define FPC_X86_CODE_FAR}
36  {$define FPC_X86_DATA_HUGE}
37{$else}
38  {$fatal No memory model defined}
39{$endif}
40
41
42{$ifndef FPC_UNIT_HAS_STRPCOPY}
43{$define FPC_UNIT_HAS_STRPCOPY}
44function strpcopy(d : pchar;const s : string) : pchar;assembler;nostackframe;
45const
46  { used for an offset fixup for accessing the proc parameters in asm routines
47    that use nostackframe. We can't use the parameter name directly, because
48    i8086 doesn't support sp relative addressing. }
49{$ifdef FPC_X86_CODE_FAR}
50  extra_param_offset = 2;
51{$else FPC_X86_CODE_FAR}
52  extra_param_offset = 0;
53{$endif FPC_X86_CODE_FAR}
54asm
55  mov bx, sp
56  mov dx, ds  // for far data models, backup ds; for near data models, use to initialize es
57{$ifdef FPC_X86_DATA_NEAR}
58  mov es, dx
59  mov si, ss:[bx + 2 + extra_param_offset]  // @s
60  mov di, ss:[bx + 4 + extra_param_offset]  // @d
61{$else FPC_X86_DATA_NEAR}
62  lds si, ss:[bx + 2 + extra_param_offset]  // @s
63  les di, ss:[bx + 6 + extra_param_offset]  // @d
64{$endif FPC_X86_DATA_NEAR}
65  // we will no longer use bx for reading parameters, so save di there
66  // in order to be able to return it in the end
67  mov bx, di
68
69{$ifdef FPC_ENABLED_CLD}
70  cld
71{$endif FPC_ENABLED_CLD}
72  lodsb        // load length in al
73  xor ah, ah
74  xchg cx, ax  // 1 byte shorter than mov
75  shr cx, 1
76  rep movsw
77  adc cx, cx
78  rep movsb
79  xchg ax, cx  // ax := 0 (1 byte shorter than xor al, al)
80  stosb        // zero terminate the destination string
81{$if defined(FPC_X86_DATA_FAR) or defined(FPC_X86_DATA_HUGE)}
82  mov ds, dx
83  mov dx, es   // return segment of d in dx
84{$endif}
85  xchg ax, bx  // return original offset of d in ax
86end;
87{$endif FPC_UNIT_HAS_STRPCOPY}
88