flat assembler
Message board for the users of flat assembler.

 Index > DOS > Summation Program
Author
rhyno_dagreat

Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat 21 Oct 2006, 23:23
I have created a small summation program which will take n amount of numbers and do the summation 1+2...+n. It is only single digit for now (mainly because I don't know how to make a multidigit decimal number from ascii, and vice versa.) Anyways, here it is! Enjoy !

21 Oct 2006, 23:23
Sebastian R.

Joined: 26 Oct 2006
Posts: 4
Sebastian R. 26 Oct 2006, 10:23
Somehow it only works for 1 - 3. If I enter 4, 5 ... it displays some characters.
26 Oct 2006, 10:23
rhyno_dagreat

Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat 27 Oct 2006, 02:32
Sebastian, it gives you a domain for what the input can be in the program. Read the first thing that pops up when you run it.
27 Oct 2006, 02:32
Sebastian R.

Joined: 26 Oct 2006
Posts: 4
Sebastian R. 27 Oct 2006, 10:01
Yes, sorry, now I can see it. ^^
27 Oct 2006, 10:01

Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 27 Oct 2006, 10:45
A few things you could do:
the sum of numbers 1 to N is ((1+N) * N) shr 1
The 1,2 and 3 are too little of a set to make a point. You could at least hardcode it to accept 2-digit numbers. The output could also be multidigit.

That is why its problematic (or even erratic) when tested now.

EDIT: My try on the program

EDIT2: Yeah I new the formula was wrong, but the program was correct. I didn't just correct it yet. Now also the formula given above is corrected.

 Description: The summator that works on 32-bit numbers so the max input is increased to 65535 Download Filename: Summation.7z Filesize: 845 Bytes Downloaded: 190 Time(s)

_________________
My updated idol http://www.agner.org/optimize/

Last edited by Madis731 on 28 Oct 2006, 09:35; edited 1 time in total
27 Oct 2006, 10:45
Goplat

Joined: 15 Sep 2006
Posts: 181
Goplat 27 Oct 2006, 17:28
Madis: That will be wrong when N is even. It's supposed to be ((1+N)*N) shr 1.
27 Oct 2006, 17:28
rhyno_dagreat

Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat 28 Oct 2006, 03:39
Ah... Well that's where the problem lies as in I don't know how to change multidigit numbers into ascii and vice versa.
28 Oct 2006, 03:39
Goplat

Joined: 15 Sep 2006
Posts: 181
Goplat 28 Oct 2006, 07:03
It's not that hard. To convert from a string into a number, for each digit you just multiply the current number by 10 and add the new digit. Like this:
Code:
```        xor cx,cx
in1:    mov ah,1
int 21h
sub al,'0'
cmp al,10
jae in2
imul cx,10
cbw
jmp in1
in2:
```

Converting a number to a string is a little more complicated, the usual way is to repeatedly divide by 10 and the remainders give you the digits, but in reverse order. One way to get around this is to save the digits on the stack, since then when you read them back from the stack you get the last one first and it becomes the correct order.
Code:
```        mov cx,10
mov bp,sp
out1:   xor dx,dx
div cx
push dx
test ax,ax
jnz out1
mov ah,2
out2:   pop dx
int 21h
cmp sp,bp
jne out2
```
28 Oct 2006, 07:03
rhyno_dagreat

Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat 28 Oct 2006, 19:39
Thanks! I'll try that.
28 Oct 2006, 19:39
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum