Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

x86 Assembly: Program to sum input values and then average them is crashing after trying to access a memory location that is not accessible

I have a school assignment that has got me stumped. The program is to read in values, pass them into an array, sum the values, and average them.

.DATA
inputArray  DWORD 100 DUP(?)
elementCnt  DWORD 0
number      DWORD ?
prompt      BYTE  "Enter values. (-1 to quit) ", 0
string      BYTE  40 DUP (?)
resultLbl   BYTE  "The sum is", 0
sum         BYTE  11 DUP (?), 0

.CODE
_MainProc PROC
    mov  eax, 0                    ; EAX = 0
    lea  ebx, inputArray           ; EBX = [inputArray]
    mov  ecx, elementCnt           ; ECX = 0

getInputs:
    input prompt, string, 40      ; Get input from the user
    atod  string                  ; Convert to decimal, result stored in EAX
    mov   [ebx + (ecx * 4)], eax        ; Move the user input into the array
    add   ecx, 1
    cmp   eax, -1
    jg    getInputs

    mov  eax, 0

    mov  eax, 0                    
    lea  ebx, inputArray           
    mov  ecx, elementCnt          

    push ebx
    push eax
    call sumAndAverage

quit:
    mov  eax, 0
    ret
_MainProc ENDP

sumAndAverage PROC
    push ebp
    mov  ebp, esp
    push eax

sumValues:
    add  eax, [ebx + ecx] ; <------ CRASHING HERE 
    loop sumValues

    xor  edx, edx
    idiv ecx

    pop  eax
    pop  ebp
    ret
sumAndAverage ENDP
END

I am not the greatest at assembly. It is definitely not my language.

input and prompt are both macros defined in a header named io.h. The section of code that uses them works fine. To be honest, I have an inkling of an idea what might be causing the crash, but I am not sure. The code is crashing because it is trying to access a memory location it’s not allowed to access. This means that something is wrong with my code to read the values from the array.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If someone could explain to me what my problem is without providing a solution, I’d appreciate it.

>Solution :

Taking a look at this section of code:

sumValues:
    add  eax, [ebx + ecx] ; <------ CRASHING HERE 
    loop sumValues

loop decrements ecx. Since ecx is zero when you enter sumAndAverage (you should be able to figure out why this is), ecx becomes negative as you iterate through the loop. This causes the effective address of [ebx + ecx] to go out of bounds. This qualifies as a segmentation fault.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading