0

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
3
  • If you changed your code to use fixed sizes so you could do address = array0 + size*idx, why did you accept the linear-search answer? Commented Jun 5, 2021 at 13:01
  • Actually both of them solved my problem. Thank you both. Commented Jun 5, 2021 at 17:51
  • ok, but the search way is a lot less efficient, and you'd only write it that way if you aren't making all your strings the same length, to make an array of records. Commented Jun 5, 2021 at 21:40

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Interesting! That ended up being more compact than I expected, and doesn't even need range-checking because rep checks for CX==0 before doing even the first compare and increment. Of course with a very high BX like 65535 or 0, it could be quite slow. (Hmm, I think you do need to test bx,bx / jz to skip searching entirely, unless you put a dummy '$' before the first real string. BX = 0 should give you array0, not 65536 iterations of banging your head against arrayEnd :P)
Thanks it works good. well how could I generate random number. I'm totally strange to asm I have experience java, C# so asm looks like rocket science to me :)
@4GDri True randomness is very difficult to obtain. For a toy project I would prefer to get seconds of current time MOV AH,2Ch; INT 21h, divide them by string-array length (10) and use the remainder as a pseudorandom number 0..9.
Hi, thanks I used this code from opened topic in here. [url]stackoverflow.com/questions/17855817/…
3

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
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, ...

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.

(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.)


To index what you currently have (a flat concatenation of strings), you'd probably have to linear search for the 4th 24h byte. The 5th string starts after that byte.

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. Don't do that; put your data at the end.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.