I want to access to specific element of array using number that generated randomly.
for example,
I want to generate random number 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.
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.
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
An alternative way to Peter's solution is to scan for the $-terminator random_number
times:
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.