Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Inline Assembler

108 views
Skip to first unread message

Grrrrrattwotoads

unread,
Oct 17, 2001, 8:00:49 PM10/17/01
to
When I try to run the following code I get an error message that says

raised exception class EAccessViolation with message
'Access violation at address 0044d38c in module
'SHAPE.EXE' Read of address FFFFFFFF'. Process stoped.

I am using Delphi 4.0 Standard. Thanks from Michael Pell

function UpperCase(TestString: String): String;
begin
asm
PUSH DS
CLD
LDS SI,TestString
LES DI,@Result
LODSB
STOSB
XOR AX,AX
XCHG AX,CX
JCXZ @3
@1:
LODSB
CMP AL,'a'
JB @2
CMP AL,'z'
JA @2
SUB AL,20h
@2:
STOSB
LOOP @1
@3:
POP DS
end;
end;
procedure TForm2.Button1Click(Sender: TObject);
var
TestString : String;
begin
TestString := 'a';
Edit2.Text := UpperCase(TestString);
end;

Stéphane Martineau

unread,
Oct 17, 2001, 9:23:38 PM10/17/01
to
You're writing segmented 16 bits assembly in a "flat" 32 bits memory model.
Why are you doing that ? I think you'll have to learn a lot more about 32
bits assembly before using it in delphi. Also there's already an uppercase
function in delphi and if you want to write your own you should write it
using object pascal code, the assembly version won't really be much faster
anyway (could even be slower if you don't know what you're doing, like you
seem to).

But if you still want to do it here a few hints, opcode like stosb,lodsb use
the 32 bits version of si and di meaning esi, edi. Also, segment registers
are fixed and are normally never changed inside a process, there's no need
since offsets are 32 bits and allow access to 4GB of flat memory space.
Also, string under delphi are dynamically allocated and reference counted
object, so what you're doing in this method won't work with them, you will
have to use shortstring if you want to do it like you were in turbo pascal.
Here how the code would look like for shortstring:

function UpperCase (const TestString: ShortString): ShortString; assembler;
asm
cld
push esi // Delphi doc say that esi,edi,ebp,esp must be preserved
push edi
mov esi,eax // TestString ptr is passed in edx
mov edi,edx // Result ptr is passed in eax
xor eax,eax
lodsb
stosb
mov ecx,eax
jcxz @3
@1:
lodsb
cmp al,'a'
jb @2
cmp al,'z'
ja @2
sub al,20h
@2:
stosb
loop @1
@3:
pop edi
pop esi
end;

procedure TForm2.Button1Click(Sender: TObject);
var
TestString : shortString;


begin
TestString := 'a';
Edit2.Text := UpperCase(TestString);
end;

But it's not efficient since Edit2.text expect a string not a shortstring
(delphi compiler will generate code to create a string from a shortstring
automatically) You can see the code generated if you put a breakpoint and
press ctrl-alt-c if you want and trace the code from there.


Grrrrrattwotoads

unread,
Oct 18, 2001, 3:51:47 PM10/18/01
to
Thanks Stephane for setting me straight. The last time I
used assembler was on a 286, so needless to say, Delphi was
a dream come true. I jumped on VB 1.0 but the bigger the
app the harder it was to use and later versions were worse.
I e-mailed Microsoft suggesting they create a visual
assembler but never got a response. Along came Delphi and
I switched. It seems very close to being a visual assembler
as I see a lot of similarities. I feel Pascal is superior to
'C' in ease of use and Basic isn't even in the same league.
My intent is not uppercase conversion but rather string
parsing. This was only a simple string test function. I was
able to use inline assembler for math functions but strings
were a problem. I want to be able to take the following sample
lines from a listbox, extract x and y values,calculate and add
a z value for a 3D plane.
N1 G1 X1.2345 Y1.2345
N2 G1 X2.2345 (if no Y then use last Y)
N3 G1 X3.2345 Y2.2345
While I'm sure this is possible with Delphi's string routines
It seems like it might be easier in assembler using scans and
stos. The thought of 32 bit assembler and no messing with
segments is also very exciting -to me anyway-. Thanks again
for the help. I think USENET is the most exciting thing to
come to computing since the GUI. Sincerely, Michael Pell

