Hello again everyone

I have some code that Calculates the value of E.


Int he following code, the value of E is stored inside a variable full_sum
How do I print this value?



.model small

.DATA
full_sum dt 2.0 ; First two series members: 1/0! + 1/1! = 2

.CODE

fp_factorial Proc Near
; ----------------------------------------------------------------------------
; INPUT:
; AX = factor (i.e. 5! means AX=5)
; OUTPUT:
; ST(0) - result: 1/AX! - the partial sum for the series
; ----------------------------------------------------------------------------
mov cx, ax
dec cx

finit
fld1
fld1
fld1

; After this we have:
; ST(0) - result = 1
; ST(1) - counter = 1
; ST(2) - increment = 1

@@_next_factorial:
fxch st(1) ; swap result with counter
fadd st, st(2) ; increment counter
fxch st(1) ; swap result with counter
fmul st, st(1) ; result = result * counter
loop @@_next_factorial

fxch st(2) ; swap result with 1.0
fdiv st, st(2) ; 1/n! - we got the partial sum for E

ret
fp_factorial EndP

CalculateE Proc Near
; ----------------------------------------------------------------------------
mov ax, 2 ; Starting from 1/2! since we have first two in 'full_sum'
mov cx, 16 ; 16 more partial sums will give enough quality to calculated E

@@_next_sum:
push cx ; CX is used by 'fp_factorial', so preserve it
call fp_factorial
fld full_sum ; ST(0) <- 'full_sum'
fadd st, st(1) ; Add the calculated sum
fstp full_sum ; Save the sum back
inc ax ; Next scale for factorial
pop cx ; Restore CX
loop @@_next_sum

; --- At this point variable 'full_sum' contains E.

ret
CalculateE EndP

Main:
; Put here your code for initializing DS register...

call CalculateE


; Here you, probably want to output it to console...
; I did not find any code that will do that...

mov ax, 4C00h
int 21h
End Main
Posted on 2004-05-04 10:53:00 by ionicbond


mov ah,9
mov dx,offset buffer
int 21h

But you still need a routine to convert a number to a string.
Also, the string must end with a "$", NOT a NULL like in C.


; sample string
buffer db "This is a sample$"
Posted on 2004-05-04 12:19:34 by QvasiModo
Man, I do not understand what you are saying.

I dont want to move any string.

Let me explain.

I have a value stored in the variable "full_sum"
I just wanna display it!!!! It doesnt have to be console, even if I can display it in the memmory using a debug program or something.
Posted on 2004-05-04 12:37:59 by ionicbond
I did this:

mov ah,9
mov dx,offset full_sum
int 21h

BTW, I did what you said... and ran a debug program.

The vlue stored in DX was "0000"
WHY?
Shouldnt it contain the address of full_sum?
Posted on 2004-05-04 12:41:45 by ionicbond
i think you have to do


mov ds,@data

first because the string has to be at DS:DX but DS isn't initialised.

but even so, full_sum is not a string so you'll get garbage. maybe the instruction fbstp can help you convert it?
Posted on 2004-05-04 12:50:07 by stormix
you're dealing with 16-bit code. full_sum is the first variable in the data segment, and the data segment is placed before the code segment, so it's doesn't seem wrong that the offset is 0.
Posted on 2004-05-04 12:51:28 by f0dder
I TRIED MOV DS,@DATA
But it told me "invalid instruction operand".

Instead of giving me speculative code in your replies folks, why dont you just try it out for yourself and let me know what to add.

PLEASE PLEASE MY PROJECT IS DUE IN LESS THAN 24 HOURS PLEEEEEEEEEAAAAASEEEEEEE!!!!!
Posted on 2004-05-04 15:39:44 by ionicbond
Look at the attachs....
mov ax,Some number
call PrtDec

PrtDec prints "Some number" to console
"Some number" must be integer...
Posted on 2004-05-04 16:01:03 by Criminal2

I TRIED MOV DS,@DATA
But it told me "invalid instruction operand".

Instead of giving me speculative code in your replies folks, why dont you just try it out for yourself and let me know what to add.

PLEASE PLEASE MY PROJECT IS DUE IN LESS THAN 24 HOURS PLEEEEEEEEEAAAAASEEEEEEE!!!!!


Dont bother the members with your homework problems
Posted on 2004-05-04 16:09:15 by comrade
If you can't put any effort into it yourself, then please go somewhere else. I don't mind helping people with homework, but I sure as hell don't want to do it for them.
Posted on 2004-05-04 16:34:43 by f0dder
Nuff said. We will see how far this thread continues, or else it will be a closed topic.

ionicbond: I dont care if someone posts solutions to do your homework, but definitely do not demand or even imply members ought to do it for you. If you left it to the last minute don't bother us. That said, your free to hang around and learn assembly from the discussions on this board. But keep it to discussions, not homework assignments.

This is a last warning. You're previous thread was closed for the same reason. Stop fishing for homework assignments

Regards,
:NaN:
Posted on 2004-05-04 16:53:43 by NaN