Abdullah Al Sulaiman
Abdullah Al Sulaiman

Reputation: 35

how to access an array of strings in a loop to store a string into the array according

My problem is I am trying to take user input in a loop and every time I want to store that input into a place in the memory to access it later and print it with some changes. And I am being confused regarding how to first declare an array that can hold my 5 words for example, and later how to store the input every time into that array.

Exactly I am taking names of subjects: and the loop in c++ would look something like that:

string subjects_code[5]
for(int i=0; i<5; i++)
    cin>>subjects_code[i];   
   // like AFJS421 , CSFA424, SCSJ1023 and so on

I did my research all over the internet and YouTube, I found that you can't declare an array of strings in assembly, you basically have a single array of bytes followed by a null terminator. I understand that and I did my code with it and it is working, but the problem is I really need to store the 5 subjects codes into 5 different variables (or at least memory locations), because later after some calculations I need to print back those subjects.

    ;taking input from user: in a Loop   
    ;in .data I have subjects_code BYTE MAX DUP(?)
    MAX = 20
mov ebx,0
mov count, 5   ; cuz ReadString uses ecx as buffersize

InputLoop:
            ; This is just a prompt out, no need to worry about it

    mov ecx, MAX    
    mov edx, OFFSET Enter_code       ;  setting offset for prompt   


            ; temp variable to read into it, use it for assgining
    mov edx, OFFSET temp_subject_code  
    call ReadString                 ; reading the code into temp
    mov subjects_code+[ebx], temp_subject_code


    add ebx, 4
    mov ecx, count
    dec count

    Loop InputLoop

;---------------------------------------------------------------

After storing every string, I expect to do at the end of the program:

subject1: SCSJ134
subject2: SCSR231
Subject3: SCSI392

all the way up to Subject5.

Upvotes: 0

Views: 730

Answers (2)

prl
prl

Reputation: 12435

Here's one approach. This is the equivalent of the C code:

char subject_code[5][20];
for(int i=0; i<5; i++)
    ReadString(subject_code[i]);

.

    MAXLEN = 20
    COUNT = 5
    mov ebx,0

InputLoop:
    mov eax, MAXLEN
    mul ebx
    lea edx, subjects_code[eax]  
    mov ecx, MAXLEN-1
    call ReadString                 ; reading the code into subject_code[ebx]

    inc ebx
    cmp ebx, COUNT
    jnz InputLoop

    mov ebx, 0
OutputLoop:
    mov ecx, MAXLEN
    mov eax, ebx
    mul ecx ; this can be done without mul since MAXLEN is a constant
    lea edx, subjects_code[eax]
    call WriteString
    call Crlf

    inc ebx
    cmp ebx,COUNT
    jl OutputLoop

    .data
subjects_code BYTE MAXLEN*COUNT DUP(?)

Upvotes: 1

prl
prl

Reputation: 12435

Here's another approach. This is the equivalent of the C code:

char *subject_code[5];
for(int i=0; i<5; i++) {
    subject_code[i] = malloc(20);
    ReadString(subject_code[i]);
}

.

    MAXLEN = 20
    COUNT = 5
    mov ebx,0

InputLoop:
    mov ecx, MAXLEN
    call malloc
    mov subjects_code[ebx*4], eax
    mov edx, eax  
    mov ecx, MAXLEN
    call ReadString                 ; reading the code into subject_code[ebx]

    inc ebx
    cmp ebx, COUNT
    jnz InputLoop

    .data
subjects_code DWORD COUNT DUP(?)

Upvotes: 1

Related Questions