Morgan - Numerical Methods (523161), страница 45
Текст из файла (страница 45)
When the float;enters here, it is in extended formatftfxproc uses bx cx dx si di, fp:qword, sptr:wordlocalsinptr:byte, exponent:bytepushfstdxormovmovmovax,axbyte ptr [sinptr],albyte ptr [exponent],aldi,word ptr sptr;point to resultinvokeround, fp, addr fp;fixup for translationax,word ptr fp[0]bx,word ptr fp[2]dx,word ptr fp[4]dx,dxget_exponentbyte ptr [sinptr];it is negative;clear the sign;; ***;do_rnd:;set_sign:movmovmovorjnsnotget_exponent:subshlsubmovmovandstcrcrcx,cxdx,ldh,86hbyte ptr exponent, dhcl,dhdx,0ffh;save number portiondl,1;restore hidden bit;remove bias from exponent389NUMERICAL METHODS;which_way:orjnsnegshift_right:cmpjamake_fraction:shrrcrrcrloopmovmOVmovjmpshift_left:cmpjabigmake_integer:shlrclrclloopmovmovmovprint_result:testjenotnotnotnegjc390cl,clshift_leftclcl,28hmake_zero;no significance, too smalldx,1bx,1ax,1make_fractionword ptr [di][0],axword ptr [di][2],bxword ptr [di][4],dxshort print_resultcl,18hmake_max;failed significance, toobx,1dx,1ax,1make_integerword ptr [di][6],axword ptr [di][4],dxword ptr [di][2],bxbyteexitwordwordwordwordexitptr [sinptr], 0ffhptrptrptrptr[di][6][di][4][di][2][di][0];two's complementIO.ASMaddadcadcword ptr [di][2],1word ptr [di][4],0word ptr [di][6],0exit:popfretmake_zero:submovstoswrepjmpmake_max:submovstoswrepnotstoswandnotstoswjmpax,axcx,4short exitax,axcx,2axword ptr [di][4], 7f80haxshort exitftfx endp;;*******************************************************; dnt_bn - decimal integer to binary conversion routine;unsigned;It is expected that decptr points at a string of ASCII decimal digits.;Each digit is taken in turn and converted until eight have been converted;or until a nondecimal number is encountered.;This might be used to pull a number from a communications buffer.;Returns with no carry if successful and carry set if not.dnt_bn procuses bx cx dx si di, decptr:word, binary:word391NUMERICAL METHODSmovsi,word ptr decptrsubmovmovmovax,axbx,axdx,bxcx, 9decimal_conversion:movcmpjbcmpjacallxoraddadcincloopal,byte ptr [si]al,'O'work_doneal,'9'work_donenear ptr times-tenal,'O'bx,axdx,Osidecimal_conversion;get pointer to the MSB of the;decimal;value;check for decimal digit;if it gets past here, it must;be OK;convert to number;propagate any carriesoops:;more than eight digits or;somethingstcretwork-done:movmovmovclcrettimes_ten:pushpushshlrclmov392di, word ptr binaryword ptr [di],bxword ptr [di][2],dxaxcxbx,ldx,lax,bx;store resultIO.ASMmovcx,dxshlrclbx,ldx,lshlrclbx,ldx,laddadcbx,axdx,cxpoppopretndnt_bn endp;multiply by tencxax;*******************************************************;bn-dnt - a conversion routine that converts binary data to decimal;A double word is converted.
Up to eight decimal digits are;placed in the array pointed at by decptr. If more are required to adequately;convert this number, the attempt is aborted and an error flagged.bn_dnt procuses bx cx dx si di, binary:dword, decptr:wordleasi,word ptr binarymovdi,word ptr decptrmovaddcx, 9di,cxsubmovmovbx,bxdx,bxbyte ptr [di],bldecdi;get pointer to the MSBb of;the decimal;value;string of decimal ASCII;digits;point to the end of the;string;this is for correctordering;see that string is;zero-terminated393NUMERICAL METHODSbinary_conversion:submovorjedivmovorjedivide_lower:movorjneorjenot_zero:divput_zero:movormovdecloopdx,dxax,word ptr [si][2]ax,axchk_emptyitenword ptr [si][2],axdx,dxchk_emptyax, word ptr [si]ax,axnot_zerodx, axput_zeroitenword ptr [si],axdl,'O'byte ptr [di],dldibinary_conversionoops:movstcretchk_empty:orjejmpstill_nothing:movorjejmp394ax,-1dx,dxstill_nothingshort divide_lowerax,word ptr [si]ax,axemptyshort not_zero;divide by tenIO.ASMempty:repincmovmovmovmovswfinished:subclcretbn_dnt endpdisi,didi, word ptr decptrcx,9ax,ax;*******************************************************;bfc_dc -A conversion routine that converts a binary fraction (doubleword);To decimal ASCII representation pointed to by the string pointer, decptr.;Set for eight digits; it could be longer.bfc_dc procuses bx cx dx si di bp, fraction:dword, decptr:wordlocalsva:word, svb:word, svd:wordmovdi,word ptr decptrmovmovbx,word ptr fractiondx,word ptr fraction[2]movsubcx,8ax,ax;digit countermovbyte ptr [di],'.';to begin the ASCII;fractionincdidecimal_conversion:orax,dxorax,bxwork_donejz;point to ASCII output;string;get fractional part;check for zero operand;check for zero operand395NUMERICAL METHODSsubax,axshlrclrclbx,1dx,1ax,1movmovmovword ptr svb,bxword ptr svd,dxword ptr sva,axshlrclrclbx,1dx,1ax,1shlrclrclbx,1dx,1ax,1addadcadcbx,word ptr svbdx,word ptr svdax,word ptr svaormovincsubloopal,'O'byte ptr [di],aldiax,axdecimal_conversion;multiply fraction by ten;times 2 multiple;multiply by tenwork_done:mov;end string with a nullbyte ptr [di],alclcretbfc_dc endp;;;*******************************************************;dfc_bn - A conversion routine that converts an ASCII decimal fraction;to binary representation.
Decptr points to the decimal string to be converted.;The conversion will produce a double word result.396IO.ASM;The fraction is expected to be padded to the right if it does not;fill eight digits.;dfcbn procuses bx cx dx si di, decptr:word, fraction:wordpushfcldmovsubmovrepne scasbdecdecdi, word ptr decptrax,axcx, 9didimovsi,dimovmovmovdi, word ptr fractionword ptr [di],axword ptr [di][2], axmovcx,8subdx,dxbinary_conversion:movax, word ptr [di][2]movcmpjbcmpjadl, byte ptr [si]dl, '0'oopsdl, '9'oopsxordl, '0'decsi;point to least;significant byte;get high word of result;variable;check for decimal digit;if it gets past here, it;must be o.k.;deASCIIizesub397NUMERICAL METHODSororjzdivno_div0:movbx,dxbx,axno_div0itenmovsubororjzdivno_divl:movax,word ptr [di]bx,bxbx,dxbx,axno_div1itensubloopword ptr [di][2],axword ptr [di],axdx,dxbinary_conversionwork_done:popfsubclcretax,axoops:popfmovax,-1stcretdfc_bn endp:;; ******;table conversion routines398;prevent a divide by zero;divide by ten;prevent a divide by zeroIO.ASM.dataint_tabdwordfrac_tabdwordtab_enddword3b9aca00h,000186a0h,0000000ah,1999999ah,0000a7c5h,00000004h00000000h05f5e100h,00002710h,00000001h028f5c29h,000010c6h,00989680h, 000f4240h,000003e8h, 00000064h,00418937h, 00068db9h,000001adh, 0000002ah,;.code;convert ASCII decimal to fixed-point binary;tb_dcbnproc uses bx cx dx si di,sptr:word, fxptr:wordreplocalsign:bytemovmOVleadi, word ptr sptrsi, word ptr fxptrbx, word ptr frac_tab;point to result;point to ASCII string;point into tablemovsubsubstoswcx,4ax,axdx,dx;clear the target variablemovdi, word ptr sptr;point to resultmovmovmovcl,alch,9hbyte ptr sign, al;to count integers;max int digits;assume positivemovcmpjecmpjeal, byte ptr [si]al,'-'negativeal,'+'positive;get character;check for sign399NUMERICAL METHODScount:cmpjechk_frac:cmpjecmpjbcmpjaal,'.'fnd_dotal,0gotnumberal,'0'not_a_numberal,'9'not_a_number;end of string?;is it a number then?cntnu:inccmpjaincmovorjnejmpclcl,chtoo_bigsial, byte ptr [si]dh,dhchk_fracshort countfnd_dot:movincmovxchgjmpnegative:notpositive:incmovmovjmpsiword ptr fxptr,sial, byte ptr [si]short countgotnumber:subxchgdecshlch,chcl,dhclword ptr cx,1400dh,cldhdl,13hch,dlshort cntnu;count;check size;next character;get character;int or frac;count characters in int;can't be zero;includes decimal pointsign;get int count;multiply by fourIO.ASMshlsubsubmovcnvrt_int:movcmpjecmpjesubmovmuladdadcmovmuladdadcaddincjmphandle_fraction:inccnvrt_frac:movcmpjesubmovmuladdmovmuladdadcaddincword ptr cx,lbx,cxcx,cxsi,word ptr fxptr;index into able;point at string againcl,byte ptr [si]cl,'.'handle_fractioncl,0do_signcl,'O'ax,word ptr [bx][2]cxword ptr [di][4l,axword ptr [di][6],dxax,word ptr [bx]cxword ptr [di][4],axword ptr [di][6],dxbx,4sishort cnvrt_int;get first charactersi;skip decimal pointcl,byte ptr [si]cl,0do_signcl,'0'ax,word ptr [bx][2];get first charactercxword ptr [di][2],axax,word ptr [bx]cxword ptr [di][0],axword ptr [di][2],dxbx,4si;go do fraction, if any;end of string;drop table pointer;end of string;this can never result in;a carry;drop table pointer401NUMERICAL METHODSjmpdo_sign:movorjenotnotnotnegjcaddadcadcexit:retnot_a_number:subnottoo_big:stcjmptb_dcbnshort cnvrt_fracal,byte ptr signal,alexitword ptr [di][6]word ptr [dil[4]word ptr [di][2]word ptr [di]exitword ptr [di][2],1word ptr [di][4],0word ptr [di][6],0;it is positiveax,axaxshort exitendp;converts binary to ASCII decimaltb_bndc402procuses bx cx dx si di,sptr:word, fxptr:wordlocalleading_zeros:bytemovmovleasi, word ptr fxptrdi, word ptr sptrbx, word ptr int_tab;point to input fix point;point to ascii string;point into tablesubmovax,axbyte ptr leading_zeros, al;assume positiveIO.ASMmovorjnsmovincnotnotnotnegjcaddadcadcpositive:movmovsubwalk_tab:cmpjajbcmpjaepushptr:cmpjemovax, wordax,axpositivebyte ptrdiword ptrword ptrword ptrword ptrpositiveword ptrword ptrword ptrptr [si][6][di],'-'[si][6][si][4][si][2][si][0];complement[si][2],1[si][4],0[si][6],0dx, word ptr [si][6]ax, word ptr [si][4]cx,cx;get integerportiondx, word ptr [bx][2]gotnumberpushptrax, word ptr [bx]gotnumberbyte ptr cl, leading_zerosskip_zeroword ptr [di],'0'cntnu:incskip_zero:incincincinccmpjaejmpdibxbxbxbxbx, offset word ptr frac_tabhandle_fractionshortwalk_tab403NUMERICAL METHODSgotnumber:subinccnvrt_int:calljmphandle_fraction:cmpjnemovincdo_frac:movincget_frac:movmovsubwalk_tabl:cmpjajbcmpjaepushptr1:movskip_zero1:incincincincinccmpjaejmpsmall_enuf:subsmall_enuf1:404cx,cxleading_zerosnear ptr indexshort cntnubyte ptr leading_zeros,0do_fracbyte ptr [di], '0'diword ptr [di],'.'didx, word ptr [si][2]ax, word ptr [si][0]cx,cxdx, word ptr [bx][2]small_enufpushptr1ax, word ptr [bx]small_enufbyte ptr [di],'0'dibxbxbxbxbx, offset word ptr tab_endexitshort walk_tab1cx,cx;put decimal pointIO.ASMcalljmpnear ptr indexshort skip_zero1incsubmovretdicl,clbyte ptr [si],clincsubsbbjncdecaddadcormovretncxax, worddx, wordindexcxax, worddx, wordcl,'0'byte ptrexit:;end of stringindex:tb_bndcptr [bx]ptr [bx][2];subtract until a carryptr [bx]ptr [bx][2];put it back;make it ASCII[di],clendp; ******;hex to ascii conversion using xlat;simple and common table driven routine to convert from hexidecimal;notation to ascii;quadword argument is passed on the stack, with the result returned;in a string pointed to by sptr.datahextab byte'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a','b', 'c', 'd', 'e', 'f'.codehexasc procuses bx cx dx si di, hexval:qword, sptr:word405NUMERICAL METHODSleamovmovmovmake_ascii:movmovshrshrshrshrandxchgxlatmovincxchgxlatmovinc;unpack byte;high nibble firstbyte ptr[di],aldial,ah;now the lower nibblebyte ptr [di],aldisimake_asciisubmoval, albyte ptr [di],alhexasc endp;end;number of bytes to be;convertedal, byte ptr [si]ah, alah,1ah,1ah,1ah,1al, 0fhal,ahdecloopret406si, byte ptr hexval[7]di, word ptr sptrbx, offset byte ptr hextabcx,8PreviousAPPENDIX FTRANS.ASMand TABLE.ASMTRANS.ASM.model small, c, os_dosinclude math.inc;.datainfzrobytebytezeroone_over_pitwo_over_pihalf_pione_over_ln2ln2sqrt_halfexpepsepsymaxbig_xlittlexy0ay0bquartercircularkhyperkqwordqwordqwordqwordqwordqwordqwordqwordqwordqwordqwordqwordqwordqwordqwordqwordqword"infinite", 0"0.0",0000000000000h3ea2f9836e4eh3f22f9836e4eh3fc90fdaa221h3fb8aa3b295ch3f317217f7d1h3f3504f30000h338000000001h39fffff70000h45c90fdb0000h42a000000000h0c2a000000000h3ed5a9a80000h3f1714ba0000h3e8000000000h9b74eda7h1351e8755h407HomeNextNUMERICAL METHODSplusminusqwordqwordhundreditenmaxchar;bytewordequ3f800000000h0bf800000000h64h0ah8.code;taylorsin - derives a sin by using a infinite series.