Question

I'm new to Assembly Programming (x86), and cannot figure out where I am going wrong in my program. After I redisplay the value that was moved into the array, I then want to display the current 'SUM'. I thought that by using the 'ebx' register, since it is used nowhere else in the program except in Loop2, that the value would not be overwritten and thus each 'add' statement would add the new array position value to my 'SUM'.

Can anyone spot what I'm doing wrong?

ConsoleWindow

INCLUDE Irvine32.inc
COUNT = 3

.data
inputMsg BYTE "Input an integer: ", 0
outputMsg BYTE "Redisplaying the integers: ", 0dh, 0ah, 0
sumMsg BYTE " Sum is now: ", 0
strArray SDWORD COUNT DUP(?)

.code
main PROC
    ; Read Integers from User
    mov ebx, 0
    mov ecx, COUNT
    mov edx, OFFSET inputMsg
    mov esi, OFFSET strArray

L1: call WriteString            ; Display Prompt
    call ReadInt                ; Read input from user
    mov [esi], eax              ; Store value into array
    add esi, TYPE strArray      ; Move to next array position
    loop L1

    call Crlf

    ; Redisplay the integers
    mov edx, OFFSET outputMsg   ; Display 'outputMsg'
    call WriteString
    mov ecx, COUNT
    mov esi, OFFSET strArray

L2: mov ebx, 0                  ; Initialize ebx to 0
    mov eax, [esi]              ; Get integer from array
    call WriteInt               ; Display integer
    mov edx, OFFSET sumMsg      ; Display value of 'sumMsg'
    call WriteString
    ; mov eax, ebx
    add ebx, [esi]

    mov eax, ebx   ; <---- MOVED from above add ebx, [esi]

    call WriteInt
    call Crlf                   
    add esi, TYPE strArray      ; Move to next array position
    loop L2

    exit
main ENDP

END main
Était-ce utile?

La solution

You need to move ebx to eax before you call WriteInt. Initializing ebx to 0 before the summation loop would be a good idea too.

Autres conseils

Probably you have to "mov eax, ebx" before calling the last WriteInt.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top