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

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?

• Here is the program that calculates the factorial,
it requires 'emu8086.inc' header file, you can get it
from emu8086 pack:

The following code should be compatible with MASM/TASM as well
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

; 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...

• I have the following problem:

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

all around the world:

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