实施“非本地”背后的逻辑数据类型 [英] Logic behind the implementation of the "non-native" data type

查看:64
本文介绍了实施“非本地”背后的逻辑数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设计算机的硬件包含指令: -

Suppose that the hardware of a computer contains an instruction :-

MOVE (source, dest, length) // Hardware instructions are presented using UPPERCASE letters



复制的字符串从 source 指定的地址到 dest 指定的地址的长度字节。



长度必须由整数指定。



此指令的一个示例是MOVE( a b , 3)将从位置a开始的三个字节复制到从位置b开始的三个字节。



注意:引用一个位置超过a的4个字节,符号 a [4]将用于指代此位置。



另外,参考通过将 x 的字节的二进制整数常量加到地址 a 给出的地址,使用符号a [x]。



MOVE操作要求程序员指定要复制的字符串的长度。因此,它的操作数是一个固定的字符长度字符串(必须知道字符串的长度)。



这里,固定长度的字符串和字节大小的二进制整数可能被视为此特定机器的原生数据类型。



问题

现在,假设我们希望在这台特定的机器上实现变长字符串。也就是说,我们希望程序员能够使用指令


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将字符串从位置源移动到位置dest,而无需指定任何长度。



解决方案



实现 这种数据类型,我们必须首先决定如何在机器的内存中表示它,然后指明它是如何代表是被操纵的。显然,有必要知道必须移动多少字节才能执行该指令。由于MOVEVAR操作未指定此数字,因此该数字必须包含在字符串本身的表示中。长度为l的变长字符串可以由连续的<1> 1 +1个字节( 1 <256)表示。第一个字节包含二进制表示,其余字节包含字符串中字符的表示。



实现MOVEVAR操作的程序可以写成如下: -


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);
}





问题根源:参见第20页

页面截图







我的问题: -





有人可以解释这个MOVEVAR指令实现的逻辑,即实现的算法(它是如何工作的?)



我试过的:



我试图查找解释但找不到它。



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.

推荐答案

假设源数据的第一个字节包含字符串长度,我们可以使用以下内容:

Assuming that the first byte of the source data contains the string length we could have something like:
--------------------------
|  4 |  A |  B |  C |  D |
--------------------------



所以MOVE循环会be:


So the MOVE loop would be:

// move the length byte from the source to the destination
MOVE (source,dest,1);  // dest[0] now contains 4
for (i=1; i <dest; i++)="" set="" i="1," and="" while="" <="" 4="" do="" the="" following<!--="" newline="" --="">{
    MOVE(source[i], dest[i], 1); // move the source byte at offset i to 
                                 // the byte at offset i of the destination
} // increment i and repeat while the value of i is less than 4



这将失败,因为循环将因此在复制偏移量4处的字节之前终止:




This will fail since the loop will terminate before copying the byte at offset 4 thus:

Value of i   copy byte
    1           A
    2           B
    3           C
    4 -> loop terminates since i is not less than byte 0 of dest.



所以for循环需要是


so the for loop needs to be

for (i=1; i <= dest; i++)


大多数指令集使用寄存器实现这一点,寄存器在复制每个字节后自动递增。要复制的编号放在一个特殊的寄存器中,该寄存器会在每次重复复制时倒计时。请参阅英特尔指令集 - REP [ ^ ]。
Most instruction sets implement this using registers that automatically increment after each byte is copied. The number to copy is put in a special register that counts down for each repetition of the copy. See Intel Instruction Set - REP[^].


这篇关于实施“非本地”背后的逻辑数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