1,105,578 Community Members

ASM - Sum of numbers

Member Avatar
txhornsfan
Newbie Poster
9 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
skaa
Light Poster
38 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
txhornsfan
Newbie Poster
9 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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!

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: