I am having some problems with my "sum of numbers program," and cannot figure out why I am not getting the correct answers. This is my first Assembly programming class, so take it easy on me, please. The assignment is: Prompt a user for an integer n, compute the sum of the integers from 1 to n, and display the sum. ex: user inputs "5". 1+2+3+4+5=15. Output "The sum is: 15"

Here is what I have so far:

.586
.MODEL FLAT

INCLUDE io.h ; header file for input/output

.STACK 4096

.DATA
sum DWORD 0
prompt BYTE "Enter a number: ", 0
num1 DWORD ?
num2 DWORD 0
string BYTE 40 DUP (?)
resultLbl BYTE "The sum is: ", 0

.CODE
_MainProc PROC

input prompt, string, 40 ; read ASCII characters
atod string
mov num1, eax ; store in memory
mov num2, 2
mov ebx, num2
cmp eax, sum
jne GetValue
je Done

GetValue:

inc sum
add sum, ebx
mov eax, ebx
cmp eax, sum
jne GetValue ; loop
je Done

Done:

dtoa sum, eax
output resultLbl, sum
ret

_MainProc ENDP
END

After you entered string with number and converted it to number, for example it is in eax:

mov     ecx,eax
        mov     ebx,0
        cmp     ecx,0
        jle     mSkip
mMain:
        add     ebx,ecx
        loop    mMain
mSkip:

, result will be in ebx.

After you entered string with number and converted it to number, for example it is in eax:

mov     ecx,eax
        mov     ebx,0
        cmp     ecx,0
        jle     mSkip
mMain:
        add     ebx,ecx
        loop    mMain
mSkip:

, result will be in ebx.

aah, makes sense now. Thank you, my code runs perfectly!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.