Search code examples
assemblyx86masm32irvine32

Assembly newbie requesting help: Putting something in an array


Ultimately, what I am trying to do is make requestInput repeat 25 times and store the input it receives from each iteration into the following index in theSINTArray, but I'm not sure how to put something into an array. The looping I will take care of later, but how would I make it so that the first iteration of requestInput puts the received input into index 0, the second iteration puts the received input into index 1 and so on?

.data
theSINTArray BYTE 25 dup(?)              
prompt BYTE "Please enter a value: ",0


.CODE

main PROC

push    TYPE theSINTArray
push    LENGTHOF theSINTArray
push    OFFSET theSINTArray
call    requestInput
exit

main ENDP

requestInput PROC

push    edx                                        
mov     edx,OFFSET prompt                          
mov     edi,OFFSET theSINTArray                    
call    WriteString                                
call    ReadInt                                    
pop     edx                                        
ret

requestInput ENDP


END     main

My second attempt based on the answer by @SepRoland:

.data
theSINTArray BYTE 25 dup(?)              
prompt BYTE "Please enter a value: ",0


.CODE

main PROC

push    TYPE theSINTArray
push    LENGTHOF theSINTArray
push    OFFSET theSINTArray
call    requestInput
exit

main ENDP

requestInput PROC

Next:
push    edx                                        
mov     edx,OFFSET prompt                          
call    WriteString                                
call    ReadInt 

mov     edx, offset theSINTArray
mov     [edx], al
inc     edx
cmp     edx, offset theSINTArray + 25
jb      Next

pop     edx                                        
ret

requestInput ENDP


END     main

Solution

  • You defined theSINTArray as an array of bytes, so you'll need an instruction such as mov [edx], al to store the value and then use inc edx to point to the next element in the byte sized array.

     mov   edx, offset theSINTArray
    Next:
     call  requestInput
     mov   [edx], al
     inc   edx
     cmp   edx, offset theSINTArray + 25
     jb    Next
    

    In response to your effort to put the value-assignment within the requestInput procedure.

    • You've put the Next label above the push edx instruction. This means that the pop edx must be done within the loop.
    • You've re-initialized the pointer on each iteration, and so will always be writing in the first array element. Move this above the loop.

    This is how:

    requestInput PROC
     mov     edx, offset theSINTArray
    Next:
     push    edx                                        
     mov     edx,OFFSET prompt                          
     call    WriteString                                
     call    ReadInt
     pop     edx 
     mov     [edx], al
     inc     edx
     cmp     edx, offset theSINTArray + 25
     jb      Next
     ret
    requestInput ENDP