Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-11

Move with pointers to memory

10 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Rainer Stratmann

http://www.freepascal.org/docs-html/rtl/system/move.html

I have a working function in assembler wich copys a memory block from sorce to destination.

Now I want it to do with the move function from unit system, but it does not work.

procedure wd_copy( s , d : pointer ; l : longint );
begin
 move( s , d , l ); // does not work! Access violation errors
end;

procedure wd_copy( s , d : pointer ; l : longint ); begin
if l > 0 then begin

  if d < s then begin
   asm
     pushf
     mov   esi,s
     mov   edi,d
     mov   ecx,l
     cld              // vorwärts
     rep   movsb
     popf
   end['EAX','EBX','ECX','EDX','ESI','EDI'];
  end
  else begin
   asm
     pushf
     mov   esi,s
     add   esi,l
     dec   esi
     mov   edi,d
     add   edi,l
     dec   edi
     mov   ecx,l
     std              // rückwärts
     rep   movsb
     popf
   end['EAX','EBX','ECX','EDX','ESI','EDI'];
  end;

end;
end;

Frederic Da Vitoria
to Rainer Stratmann

2013/11/13 Rainer Stratmann

Show quoted text 21 lines
On Wednesday 13 November 2013 09:39:27 you wrote: > > Move (S^, D^, L) > > should do it. > > Tomas Yes, thanks. I am confused with the meaning of const source; and var dest; Is there a description of the behaviour of const and var in this case? Why is it not both const or both var?

"source" is where the data "comes from", so the procedure won't modify it (if there is no memory overlap between source and dest), so it is declared as "const"
"dest" is where the data is copied to, so the procedure will necessarily modify it (if "count" is 0), and the procedure should modify so it is declared as "var". Actually, IMO it could be declared as "out", this would avoid a meaningless warning.

Frederic Da Vitoria (davitof) Membre de l'April - « promouvoir et défendre le logiciel libre » - http://www.april.org
Rainer Stratmann
to Frederic Da Vitoria
you wrote 11 lines
2013/11/13 Rainer Stratmann > Is there a description of the behaviour of const and var in this case? > Why is it not both const or both var? "source" is where the data "comes from", so the procedure won't modify it (if there is no memory overlap between source and dest), so it is declared as "const" "dest" is where the data is copied to, so the procedure will necessarily modify it (if "count" is 0), and the procedure should modify so it is declared as "var". Actually, IMO it could be declared as "out", this would avoid a meaningless warning.

But why does it work without specifying the vars more exactly like in other cases?

Frederic Da Vitoria
to Rainer Stratmann

2013/11/13 Rainer Stratmann

Show quoted text 18 lines
On Wednesday 13 November 2013 10:52:23 you wrote: > 2013/11/13 Rainer Stratmann > > Is there a description of the behaviour of const and var in this case? > > Why is it not both const or both var? > > "source" is where the data "comes from", so the procedure won't modify it > (if there is no memory overlap between source and dest), so it is declared > as "const" > "dest" is where the data is copied to, so the procedure will necessarily > modify it (if "count" is 0), and the procedure should modify so it is > declared as "var". Actually, IMO it could be declared as "out", this would > avoid a meaningless warning. But why does it work without specifying the vars more exactly like in other cases?

You mean, how can it work without using typed variables? In Pascal, you don't have to give the type of a parameter, although 99.99% of the times you should do it. If you don't do it, you could as well use assembly :-) Actually, that's probably how Move is written: in assembly language. Of course, since the types were not given in the parameter declaration of Move, the compiler can't know the size of the data, and you have to give the compiler some indication. That's what "count" is for. Move considers the memory as 2 big arrays of bytes, one starting at "source", and the second at "dest".

Frederic Da Vitoria (davitof) Membre de l'April - « promouvoir et défendre le logiciel libre » - http://www.april.org
Sven Barth
to Frederic Da Vitoria
Frederic Da Vitoria wrote 37 lines
2013/11/13 Rainer Stratmann < > On Wednesday 13 November 2013 10:52:23 you wrote: > 2013/11/13 Rainer Stratmann < > > > Is there a description of the behaviour of const and var in this case? > > Why is it not both const or both var? > > "source" is where the data "comes from", so the procedure won't modify it > (if there is no memory overlap between source and dest), so it is declared > as "const" > "dest" is where the data is copied to, so the procedure will necessarily > modify it (if "count" is 0), and the procedure should modify so it is > declared as "var". Actually, IMO it could be declared as "out", this would > avoid a meaningless warning. But why does it work without specifying the vars more exactly like in other cases? You mean, how can it work without using typed variables? In Pascal, you don't have to give the type of a parameter, although 99.99% of the times you should do it. If you don't do it, you could as well use assembly :-) Actually, that's probably how Move is written: in assembly language. Of course, since the types were not given in the parameter declaration of Move, the compiler can't know the size of the data, and you have to give the compiler some indication. That's what "count" is for. Move considers the memory as 2 big arrays of bytes, one starting at "source", and the second at "dest".

The fallback implementation (for new architectures) is written in Pascal. See $fpcdir\rtl\inc\generic.inc.

Regards,
Sven

Frederic Da Vitoria
to Sven Barth

2013/11/13 Sven Barth

Show quoted text 38 lines
Am 13.11.2013 11:07, schrieb Frederic Da Vitoria: 2013/11/13 Rainer Stratmann > On Wednesday 13 November 2013 10:52:23 you wrote: > > 2013/11/13 Rainer Stratmann > > > Is there a description of the behaviour of const and var in this case? > > > Why is it not both const or both var? > > > > "source" is where the data "comes from", so the procedure won't modify > it > > (if there is no memory overlap between source and dest), so it is > declared > > as "const" > > "dest" is where the data is copied to, so the procedure will necessarily > > modify it (if "count" is 0), and the procedure should modify so it is > > declared as "var". Actually, IMO it could be declared as "out", this > would > > avoid a meaningless warning. > > But why does it work without specifying the vars more exactly like in > other > cases? > You mean, how can it work without using typed variables? In Pascal, you don't have to give the type of a parameter, although 99.99% of the times you should do it. If you don't do it, you could as well use assembly :-) Actually, that's probably how Move is written: in assembly language. Of course, since the types were not given in the parameter declaration of Move, the compiler can't know the size of the data, and you have to give the compiler some indication. That's what "count" is for. Move considers the memory as 2 big arrays of bytes, one starting at "source", and the second at "dest". The fallback implementation (for new architectures) is written in Pascal. See $fpcdir\rtl\inc\generic.inc.

Of course it is, I should have guessed it, Lazarus/FPC is multi-platform. I keep thinking as if I was using a Windows-only development tool.

Frederic Da Vitoria (davitof) Membre de l'April - « promouvoir et défendre le logiciel libre » - http://www.april.org
Sven Barth
to Frederic Da Vitoria
Frederic Da Vitoria wrote 49 lines
2013/11/13 Sven Barth < > Am 13.11.2013 11:07, schrieb Frederic Da Vitoria: > 2013/11/13 Rainer Stratmann < > > > > On Wednesday 13 November 2013 10:52:23 you wrote: > > 2013/11/13 Rainer Stratmann < > > > > > Is there a description of the behaviour of const and var > in this case? > > > Why is it not both const or both var? > > > > "source" is where the data "comes from", so the procedure > won't modify it > > (if there is no memory overlap between source and dest), so > it is declared > > as "const" > > "dest" is where the data is copied to, so the procedure > will necessarily > > modify it (if "count" is 0), and the procedure should > modify so it is > > declared as "var". Actually, IMO it could be declared as > "out", this would > > avoid a meaningless warning. > > But why does it work without specifying the vars more exactly > like in other > cases? > > > You mean, how can it work without using typed variables? In > Pascal, you don't have to give the type of a parameter, although > 99.99% of the times you should do it. If you don't do it, you > could as well use assembly :-) Actually, that's probably how Move > is written: in assembly language. Of course, since the types were > not given in the parameter declaration of Move, the compiler > can't know the size of the data, and you have to give the > compiler some indication. That's what "count" is for. Move > considers the memory as 2 big arrays of bytes, one starting at > "source", and the second at "dest". The fallback implementation (for new architectures) is written in Pascal. See $fpcdir\rtl\inc\generic.inc. Of course it is, I should have guessed it, Lazarus/FPC is multi-platform. I keep thinking as if I was using a Windows-only development tool.

Most architectures do have a (often much shorter) implementation in assembly language though which is located in $fpcdir\rtl\$cpu\$cpu.inc.

Regards,
Sven

leledumbo
to Rainer Stratmann: Yes, thanks. I am confused with the meaning of const source; and var dest; Is there a…
Is there a description of the behaviour of const and var in this case?

Not so clear: http://www.freepascal.org/docs-html/ref/refsu59.html A liiiitle bit clearer:
http://docwiki.embarcadero.com/RADStudio/XE5/en/Parameters_(Delphi)#Untyped_Parameters

Why is it not both const or both var?

If the Dest is const, then it cannot be written to. If Src is var, then the value might be modified while it's not expected to be. Moreover, const has no exact passing mechanism, the compiler may choose to pass by reference or value. Var is always by reference, another reason to mark Dest as var.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Move-with-pointers-to-memory-tp5717477p5717489.html Sent from the Free Pascal - General mailing list .