Hello,
I try to understand a function block where a string is copied from an address (source) to another address (destination) by using pointers and SFC20 (BLKMOV).
The function that does the job has the following code:
TAR1 #TAR1
TAR2 #TAR2
// Copies the UserName To the SRC
L P##UserName
LAR1
L P##Any_SRC
LAR2
L 5
L1: T #CNT
L W [AR1,P#0.0]
T W [AR2,P#0.0]
L P#2.0
+AR1
+AR2
L #CNT
LOOP L1
// Copies the CurrentUser to Dest
L P##CurrentUser
LAR1
L P##Any_DEST
LAR2
L 5
L2: T #CNT
L W [AR1,P#0.0]
T W [AR2,P#0.0]
L P#2.0
+AR1
+AR2
L #CNT
LOOP L2
LAR1 #TAR1
LAR2 #TAR2
// Copies the blocks
CALL SFC 20
SRCBLK :=#Any_SRC
RET_VAL:=#ReturnValue
DSTBLK :=#Any_DEST
Where the parameters of the FB are like this:
Name, Data_Type, Address, Interface_of_FB
UserName, Any, 10.0, IN
CurrentUser, Any, 30.0, IN
Any_SRC, Any, 0.0, TEMP
Any_DEST, Any, 10.0, TEMP
The FB is called with the following parameters:
UserName := P#DB420.DBX24.0 BYTE 16
CurrentUser := P#DB410.DBX136.0 BYTE 16
Content of DB420 is:
Address, Type, Initial_Value
+22.0, STRING[16], ''
Content of DB410 is:
Address, Type, Initial_Value
+134.0, STRING[16], ''
I think the bold part of FB does this things:
L 5 // AC1 = 5 (index)
L1: T #CNT // #CNT = AC1 (5)
L W [AR1,P#0.0] // AC1 = First Word from P##UserName[0]
T W [AR2,P#0.0] // AC2 = AC1 copy to P##Any_SRC[0] a Word from P##UserName[0]
L P#2.0 // AC1 = P#2.0
+AR1 // AR1 = AR1 + AC1; P##UserName[2.0] will be addresd in the first LOOP, then P##UserName[4.0] until P##UserName[8.0] (5 passes)
+AR2 // AR2 = AR2 + AC1; P##Any_SRC[2.0], and so on
L #CNT // AC1 = #CNT
LOOP L1 // AC1 = AC1 -1;
// if (AC1 > 0) JMP to L1
// else -> the program execution goes to the next line
The problem
If my understanding is correct (and please tell me) those instructions will copy only the first 5 Words from P##UserName to P##Any_SRC. This means that only 10 Bytes [0-9] are copied, but P##UserName points to 16 Bytes, starting from DB420.DBX24.0 and so 6 Bytes are suppoesed to remain uncopied. But in practice this FB is working well, all the characters from source string are copied to the destination string.
Please help me to solve this mistery !!!
I try to understand a function block where a string is copied from an address (source) to another address (destination) by using pointers and SFC20 (BLKMOV).
The function that does the job has the following code:
TAR1 #TAR1
TAR2 #TAR2
// Copies the UserName To the SRC
L P##UserName
LAR1
L P##Any_SRC
LAR2
L 5
L1: T #CNT
L W [AR1,P#0.0]
T W [AR2,P#0.0]
L P#2.0
+AR1
+AR2
L #CNT
LOOP L1
// Copies the CurrentUser to Dest
L P##CurrentUser
LAR1
L P##Any_DEST
LAR2
L 5
L2: T #CNT
L W [AR1,P#0.0]
T W [AR2,P#0.0]
L P#2.0
+AR1
+AR2
L #CNT
LOOP L2
LAR1 #TAR1
LAR2 #TAR2
// Copies the blocks
CALL SFC 20
SRCBLK :=#Any_SRC
RET_VAL:=#ReturnValue
DSTBLK :=#Any_DEST
Where the parameters of the FB are like this:
Name, Data_Type, Address, Interface_of_FB
UserName, Any, 10.0, IN
CurrentUser, Any, 30.0, IN
Any_SRC, Any, 0.0, TEMP
Any_DEST, Any, 10.0, TEMP
The FB is called with the following parameters:
UserName := P#DB420.DBX24.0 BYTE 16
CurrentUser := P#DB410.DBX136.0 BYTE 16
Content of DB420 is:
Address, Type, Initial_Value
+22.0, STRING[16], ''
Content of DB410 is:
Address, Type, Initial_Value
+134.0, STRING[16], ''
I think the bold part of FB does this things:
L 5 // AC1 = 5 (index)
L1: T #CNT // #CNT = AC1 (5)
L W [AR1,P#0.0] // AC1 = First Word from P##UserName[0]
T W [AR2,P#0.0] // AC2 = AC1 copy to P##Any_SRC[0] a Word from P##UserName[0]
L P#2.0 // AC1 = P#2.0
+AR1 // AR1 = AR1 + AC1; P##UserName[2.0] will be addresd in the first LOOP, then P##UserName[4.0] until P##UserName[8.0] (5 passes)
+AR2 // AR2 = AR2 + AC1; P##Any_SRC[2.0], and so on
L #CNT // AC1 = #CNT
LOOP L1 // AC1 = AC1 -1;
// if (AC1 > 0) JMP to L1
// else -> the program execution goes to the next line
The problem
If my understanding is correct (and please tell me) those instructions will copy only the first 5 Words from P##UserName to P##Any_SRC. This means that only 10 Bytes [0-9] are copied, but P##UserName points to 16 Bytes, starting from DB420.DBX24.0 and so 6 Bytes are suppoesed to remain uncopied. But in practice this FB is working well, all the characters from source string are copied to the destination string.
Please help me to solve this mistery !!!