The MASM Forum

General => The Campus => Topic started by: imadhx30 on February 01, 2014, 11:19:40 AM

Title: add+mul
Post by: imadhx30 on February 01, 2014, 11:19:40 AM
how does the program masm32!!
a^0*b^n+...........................+a^n*b^0
Title: Re: add+mul
Post by: Farabi on February 01, 2014, 12:17:50 PM
You'll have to know the order of the multiplication and the addition and doing it one by one. There is a function on the FPU for the exponential and addition to solve your problem. But I forget how to use the exponent function using FPU.
Title: Re: add+mul
Post by: imadhx30 on February 01, 2014, 12:25:22 PM
my program not calculate the formula gives!!
just calculated the power a^n


.386
.model flat,stdcall
option casemap:none
include\masm32\include\masm32.inc
include\masm32\include\kernel32.inc
include\masm32\include\msvcrt.inc
includelib\masm32\lib\masm32.lib
includelib\masm32\lib\kernel32.lib
includelib\masm32\lib\msvcrt.lib
;---------------------------------------------------------
somiter proto :DWORD,:DWORD
.data
ms1 db "donner a : ",0
ms4 db "donner b : ",0
ms3 db "donner  n : ",0
ms2 db "la somme final est : %d ",0
format db "%d",0
.data?
a dd ?
b dd ?
n dd ?
tab dd 25 dup(?)
.code
start:
push offset ms1
call crt_printf
push offset a
push offset format
call crt_scanf
push offset ms4
call crt_printf
push offset b
push offset format
call crt_scanf
push offset ms3
call crt_printf
push offset n
push offset format
call crt_scanf
invoke somiter,a,n
push eax
push offset ms2
call crt_printf
ici:jmp ici
invoke ExitProcess,0
;---------------------------------------------
somiter proc x:DWORD,z:DWORD
mov eax,x
mov edx,z
    cmp edx,0
    je els
repete:
    dec z
    jz fin
    mov ebx,x
    mul ebx
jmp repete   
    els:
    mov eax,1
    fin:
    ret
    somiter endp
    end start


Title: Re: add+mul
Post by: hutch-- on February 01, 2014, 12:32:57 PM
Something you need to do with your called procedure, preserve EBX. It must be the same on exit as it was on entry or you can get problems with both API functions and MSVCRT functions.


somiter proc x:DWORD,z:DWORD

    push ebx  ; preserve here

mov eax,x
mov edx,z
    cmp edx,0
    je els
repete:
    dec z
    jz fin
    mov ebx,x
    mul ebx
jmp repete   
    els:
    mov eax,1
    fin:

    pop ebx    ; restore here

    ret
    somiter endp

    end start


In this case you could have used ECX instead of EBX but its worth knowing about preserving and restoring registers.
Title: Re: add+mul
Post by: dedndave on February 01, 2014, 12:34:42 PM
i guess a and n are integers ?
if so, i have a ling long kai fang routine for that

http://masm32.com/board/index.php?topic=222.0 (http://masm32.com/board/index.php?topic=222.0)
Title: Re: add+mul
Post by: imadhx30 on February 01, 2014, 12:42:46 PM
my program not calculate the formula a^0*b^n+a^1*b^n-1+...........................+a^n*b^0
help,:me
Title: Re: add+mul
Post by: qWord on February 01, 2014, 03:24:49 PM
Quote from: imadhx30 on February 01, 2014, 12:42:46 PM
my program not calculate the formula a^0*b^n+a^1*b^n-1+...........................+a^n*b^0
a0bn+a1bn-1+...+anb0 == bn+a(bn-1+a(bn-2+a(...+a(b+a))))
include \masm32\include\masm32rt.inc
.code
main proc
LOCAL a:REAL8,b:REAL8,n:DWORD,x:REAL8

    finit
    print "double a = "
    fncx crt_scanf,"%lG",&a
    print "double b = "
    fncx crt_scanf,"%lG",&b
    print "unsigned int n = "
    fncx crt_scanf,"%u",&n
   
    fld1
    fld1
    mov ecx,n
    jmp @w
