Suppose that the hardware of a computer contains an instruction :-
MOVE (source, dest, length)
that copies a character string of
length bytes from an address specified by
source to an address specified by
dest.
The length must be specified by an integer.
An example of this instruction is MOVE (
a,
b,3) which copies the three bytes starting at location a to the three bytes starting at location b.
NOTE : To refer to a location that is 4 bytes beyond a, the notation
a[4] will be used to refer to this location.
Also, to refer to the address given by adding the binary integer constants of the byte at
x to the address
a, the notation a[x] is used.
The MOVE operation requires a programmer to specify the length of the string to be copied. Thus its operand is a fixed character length string(the lenght of the string must be known).
Here, a fixed length string and a byte-sized binary integer may be considered as "native" data type for this "particular machine".
Problem :
Now, suppose we wished to implement varying-length character strings on this particular machine. That is, we want to enable programmers to use an instruction
MOVEVAR (source,dest)
to move a character string from location source to location dest without being required to specify any length.
Solution :
To
implement this data type, we must first decide how it is to be represented in the memory of the machine and then indicate how that representation is to be manipulated. Clearly, it is necessary to know how many bytes must be moved to execute this instruction. Since the MOVEVAR operation doesn't specify this number, therefore the number must be contained within the representation of the character string itself. A varying-length character string of length l may be represented by contiguous set of
l+1 bytes (
l<256). The first byte contains the binary representation and the remaining bytes contain the representations of the characters in the string.
The program to implement the MOVEVAR operation can be written as follows :-
MOVE (source,dest,1);
for (i=1; i<dest; i++)
{
MOVE(source[i], dest[i], 1);
}
Source of the problem : Refer page 20
(
Screenshot of the page)
My Question :-
Can someone please explain the logic of this implementation of MOVEVAR instruction i.e., the algorithm of the implementation (How it works ?)
What I have tried:
I tried to look up for the explanation but couldn't find it.