简体   繁体   English

使用 integer 索引访问字符串数组的特定元素

[英]Accessing specific element of array of strings, with an integer index

I want to access to specific element of array using number that generated randomly.我想使用随机生成的数字访问数组的特定元素。

for example,例如,

I want to generate random number 0-9.我想生成随机数 0-9。 After number is generated (in this example we assume random number is generated as 4), I want to access array5 and print screen.生成数字后(在本例中,我们假设随机数生成为 4),我想访问 array5 并打印屏幕。

How could I success this.我怎么可能成功。

String array initialization code is below字符串数组初始化代码如下

I edited my code as equal size string like below.我将我的代码编辑为相同大小的字符串,如下所示。 Each strings have 8 char.每个字符串有 8 个字符。

How can I generate random number?如何生成随机数?

org 100h

array0:    db      "abstract", 0Dh,0Ah, 24h
array1:    db      "academic", 0Dh,0Ah, 24h
array2:    db      "accurate", 0Dh,0Ah, 24h
array3:    db      "bacteria", 0Dh,0Ah, 24h
array4:    db      "attorney", 0Dh,0Ah, 24h
array5:    db      "equation", 0Dh,0Ah, 24h
array6:    db      "umbrella", 0Dh,0Ah, 24h
array7:    db      "overcome", 0Dh,0Ah, 24h
array8:    db      "universe", 0Dh,0Ah, 24h
array9:    db      "analysis", 0Dh,0Ah, 24h

That's not an array;那不是数组; the elements aren't all the same length.元素的长度不同。

You need to pad out to some fixed max size so you can scale an index like C您需要填充到某个固定的最大大小,以便您可以缩放像 C 这样的索引
struct {char c[16];} arr[10]; to make an array of fixed-size string buffers.制作一个固定大小的字符串缓冲区数组。
Or make a separate array of pointers like arr: dw array0, array1, ...或者创建一个单独的指针数组,例如arr: dw array0, array1, ...

Initialise array of strings in assembly shows examples of both ways. 在程序集中初始化字符串数组显示了两种方式的示例。 For your case, to pad the entries you might use align 16 before each, which would emit padding bytes at that position until the current address is a multiple of 16.对于您的情况,要填充条目,您可能会在每个条目之前使用align 16 ,这将在该 position 处发出填充字节,直到当前地址是 16 的倍数。

(Scaling an index by 16 would normally be shl di, 4 or whatever register, but original 8086 doesn't have immediate shifts. So mov cl,4 / shl di, cl if you need compatibility with ancient hardware / emulators.) (将索引缩放 16 通常是shl di, 4或任何寄存器,但原始 8086 没有立即移位。所以mov cl,4 / shl di, cl如果您需要与古代硬件/模拟器兼容。)


To index what you currently have (a flat concatenation of strings), you'd probably have to linear search for the 4th 24h byte.要索引您当前拥有的内容(字符串的平面串联),您可能必须线性搜索第 4 个24h字节。 The 5th string starts after that byte.第 5 个字符串在该字节之后开始。

Also, if you actually put this data right at the top of a file for a .com executable (org 100h), the string data will execute as code.此外,如果您实际上将此数据放在.com可执行文件(org 100h)的文件顶部,则字符串数据将作为代码执行。 Don't do that;不要那样做; put your data at the end.把你的数据放在最后。

An alternative way to Peter's solution is to scan for the $ -terminator random_number times:彼得解决方案的另一种方法是扫描 $ -terminator random_number次:

     MOV BX,random_number   
     MOV DI,offset array0  ; Beginning of strings. 
     MOV CX,offset arrayEnd; A label below all "arrays".
     SUB CX,DI             ; Let CX=total size of all strings.
     MOV AL,24h            ; '$' which terminates each string.
     CLD                   ; Ascending scan.
Next:REPNE SCASB           ; Search for '$', start at ES:DI.
     DEC BX                ; 
     JNZ Next              ; Repeat random_number times.
     MOV DX,DI             ; DI now points to the desired string.
     MOV AH,9              ; Use DOS function to print it. 
     INT 21h               ; Output $-terminated string at DS:DX.
     RET                   ; Terminate COM program.
    ; Data section follows:
array0:  db "Car", 0Dh, 0Ah, 24h  
 ...
arrayEnd:db 24h             ; End of strings 

The code is longer and slower, but it saves space in data section, because the strings don't have to be stuffed to unified length.代码更长更慢,但它节省了数据部分的空间,因为字符串不必填充到统一长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM