assembly_language_binomial

5
01 ;nasm -f bin Binomial.asm -o Binomial.com 02 ;Result in D X 03 org 100h 04 start: 05 jmp begin 06 data: 07 begin: 08 mov ax,4 09 mov bx,2 10 call BinomialC 11 12 mov ah,4ch 13 int 21h 14 ;======================================================================= 15 BinomialC: 16 ;Binomial Coefficient 17 ;Parameters: 18 ; AX - n 19 ; BX - k 20 ;Result: 21 ; DX - Coefficient 22 mov dx,1 23 24 cmp bx,0 ;if(k==0 ) 25 je mex 26 27 cmp ax,bx ;if(n==k ) 28 je mex 29 30 push ax 31 32 dec ax 33 call BinomialC ;binomial(n-1,k) 34 push dx 35 36 push bx 37 dec bx 38 call BinomialC ;binomial(n-1,k-1) 39 40 pop bx 41

Upload: clau

Post on 16-Dec-2015

214 views

Category:

Documents


0 download

DESCRIPTION

assembly_language_binomial

TRANSCRIPT

01;nasm -f bin Binomial.asm -o Binomial.com

02;ResultinDX

03org 100h

04start:

05jmp begin

06data:

07begin:

08mov ax,4

09mov bx,2

10call BinomialC

11

12mov ah,4ch

13int 21h

14;=======================================================================

15BinomialC:

16;Binomial Coefficient

17;Parameters:

18; AX - n

19; BX - k

20;Result:

21; DX - Coefficient

22mov dx,1

23

24cmpbx,0 ;if(k==0)

25je mex

26

27cmpax,bx ;if(n==k)

28je mex

29

30push ax

31

32dec ax

33call BinomialC ;binomial(n-1,k)

34push dx

35

36push bx

37dec bx

38call BinomialC ;binomial(n-1,k-1)

39

40pop bx

41

42mov ax,dx

43pop dx

44add dx,ax

45

46pop ax

47

48mex:

49ret

50;=======================================================================

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118; Description: This program will output the binomial coefficients based upon the users input.; Author: Robert Phelan; Last Updated: 2/12/13INCLUDE Irvine32.incBinomialFunc proto.dataendl EQU inputPormpt BYTE "Please enter an integer:",0spaceString BYTE " ", 0byeString BYTE "Bye",endl, 0inputValue DWORD ?.code; main procmain PROCcall Clrscr ; CLears Screenmain_start:mov edx, offset inputPormptcall WriteStringcall ReadDecmov ecx, eaxadd ecx, 1mov inputValue, eaxcmp eax , 0jz main_exitmain_loop:dec ecxpush ecxpush inputValuecall BinomialFunccall WriteDecmov edx, offset spaceStringcall WriteStringcmp ecx, 0jg main_loopcall Crlfjmp main_startmain_exit:mov edx, offset byeStringcall WriteStringexit main endpEND main; Description: This is the recursive algorithm that will return the coefficients to the main program for displaying.; Author: Robert Phelan; Last Updated: 2/12/13.486 ; create 32 bit code.model flat, stdcall ; 32 bit memory modeloption casemap :none ; case sensitive.codeBinomialFunc procpush ecxpush ebxpush edx mov eax,0mov ecx, [esp + 20]mov edx, [esp + 16]cmp ecx,0jz ret1cmp edx,0jz ret1cmp ecx ,edxjz ret1cmp ecx,0jl ret0cmp edx,0jl ret0cmp ecx,edxjg ret0dec edxpush ecxpush edxcall BinomialFuncadd esp,8dec ecxmov ebx, eaxpush ecxpush edxcall BinomialFuncadd esp,8add eax, ebxpop edxpop ebxpop ecxretnret0:mov eax,0pop edxpop ebxpop ecxretnret1:mov eax,1pop edxpop ebxpop ecxretnBinomialFunc endpend