Article View: comp.lang.pascal.borland
Article #176Assigning & concatenating strings
From: Robert AH Prins
Date: Wed, 12 Jul 2000 00:00
Date: Wed, 12 Jul 2000 00:00
100 lines
3455 bytes
3455 bytes
Hi all,
How do you put a literal in a string?
Uh, what's wrong with "myline:= 'This is an example';"
Nothing, but now assume that you're doing this many times in a program,
and look at the code generated to make this assignment, you'll see that
it comes to 18 bytes, excluding the string itself.
How do you concatenate a string with a literal?
Uh, what's wrong with "myline:= myline + 'This is an example';"
Nothing, but now assume that you're doing this many times in a program,
and look at the code generated to make this assignment, you'll see that
it comes to 39 bytes, excluding the string itself.
Wouldn't it be nice if you could shorten this? Faced with 20 of such
assignments, and 18 concatenations in a program, I came up with this,
but be warned that it _does not contain any error checking_, in other
words, you have to know what you are doing!
var myline: string; {must be global}
procedure cs_2_line; far; assembler;
asm
push bp
mov bp, sp
mov dx, ds
mov es, dx
lds si, [bp + 2] {get start of string}
mov [bp + 2], cx {set return address past string}
mov al, [si] {get info byte, $00: fill, $ff: concatenate}
inc si
sub cx, si {length of string}
seges; and byte ptr myline, al {set length to zero/keep original}
mov di, offset myline
mov bl, es:[di] {get current length og myline}
xor bh, bh
add es:[di], cl {set new length}
inc bx
add di, bx {start position of destination}
inc cx {I know what I'm doing, and}
shr cx, 1 {want to move two bytes at a time}
cld
rep movsw
mov ds, dx
pop bp
end; {cs_2_line}
Moving a literal to myline now becomes:
asm
mov cs, offset @1
call cs_2_line
db $00
db "This is an example"
@1:
end;
and concatenating something to it is almost the same with:
asm
mov cs, offset @1
call cs_2_line
db $ff
db "This will be concatenated"
@1:
end;
My savings? 710 bytes...
Decide for yourself if you think it's useful!
Robert
PS: Q: What's the difference between:
myline:= myline + 'this is a veryyyyyyyyyyyyyyyyyyyyyyyyyyy ' +
'looooooooooooooooooooooooooooooooooooong ' +
'extension to myline';
and
myline:= myline + ('this is a veryyyyyyyyyyyyyyyyyyyyyyyyyyy ' +
'looooooooooooooooooooooooooooooooooooong ' +
'extension to myline');
A: BP does not fold the constant in the first example at compile time!
--
Robert AH Prins
prino@bigfoot.com
Sent via Deja.com http://www.deja.com/
Before you buy.
Technical Details
Message-ID:
<8khf2d$7g4$1@nnrp1.deja.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111659,429640dfcaaf13a8,start X-Google-Attributes: gid111659,public X-Google-Thread: 108ebd,429640dfcaaf13a8,start X-Google-Attributes: gid108ebd,public From: Robert AH Prins <prino@bigfoot.com> Subject: Assigning & concatenating strings Date: 2000/07/12 Message-ID: <8khf2d$7g4$1@nnrp1.deja.com> X-Deja-AN: 645404948 X-Http-Proxy: 1.0 cache.suffolkcc.gov.uk:8080 (Squid/2.2.STABLE2), 1.0 x67.deja.com:80 (Squid/1.1.22) for client unknown, 193.123.213.8 Organization: Deja.com - Before you buy. X-Article-Creation-Date: Wed Jul 12 09:51:13 2000 GMT X-MyDeja-Info: XMYDJUIDprino Newsgroups: comp.lang.pascal.borland,borland.public.turbopascal X-Http-User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 95; DigExt)