虽然另一个答案是正确的,但如果假设字符串数组的概念类似于像 C 这样的高级语言,那么你实际上是在处理指针数组(偏移量)内存中其他地方的字符串。您可以创建一个包含这些偏移量(指针)的数组,指向字符串本身。
例如,C 程序可能会这样定义数组:
#include <stdio.h>
int main()
{
char *strarray[] = { "Shirt$", "Pants$", "Socks$" };
printf("%s", strarray[2]);
printf("%s", strarray[1]);
return 0;
}
以下在功能上是等效的,但我们已命名字符串:
#include <stdio.h>
int main()
{
char *str1 = "Shirt$";
char *str2 = "Pants$";
char *str3 = "Socks$";
char *strarray[] = { str1, str2, str3 };
printf("%s", strarray[2]);
printf("%s", strarray[1]);
return 0;
}
后一个示例我将用于在 16 位汇编中重现类似的代码。它使比较汇编代码和C代码变得更加容易。
以下是一个用 MASM/TASM/JWASM 汇编语言编写的 16 位 DOS 程序,它创建 3 个单独的字符串并为每个字符串创建一个指针(偏移量)数组。然后它索引指针数组以获取要打印的字符串的地址。此示例显示了 2 种访问数组的技术。一个索引在内存操作数中编码,另一个将索引放置在寄存器中(在这种情况下为BX)。
.model small
.stack 256
.data
str1 DB 'Shirt$'
str2 DB 'Pants$'
str3 DB 'Socks$'
; Create an array of pointers (offsets) to the strings in memory
strarray DW OFFSET str1, OFFSET str2, OFFSET str3
.code
start:
; Setup the DS register to point at .data
mov ax, @data
mov ds, ax
; TYPE operator returns the size of an element in strarray.
; 2 in this case since we defined strarray with elements
; of type word (DW)
; Get the pointer stored in 3rd element of strarray to DX
mov dx, strarray[2*(TYPE strarray)]
; Print the string using DOS function call
mov ah, 9h
int 21h
; Alternatively you can access the array element through
; a register like BX, SI, DI
; Get the offset of the 2nd element into BX
mov bx, 1*(TYPE strarray)
; Get the pointer stored in 2nd element of strarray to DX
mov dx, strarray[bx]
; Print the string using DOS function call
mov ah, 9h
int 21h
; Exit program
mov ax, 4C00h
int 21h
end start
使用 MASM 的 x86 Intel 语法,内存操作数看起来像数组索引,但它们不是。 [] 之间的值是 BYTES 中的索引。因此,您总是必须将索引乘以数组中元素的大小。 NEAR 指针是 16 位代码中的 2 个字节,因此每个元素的字节索引必须乘以 2。在上面的代码中,我使用返回数组元素大小的 TYPE 运算符。 strarray 定义为 DW(16 位字数组),因此返回值 2,因为每个元素的大小为 2 个字节。
这段代码的输出是:
SocksPants