Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Need help in mathematical algorithm (S.O.S!!!)

webboywebboy Member Posts: 4
I'm an absolute beginner in Assembly programming.
First I have to write n! (n factorial) in Assembly (probably on a recursive way), then I have to write the summary of the first n positive integers: n*(n+1)/2.

Please, tell me how to write mathematical programs in Assembly.

How should I begin to write such programs, which methods are to be needed?



Comments

  • emu8086emu8086 Member Posts: 125
    Here is the program that calculates the factorial,
    it requires 'emu8086.inc' header file, you can get it
    from emu8086 pack:
    http://www.programmersheaven.com/search/download.asp?FileID=20562

    The following code should be compatible with MASM/TASM as well
    (some tiny modifications maybe required), see this article
    for more info:
    http://www.geocities.com/emu8086/Help/compatibility.html

    [code]
    ; This sample gets the number
    ; from the user, and calculates
    ; factorial for it.
    ; Supported input from 0 to 8
    ; inclusive!

    #make_COM#

    include 'emu8086.inc'

    ORG 100h

    start:

    ; get first number:

    CALL PTHIS
    DB 13, 10, 'Enter the number: ', 0

    CALL scan_num


    ; factorial of 0 = 1:
    MOV AX, 1
    CMP CX, 0
    JE print_result

    ; move the number to BX:
    ; CX will be a counter:

    MOV BX, CX

    MOV AX, 1
    MOV BX, 1

    calc:
    MUL BX
    CMP DX, 0
    JNE overflow
    INC BX
    LOOP calc

    print_result:

    ; print result in AX:
    CALL PTHIS
    DB 13, 10, 'Factorial: ', 0

    CALL PRINT_NUM_UNS
    JMP exit


    overflow:
    CALL PTHIS
    DB 13, 10, 'The result is too big!', 13, 10, 'Use values from 0 to 8.', 0
    JMP start

    exit:

    RET

    ;=================================
    ; here we define the functions
    ; from emu8086.inc

    ; SCAN_NUM reads a
    ; number from the user and stores
    ; it in CX register.

    DEFINE_SCAN_NUM

    ; PRINT_NUM prints a signed
    ; number in AX.
    ; PRINT_NUM_UNS prints an unsigned
    ; number in AX (required by
    ; PRINT_NUM):

    DEFINE_PRINT_NUM
    DEFINE_PRINT_NUM_UNS

    ; PTHIS prints NULL terminated
    ; string defined just after
    ; the CALL PTHIS instruction:
    DEFINE_PTHIS

    ;=================================
    END
    [/code]

    Hope this helps.

    Let me know if any trouble...


  • webboywebboy Member Posts: 4
    I have the following problem:

    EMU8086 can't be downloaded ordinarly. At the end of the download, the setup can't run.

    I need EMU8086 because of emu8086.inc, please tell me which site I can download it from.

    Or if you have the version, which is functioning, you can send it to me by E-Mail.

    My E-mail address is: [email protected]
  • emu8086emu8086 Member Posts: 125
    you can try downloading emu8086 from Simtel Servers
    all around the world:


    http://www.simtel.net/pub/pd/58244.shtml

  • webboywebboy Member Posts: 4
    Thanks for all the informations. I have downloaded it successfully.

  • Shawn CarterShawn Carter Member Posts: 0

    ____ // http://forcoder.org // free video tutorials and ebooks about // Scratch, Swift, Ruby, Python, MATLAB, Objective-C, PL/SQL, Go, JavaScript, Java, PHP, Perl, Visual Basic .NET, C++, C#, Visual Basic, R, Delphi, C, Assembly Fortran, COBOL, Rust, Hack, D, Apex, Scala, LabVIEW, Awk, Lua, Transact-SQL, ABAP, Lisp, Julia, Dart, Ada, Scheme, Crystal, FoxPro, Alice, Kotlin, Bash, VBScript, ML, Logo, F#, Erlang, SAS, Prolog, Clojure // _________

Sign In or Register to comment.