Search code examples
visual-studio-2010assemblymasmirvine32

Creating array and adding values


So i am working on an assignment and i am having some issues understanding arrays in this type of code (keep in mind that my knowledge of this stuff is limited). My code is supposed to ask the user to enter the number of values that that will be put in an array of SDWORD's and then create a procedure that has the user input the numbers. I have the part done below that asks the user for the amount (saved in "count") but i am struggling with the other procedure part For example with my code below if they enter 5 then the procedure that i have to make would require them to input 5 numbers that would go in to an array.

The problem I am facing is that i'm not sure how to actually set up the array. It can contain anywhere between 2 and twelve numbers which is why i have the compare set up in the code below. Let's say for example that the user inputs that they will enter 5 numbers and i set it up like this...

.data
array SDWORD 5

the problem i am having is that I'm not sure if that is saying the array will hold 5 values or if just one value in the array is 5. I need the amount of values in the array to be equal to "count". "count" as i have set up below is the amount that the user is going to enter.

Also i obviously know how to set up the procedure like this...

EnterValues PROC

    return
EnterValues ENDP    

I just don't know how to implement something like this. All of the research that i have done online is only confusing me more and none of the examples i have found ask the user to enter how many number will be i the array before they physically enter any numbers in to it. I hope what i described makes sense. Any input on what i could possibly do would be great!

INCLUDE Irvine32.inc
.data
count SDWORD ?
prompt1 BYTE "Enter the number of values to sort",0
prompt2 BYTE "Error. The number must be between 2 and 12",0

.code

Error PROC
    mov edx, OFFSET prompt2
    call WriteString
    exit         ; exit ends program after error occures
Error ENDP

main PROC
    mov edx, OFFSET prompt1
    call WriteString    ; prints out prompt1
    call ReadInt
    mov count, eax     ; save returned value from eax to count

    cmp count, 12     
    jle Loop1       ; If count is less than or equal to 12 jump to Loop1, otherwise continue with Error procedure
    call Error      ; performs Error procedure which will end the program

    Loop1: cmp count, 2
    jge Loop2    ; If count is greater than or equal to 2 jump to Loop2, otherwise continue with Error procedure
    call Error   ; performs Error procedure which will end the program

     Loop2: exit
main ENDP
END main

============EDIT==============

I came up with this...

EnterValues PROC
    mov ecx, count
    mov edx, 0
    Loop3: 
       mov eax, ArrayOfInputs[edx * 4] 
       call WriteInt
       call CrLf 
       inc edx 
       dec ecx 
       jnz Loop3
    ret
EnterValues ENDP

Solution

  • .data
    array SDWORD 5
    

    defines one SDWORD with the initial value 5 in the DATA section and gives it the name "array".

    You might want to use the DUP operator

    .data
    array SDWORD 12 DUP (5)
    

    This defines twelve SDWORD and initializes each of them with the value 5. If the initial value doesn't matter, i.e. you want an uninitialized array change the initial value to '?':

    array SDWORD 12 DUP (?)
    

    MASM may now create a _BSS segment. To force the decision:

    .data?
    array SDWORD 12 DUP (?)
    

    The symbol array is used in a MASM program as a constant offset to the address of the first entry. Use an additional index to address subsequent entries, for example:

    mov eax, [array + 4]    ; second SDWORD
    mov eax, [array + esi]
    

    Pointer arithmetic:

    lea esi, array          ; copy address into register
    add esi, 8              ; move pointer to the third entry
    mov eax, [esi]          ; load eax with the third entry
    
    lea esi, array + 12     ; copy the address of the fourth entry
    mov eax, [esi]          ; load eax with the fourth entry
    

    You've got in every case an array with a fixed size. It's on you, just to fill it with count values.