如何根据

时间:2019-05-04 20:09:53

标签: assembly x86

我的问题是我试图使用户输入成为一个循环,并且每次我想将该输入存储到内存中的某个位置以便以后访问它并进行一些更改后将其打印出来。我对于如何首先声明一个可以容纳例如5个单词的数组,以及后来如何每次将输入存储到该数组中感到困惑。

我确实在使用主题名称:c ++中的循环看起来像这样:

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

我在整个互联网和YouTube上进行了研究,发现无法在汇编中声明字符串数组,基本上只有一个字节数组,后跟一个空终止符。我了解了这一点,并且使用它做了代码,并且可以正常工作,但是问题是我确实需要将5个主题代码存储到5个不同的变量中(或至少存储在内存中),因为稍后经过一些计算后,我需要打印回去这些主题。

    ;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

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

存储完每个字符串后,我希望在程序末尾执行该操作:

subject1: SCSJ134
subject2: SCSR231
Subject3: SCSI392

一直到Subject5

2 个答案:

答案 0 :(得分:0)

这是一种方法。这等效于C代码:

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

答案 1 :(得分:0)

这是另一种方法。这等效于C代码:

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