@@:
    fmul a
    fld b
    fmulp st(2),st
    fadd st,st(1)
@w: add ecx,-1
    jge @B
    fstp st(1)
    fstp x
    fncx crt_printf,"%.6G\n",x
   
    inkey
    exit   
main endp
end main
Title: Re: add+mul
Post by: Gunther on February 01, 2014, 08:41:38 PM
Hi qWord,

Quote from: qWord on February 01, 2014, 03:24:49 PM
a0bn+a1bn-1+...+anb0 == bn+a(bn-1+a(bn-2+a(...+a(b+a))))

well done.  :t It's the well known Horner's method.

Gunther
Title: Re: add+mul
Post by: imadhx30 on February 02, 2014, 01:24:18 AM

qWord  :t

give another example except following library:
include \ masm32 \ include \ kernel32.inc
include \ masm32 \ include \ masm32.inc
include \ masm32 \ include \ msvcrt.inc
includelib \ masm32 \ lib \ masm32.lib
includelib \ masm32 \ lib \ kernel32.lib
includelib \ masm32 \ lib \ msvcrt.lib
Title: Re: add+mul
Post by: dedndave on February 02, 2014, 02:30:51 AM
i think there's a way to solve the geometric series without iteration
i just don't remember what it is   :lol:

imad...
INC files are plain text files - you can examine them with NotePad
have a look at the file \masm32\include\masm32rt.inc   :t
Title: Re: add+mul
Post by: imadhx30 on February 02, 2014, 03:08:11 AM
but not included /masm32rt.inc   :icon_exclaim:
plz corrigé my programme
   :(
.386
.model flat,stdcall
option casemap:none
include\masm32\include\masm32.inc
include\masm32\include\kernel32.inc
include\masm32\include\msvcrt.inc
includelib\masm32\lib\masm32.lib
includelib\masm32\lib\kernel32.lib
includelib\masm32\lib\msvcrt.lib
;---------------------------------------------------------
somiter proto :DWORD,:DWORD
.data
ms1 db "donner a : ",0
ms4 db "donner b : ",0
ms3 db "donner  n : ",0
ms2 db "la somme est : %d ",0
format db "%d",0
.data?
a dd ?
b dd ?
n dd ?
tab dd 25 dup(?)
.code
start:
push offset ms1
call crt_printf
push offset a
push offset format
call crt_scanf
push offset ms4
call crt_printf
push offset b
push offset format
call crt_scanf
push offset ms3
call crt_printf
push offset n
push offset format
call crt_scanf
invoke somiter,a,n
push eax
push offset ms2
call crt_printf
ici:jmp ici
invoke ExitProcess,0
;---------------------------------------------
somiter proc x:DWORD,z:DWORD
mov eax,x
mov edx,z
    cmp edx,0
    je els
repete:
    dec z
    jz fin
    mov ebx,x
    mul ebx
jmp repete   
    els:
    mov eax,1
    fin:
    ret
    somiter endp
    end start


Title: Re: add+mul
Post by: dedndave on February 02, 2014, 03:30:48 AM
start with something like this
.386 processor does not allow certain addressing modes - should be .486 minimum
also, windows.inc should be the first one included
and the masm32 library depends on msvcrt (probably a few others, too - like user32)
        .586
        .model  flat,stdcall
        option  casemap:none
        include     \masm32\include\windows.inc
        include     \masm32\include\kernel32.inc
        include     \masm32\include\msvcrt.inc
        include     \masm32\include\masm32.inc
        includelib  \masm32\lib\kernel32.lib
        includelib  \masm32\lib\msvcrt.lib
        includelib  \masm32\lib\masm32.lib
Title: Re: add+mul
Post by: imadhx30 on February 02, 2014, 04:00:47 AM
give a solution used except (a^0*b^n+...........................+a^n*b^0)
use except:
include\masm32\include\masm32.inc
include\masm32\include\kernel32.inc
include\masm32\include\msvcrt.inc
includelib\masm32\lib\masm32.lib
includelib\masm32\lib\kernel32.lib
includelib\masm32\lib\msvcrt.lib
:icon_redface: :icon_redface: :icon_redface: :icon_redface:
Title: Re: add+mul
Post by: qWord on February 02, 2014, 04:51:28 AM
learn programming or drop out of college.
Title: Re: add+mul
Post by: TWell on February 02, 2014, 10:38:06 AM
Quote from: qWord on February 02, 2014, 04:51:28 AM
learn programming or drop out of college.
If you don't want to use masm32rt.inc think about another forum.
Title: Re: add+mul
Post by: raymond on February 02, 2014, 12:40:51 PM
imadhx30

Before you ever start programming anything mathematical, you must first use your brain to determine if there is an easier way to perform the calculation. Something which may take hours to get the result on a computer may happen to take a fraction of a second when the process can be simplified.

For example, your equation can be expressed as a much more simple one which you learn in early high school algebra:
a0*bn+...........................+an*b0 is equivalent to:
(bn+1 - an+1)/(b-a)

Even with the simplification offered by qWord, you could be computing for a long time if the value of n is very high. With the simplified equation, you only need to compute two powers, two subtractions and one division.

Depending on which limits you accept for your a, b and n values (which limits you must check even before starting your computation),  you then provide code to avoid overflows either with integer instructions or floating point instructions. If the expected answer would exceed 64-bit integers with older computers (or 128 bits on modern 64-bit computers), your code would then have to rely on integer instructions using a bignum library if you need the answer exact to the least significant digit.

Have fun. Nobody here will provide you with the required code. You will now have to do your homework yourself.
Title: Re: add+mul
Post by: dedndave on February 02, 2014, 02:27:33 PM
thanks Ray - i knew we were doing the hard way   :biggrin:
Title: Re: add+mul
Post by: Gunther on February 02, 2014, 10:03:55 PM
Dave,

Quote from: dedndave on February 02, 2014, 02:30:51 AM
i think there's a way to solve the geometric series without iteration
i just don't remember what it is   :lol:

that's not necessarily a geometric series.

Gunther
Title: Re: add+mul
Post by: imadhx30 on February 02, 2014, 10:58:24 PM
thanks Ray (y)
:t :t :t :t :t
Title: Re: add+mul
Post by: dedndave on February 03, 2014, 09:11:57 AM
if a = b
a0 bn + a1 bn-1 +....+ an b0 = (n+1) an

if a <> b
a0 bn + a1 bn-1 +....+ an b0 = (bn+1 - an+1) / (b - a)
Title: Re: add+mul
Post by: K_F on February 04, 2014, 06:59:55 AM
Quote from: raymond on February 02, 2014, 12:40:51 PM
imadhx30

Before you ever start programming anything mathematical, you must first use your brain to determine if there is an easier way to perform the calculation. Something which may take hours to get the result on a computer may happen to take a fraction of a second when the process can be simplified.
Talking about this.. you might find this PDF usefull - I consult it many times - cannot remember where I found it - had it for many moons :)

https://www.dropbox.com/s/9fkq6lg5xqcce7o/ProgrammersAlgorithms.pdf (https://www.dropbox.com/s/9fkq6lg5xqcce7o/ProgrammersAlgorithms.pdf)
Title: Re: add+mul
Post by: dedndave on February 04, 2014, 07:12:39 AM
very nice Van   :t
Title: Re: add+mul
Post by: TWell on February 04, 2014, 07:58:54 PM
Found latest from here (http://www.jjj.de/fxt/fxtbook.pdf) ?
Title: Re: add+mul
Post by: dedndave on February 04, 2014, 09:38:42 PM
 :t
Title: Re: add+mul
Post by: Gunther on February 04, 2014, 11:11:45 PM
It's a crazy but hard working guy.

Gunther
Title: Re: add+mul
Post by: Gunther on February 05, 2014, 12:38:57 AM
I can also recommend the following book (http://www.jjj.de/pibook/pibook.html) by Jörg Arndt.

Gunther