130 likes | 326 Views
87 examples. Getting sqrt. call readint fstcw control;;;store current control word or control,0800h;set bit 11 clear bit 10 to round up fldcw control;;load new control word mov num,eax fild num fsqrt fistp sqr fwait mov edx, offset prompt2 call writestring mov eax,sqr call writeint.
E N D
Getting sqrt call readint fstcw control;;;store current control word or control,0800h;set bit 11 clear bit 10 to round up fldcw control;;load new control word mov num,eax fild num fsqrt fistp sqr fwait mov edx, offset prompt2 call writestring mov eax,sqr call writeint
Rounding set to round up • c:\Masm615>primes • enter an integer125 • sqrt of integer+12
Add code to check for prime and print divisors for composites mov eax,num call crlf mov ebx,2;;;first divisor top: xor edx,edx push eax div ebx;;divide mov divi,ebx cmp edx,0 je notprime inc ebx cmp ebx,sqr jg prime pop eax jmp top notprime: call writedec call crlf mov eax,divi call writedec call crlf mov edx,offset f call writestring prime: mov edx,offset t call writestring
Output from primes enter an integer1337711 18841 71 not a prime c:\Masm615>primes enter an integer17171731 746597 23 not a prime c:\Masm615>primes enter an integer313713 104571 3 not a prime c:\Masm615>primes enter an integer313717 prime c:\Masm615>
factorials call readint mov num,eax call crlf fld1 ;;load a 1 for subtacting and comparing..this will be st(2) fld1 ;;prod value initialized in st(1) fild num;;;multiplier... need to count down and multiply st by st(1) theloop: ftst ;;is st==0? fstsw status and status, 4100h;check bits c3 and c0...c3=0 c0=1 means st<source cmp status,4000h;;st==source jz done fmul st(1),st ;;leave prod in st(1) fsub st,st(2) jmp theloop done: fistp dummy fistp answer mov edx,offset p2 call writestring call crlf fwait mov eax,answer call writedec
factorials c:\Masm615>factorials enter an integer6 factorial is 720 c:\Masm615>factorials enter an integer7 factorial is 5040 c:\Masm615>factorials enter an integer20 factorial is 2147483648 c:\Masm615>
Fibonacci values mov edx, offset prompt call crlf call writestring call readint call crlf fld1 ;;load a 1 for subtacting and comparing..this will be st(2) fld1 ;;prod value initialized in st(1) top: cmp eax,0 je done fadd st(1),st fsubr st,st(1) ;;;reverse subtract!!!! cute huhn?st=st(1)-st dec eax jmp top done: fistp dummy fistp answer mov edx,offset p2 call writestring call crlf fwait mov eax,answer call writedec
Fibonacci c:\Masm615>fibs enter an integer4 fib is 8 c:\Masm615>fibs enter an integer6 fib is 21 c:\Masm615>fibs enter an integer7 fib is 34 c:\Masm615>fibs enter an integer100 fib is 2147483648 c:\Masm615>
Mimicking real io • You can output reals by outputting first the integer part, subtracting this from the original value, then repeatedly multiplying the fraction by ten, (subtracting this off from the remainder) and outputting this sequence of fractional digits. • This is NOT an IEEE f-p conversion routine!
Rounding control & the int part fstcw control or control,0C00h ;;;chop or truncate fldcw control fild ten ;;ten is in st(1) fild num ;;num is in st(0) fsqrt ;;sqrt in st fist intsqr ;;;store chopped result, don’t pop call crlf mov edx,offset message call writestring fwait mov ax,intsqr ;;write the int part call writedec
realio mov edx,offset dot;;decimal point call writestring fisub intsqr ;;subtract from sqrt the int part leaving fractional part ;now loop & store 5 decimal places mov edi, offset decimals mov ecx, 5 up: fmul st,st(1) ;;multiply by 10 to shift dec point fist digit ;store truncated int fisub digit ;;subtract it off of the total fwait mov ax,digit add al,48 mov byte ptr [edi],al ;;store this digit inc edi loop up
Run of realio c:\Masm615>realio enter an integer: 121 sqrt of integer 11.00000 c:\Masm615>realio enter an integer: 123 sqrt of integer 11.09053 c:\Masm615>realio enter an integer: 143 sqrt of integer 11.95826 c:\Masm615>