2

For my x86 Assembly Language class we were given an assignment wherein we needed to take in 12 integers and store them in an array called Autumn, and then display them. My question is why with this code:

INCLUDE Irvine32.inc
.data
Prompt1  BYTE "Please input a value:", 0
Autumn   WORD 12 DUP(?) 

.code
main PROC
    mov esi, OFFSET Autumn
    mov ecx, LENGTHOF Autumn
    call PromptForIntegers

    mov edi, OFFSET Autumn
    mov ecx, LENGTHOF Autumn

L2:
    mov eax, [edi]
    add edi, TYPE Autumn
    call WriteInt               ;Displays value in EAX
    call Crlf                   ;Moves to next output line
    loop L2

exit
main ENDP

;-----------------------------------------------------
PromptForIntegers PROC USES ecx edx esi
;
; Prompts the user for an arbitrary number of integers 
; and inserts the integers into an array.
; Receives: ESI points to the array, ECX = array size
; Returns:  nothing
;-----------------------------------------------------

    mov edx,OFFSET prompt1  

L1: call    WriteString     ; display string
    call    ReadInt         ; read integer into EAX
    call    Crlf            ; go to next output line
    mov [esi],eax           ; store in array
    add esi,TYPE SWORD      ; next integer
    loop    L1

    ret
PromptForIntegers ENDP
END main

When I input the integers 1 - 12 into the array. It prints out:

+131073
+196610
+262147
+327684
+393221
+458758
+524295
+589832
+655369
+720906
+786443
+12

The only correct output is the +12. The rest should be the numbers 1-11.

EDIT: Got it working for positive numbers by changing mov eax, [edi] to mov ax, [edi] in L2

rkhb
  • 14,159
  • 7
  • 32
  • 60
Riley Emance
  • 41
  • 1
  • 5
  • 1
    `Autumn WORD 12 DUP(?) ` seems wrong since you seem to be moving 32 bit data to it (ie. `mov [esi],eax`). Maybe you meant `Autumn DWORD 12 DUP(?) ` ? Also think you need to change `add esi,TYPE SWORD` to `add esi,TYPE Autumn` – Michael Petch Oct 08 '15 at 01:47
  • 1
    A bulk of you code is in the include file, but for purposes of your initial question, the comment hints `ECX = array size`. The `ecx` register is your loop counter. The value in `ecx` will be decremented by `1` each iteration until it is `0`. You need to set this value before you begin the loop. Also, if there is anything within the loop that can clobber the value of `ecx` you need to save the loop count by storing it, or pushing it onto the stack, do the computations, then pop it back of the stack before you reach `loop L1` (push at start of loop, pop right before end) – David C. Rankin Oct 08 '15 at 04:08
  • 1
    @DavidC.Rankin Kip's Irvine32 library preserves all the registers except for return values for functions that actually return something (most often in _eax_). The OP's code is okay in that regard. I think _ecx_ is properly defined with `mov ecx, LENGTHOF Autumn` which is the number of elements in the _Autumn_ array. – Michael Petch Oct 08 '15 at 06:17
  • 1
    See, I learned something. I had no idea what was being the `INCLUDE Irvine32.inc`. Glad to know it is solid. – David C. Rankin Oct 08 '15 at 07:30

0 Answers0