Thread View: comp.lang.pascal.borland
2 messages
2 total messages
Started by Robert AH Prins
Wed, 12 Jul 2000 00:00
Assigning & concatenating strings
Author: 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.
Re: Assigning & concatenating strings
Author: Osmo Ronkanen
Date: Wed, 12 Jul 2000 00:00
Date: Wed, 12 Jul 2000 00:00
43 lines
2324 bytes
2324 bytes
In article <8khf2d$7g4$1@nnrp1.deja.com>,
Robert AH Prins <prino@bigfoot.com> wrote:
>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!
Why not just write two procedures:
Procedure AssignString(var st:string; st2:string);
Procedure ConcatStr(var st;string; st2:string);
The implementations are trivial. That is just 13 bytes per call. Now if
you want to use open strings, it is 16 bytes per call. Those are for
near calls, fars use more.
No need to write any ASM code as the procedure code is included only
once no matter how many times it is used.
>var myline: string; {must be global}
Do you realize what nightmare these "must be global" things cause. Also
even if one adds to a global variable one still can use Pascal.
Osmo
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads