flat assembler
Message board for the users of flat assembler.

Index > DOS > Summation Program

Author
Thread Post new topic Reply to topic
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat
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 Very Happy !


Description:
Download
Filename: Summation.zip
Filesize: 1.01 KB
Downloaded: 63 Time(s)

Post 21 Oct 2006, 23:23
View user's profile Send private message Reply with quote
Sebastian R.



Joined: 26 Oct 2006
Posts: 4
Sebastian R.
Somehow it only works for 1 - 3. If I enter 4, 5 ... it displays some characters.
Post 26 Oct 2006, 10:23
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat
Sebastian, it gives you a domain for what the input can be in the program. Razz Read the first thing that pops up when you run it.
Post 27 Oct 2006, 02:32
View user's profile Send private message Reply with quote
Sebastian R.



Joined: 26 Oct 2006
Posts: 4
Sebastian R.
Yes, sorry, now I can see it. ^^
Post 27 Oct 2006, 10:01
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2141
Location: Estonia
Madis731
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: 52 Time(s)


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


Last edited by Madis731 on 28 Oct 2006, 09:35; edited 1 time in total
Post 27 Oct 2006, 10:45
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat
Madis: That will be wrong when N is even. It's supposed to be ((1+N)*N) shr 1.
Post 27 Oct 2006, 17:28
View user's profile Send private message Reply with quote
rhyno_dagreat



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



Joined: 15 Sep 2006
Posts: 181
Goplat
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
        add cx,ax
        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
        add dl,'0'
        int 21h
        cmp sp,bp
        jne out2
    
Post 28 Oct 2006, 07:03
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat
Thanks! I'll try that.
Post 28 Oct 2006, 19:39
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.