Stéphane Martineau

unread,
Oct 18, 2001, 11:25:41 PM10/18/01
to
> Thanks Stephane for setting me straight. The last time I
> used assembler was on a 286, so needless to say, Delphi was
> a dream come true. I jumped on VB 1.0 but the bigger the
> app the harder it was to use and later versions were worse.
> I e-mailed Microsoft suggesting they create a visual
> assembler but never got a response. Along came Delphi and
> I switched. It seems very close to being a visual assembler
> as I see a lot of similarities. I feel Pascal is superior to
> 'C' in ease of use and Basic isn't even in the same league.

I agree for VB I don't touch that shit, and you're right, delphi/pascal is
more user friendly then C/C++ and have some functionality that I would like
to have in C++ (imo C need another update, C++ is getting old, they should
take note of java), but object pascal also lack a lot of features that C++
have, imo borland is pretty cheap when it come the time to enhance it's OP
compiler. But even with all it's limitations and probrems I'm not aware of
anything that's nicer then delphi. (It's a little bit sad :(

For an assembler, well few peoples code in assembly these days, the market
for it is so small that I doubt any big software company would take the risk
to invest time and money it would take to create one. But the inline
assembler in delphi is useful.

> While I'm sure this is possible with Delphi's string routines
> It seems like it might be easier in assembler using scans and
> stos

Not sure if it's gonna be easier. But feel free to have fun with it :)

. The thought of 32 bit assembler and no messing with
> segments is also very exciting -to me anyway-.

:)

> Thanks again for the help.

No problem.

> I think USENET is the most exciting thing to come to
> computing since the GUI. Sincerely, Michael Pell

Hum, it's useful, but imo it's not very good, most news server keep messages
and thread only for short period of time and there's not that many peoples
in it. Messages board can be better sometime, but good ones are rare. Let
say that the internet is good, but a little bit of order would be nice (I'm
a dreamer :)

Have fun with delphi :)

J French

unread,
Oct 19, 2001, 4:24:07 AM10/19/01
to
<snip>

.386 Asm which is what Delphi32 supports is a damn sight easier than
.286 Asm - as one does not have to worry about segments.

However since the .486 certain simple instructions run significantly
faster than the more complex ones like REP MOVSB

This means that 'Noddy' assembler is far more efficient than hand
crafted ASM - and Delphi's optimizing Assembler knows all about this,
so in many cases it is hard to beat pure Delphi code.

Sadly it means that inline ASM is only really useful for operations
that 'de-obfuscate' code

Duncan Murdoch

unread,
Oct 19, 2001, 7:54:40 AM10/19/01
to
On Thu, 18 Oct 2001 23:25:41 -0400, "Stéphane Martineau"
<sma...@mail.com> wrote:
>> I think USENET is the most exciting thing to come to
>> computing since the GUI. Sincerely, Michael Pell
>
>Hum, it's useful, but imo it's not very good, most news server keep messages
>and thread only for short period of time and there's not that many peoples
>in it.

Go to groups.google.com, to search archives that are remarkably
complete (the old dejanews.com archives are there, as well as newer
and older stuff).

Duncan Murdoch

Sundial Services

unread,
Oct 19, 2001, 12:39:32 PM10/19/01
to
Assembler is used nowadays only for low-level routines that are provable
"hot spots." And I mean, "provable." Consequently it is used less and
less. Inline assembler is no longer needed and it has been wisely
discontinued.

The fact that microprocessors are "so damned fast" nowadays, and getting
faster, is also a consideration. (At fifty million ops per second,
nobody can hear you scream.)


>J French wrote:
>
> <snip>
>
> .386 Asm which is what Delphi32 supports is a damn sight easier than

0 new messages