home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 031 / roots02.zip / ROOTS.ASM < prev    next >
Assembly Source File  |  1994-09-18  |  7KB  |  318 lines

  1. ; ROOTS  Copyright (C) 1992-1994 by Celso Minnitti Jr. All Rights Reserved.
  2. ;
  3. ; Version:  0.2 (September-18-1994)
  4. ;
  5. ; Compile:  TASM /m2 example
  6. ;           TLINK /t /3 /x example
  7. ;
  8. ; I just wrote this program because I found quite interesting two new
  9. ; algorithms that developed to find N^2 and N^3 only using additions.
  10. ; To compute N^2 we need two registers and two ADDs.
  11. ; To compute N^3 we need three registers and three ADDs.
  12. ;
  13. ; Please note that:
  14. ;
  15. ;    1     4   9    16  25    36  49
  16. ;     \-/ \-/ \-/ \-/ \-/ \-/
  17. ;      3   5   7   9   11  13        (subtract 4-1, 9-4,...)
  18. ;    \-/ \-/ \-/ \-/ \-/
  19. ;     2   2     2   2     2        (2!) 2 factorial...
  20. ;
  21. ;
  22. ;    1     8  27    64 125 216
  23. ;     \-/ \-/ \-/ \-/ \-/
  24. ;      7  19   37  61  91        7 = 8-1    19 = 27-8    37 = 64-27
  25. ;    \-/  \-/  \-/  \-/
  26. ;     12  18   24   30        12=19-7    18 = 37-19   30 = 91-61
  27. ;       \-/    \-/  \-/
  28. ;        6     6    6         (3!) 3 factorial...
  29. ;
  30. ; N^4 will require 4 additions and the constant to add is 24 (4!)...
  31. ; N^5 will require 5 additions and the constant to add is 120 (5!)...
  32. ; N^6 will require 6 additions and the constant to add is 720 (6!)...etc...
  33. ;
  34. ; If you find these algorithms interesting please let me know... If you
  35. ; have any questions or comments, please contact me at:
  36. ;
  37. ; Celso Minnitti Jr
  38. ; 139 Linden St
  39. ; Wellesley, MA 02181
  40. ; Home phone #: 617-235-4018
  41. ; email: celsomj@world.std.com
  42.  
  43.  
  44. _TEXT   segment byte public 'CODE'
  45.     assume cs:_TEXT, ds:_TEXT
  46.     org    100h
  47. .386
  48.  
  49. ANY_NUMBER    =    10000            ;can be any number from
  50.                         ;1 to 429101525 (0FFC3B3C9h)
  51.  
  52. start:        mov    ax,13h            ;320x200x256
  53.         int    10h
  54.  
  55.         cld                ;just to be safe...
  56.         mov    es,[vidseg]        ;A000
  57.         mov    di,150*320+10        ;x = 10, y = 150
  58.         mov    al,1            ;color = blue
  59.         mov    bx,140            ;draw 140 pixels
  60.  
  61. draw_x1:    mov    es:[di],al        ;draw y = x
  62.         sub    di,319
  63.         dec    bx
  64.         jnz    draw_x1
  65.  
  66.         call    getch            ;wait for a key
  67.  
  68.         mov    si,1
  69.         mov    dx,1
  70.         mov    bx,17            ;draw 17 segments...
  71.         mov    al,2            ;color = green
  72.         mov    di,150*320+10        ;x = 10, y = 155
  73.  
  74. draw_x2:    mov    cx,si
  75.         rep    stosb            ;draw y = x^2
  76.         add    si,2            ;si=1,3,5,7,9...
  77.         sub    di,320
  78.         dec    bx
  79.         jnz    draw_x2
  80.  
  81.         call    getch
  82.  
  83.                 mov     dx,1                    ;
  84.         mov    si,6            ;3 factorial...
  85.         mov    bx,6            ;draw 6 segments
  86.         mov    al,3            ;color = cyan
  87.         mov    di,150*320+10        ;x = 10, y = 160
  88.  
  89. draw_x3:    mov    cx,dx            ;
  90.         rep    stosb            ;draw y = x^3
  91.         add    dx,si            ;dx=1,7,19,37,61...
  92.         add    si,6
  93.         sub    di,320
  94.         dec    bx
  95.         jnz    draw_x3
  96.  
  97.         call    getch
  98.  
  99.         mov    ax,3            ;return to 80x25
  100.         int    10h
  101.  
  102.         mov    si,offset copyRight
  103.                 call    prints
  104.                 mov     si,offset message_1
  105.                 call    prints
  106.         mov    eax,ANY_NUMBER
  107.                 call    print2d
  108.                 mov     si,offset message_11
  109.                 call    prints
  110.  
  111.         call    root22            ;eax = sqrt, edx = reminder
  112.                 call    print2d
  113.                 mov     si,offset message_12
  114.                 call    prints
  115.                 mov     eax,edx
  116.                 call    print2d
  117.  
  118.                 call    print_crlf
  119.                 mov     si,offset message_2
  120.                 call    prints
  121.         mov    eax,ANY_NUMBER
  122.                 call    print2d
  123.                 mov     si,offset message_21
  124.                 call    prints
  125.  
  126.         call    root32            ;eax = cubic_rt, edx = reminder
  127.                 call    print2d
  128.                 mov     si,offset message_22
  129.                 call    prints
  130.                 mov     eax,edx
  131.                 call    print2d
  132.  
  133.         int    20h            ;exit to DOS
  134.  
  135. ;**************************************************************************
  136. ;                 ROOT22
  137. ; enter:   EAX = 1 - 0FFFE0001h (4294836225=65535*65535)
  138. ; return:  EAX = square root
  139. ;       EDX = reminder
  140. ;**************************************************************************
  141. root22        proc    near
  142.         push    ecx
  143.         push    ebx
  144.  
  145.         xor    ecx,ecx
  146.         inc    cx
  147.         mov    edx,ecx
  148.         mov    ebx,ecx          ;ecx=edx=ebx=1
  149.  
  150. root22_loop:    cmp    edx,eax
  151.         je    short root22_ret
  152.         ja    short root22_ret0
  153.  
  154.         add    ebx,2             ;ebx=1,3,5,7 ,9 ,11,13...
  155.         add    edx,ebx          ;edx=1,4,9,16,25,36,49...
  156.         inc    ecx             ;ecx=1,2,3,4 ,5 ,6 ,7 ...
  157.         jmp    short root22_loop
  158.  
  159. root22_ret0:    dec    ecx
  160.         sub    edx,ebx
  161. root22_ret:    sub    edx,eax
  162.         neg    edx
  163.         mov    eax,ecx
  164.         pop    ebx
  165.         pop    ecx
  166.         ret
  167. root22        endp
  168.  
  169.  
  170. ;**************************************************************************
  171. ;                 ROOT32
  172. ; enter:   EAX = 1 - 0FFC3B3C9h (4291015625=1625*1625*1625)
  173. ; return:  EAX = cubic root
  174. ;       EDX = reminder
  175. ;**************************************************************************
  176. root32        proc    near
  177.         push    ecx
  178.         push    ebx
  179.         push    esi
  180.  
  181.         xor    ecx,ecx
  182.         inc    cx
  183.         mov    edx,ecx
  184.         mov    ebx,ecx          ;ecx=edx=ebx=1
  185.         mov    esi,6
  186.  
  187. root32_loop:    cmp     edx,eax
  188.         je    short root32_ret
  189.         ja    short root32_ret0
  190.  
  191.         add    ebx,esi          ;ebx=7 ,19,37,61...
  192.         add    esi,6             ;esi=12,18,24,30...
  193.         add    edx,ebx          ;edx=8 ,27,64,125...
  194.         inc    ecx             ;ecx=2 , 3, 4, 5
  195.         jmp    root32_loop
  196.  
  197. root32_ret0:    dec    ecx
  198.         sub    edx,ebx
  199. root32_ret:    sub    edx,eax
  200.         neg    edx
  201.         mov    eax,ecx
  202.  
  203.         pop    esi
  204.         pop    ebx
  205.         pop    ecx
  206.         ret
  207. root32        endp
  208.  
  209.  
  210. ;**************************************************************************
  211. ;                PRINT2D
  212. ; enter:   EAX = # to print in decimal
  213. ;**************************************************************************
  214. print2d     proc    near
  215.         push    eax
  216.                 push    cx
  217.                 push    edx
  218.         push    ebx
  219.         push    si
  220.  
  221.         xor    cx,cx
  222.         mov    si,offset pbuff
  223.         mov    ebx,10
  224.  
  225. pri21d:     xor    edx,edx
  226.         div    ebx
  227.         add    dl,30h
  228.         mov    [si],dl
  229.         inc    si
  230.         inc    cx
  231.         or    eax,eax
  232.         jnz    short pri21d
  233.  
  234. pri22d:     dec    si
  235.         mov    al,[si]
  236.                 call    putchar
  237.         dec    cx
  238.         jnz    pri22d
  239.  
  240.         pop    si
  241.         pop    ebx
  242.         pop    edx
  243.         pop    cx
  244.         pop    eax
  245.         ret
  246. print2d     endp
  247.  
  248.  
  249. ;**************************************************************************
  250. ;                  PRINTS
  251. ; enter:   DS:SI = address of string
  252. ;**************************************************************************
  253. prints        proc    near
  254.         push    ax
  255.         push    si
  256. prints1:    mov    al,[si]
  257.         inc    si
  258.         or    al,al
  259.         jz    prints_ret
  260.         call    putchar
  261.         jmp    prints1
  262. prints_ret:    pop    si
  263.         pop    ax
  264.         ret
  265. prints        endp
  266.  
  267.  
  268. ;**************************************************************************
  269. ;                 PUTCHAR
  270. ; enter:   AL = character to write
  271. ;**************************************************************************
  272. putchar         proc    near
  273.         push    ax
  274.         push    cx
  275.         push    bx
  276.         mov    ah,0eh             ;Teletype Output
  277.         mov    cx,1             ;# times
  278.         mov    bh,0             ;page number
  279.         int    10h
  280.         pop    bx
  281.         pop    cx
  282.         pop    ax
  283.         ret
  284. putchar         endp
  285.  
  286. ;**************************************************************************
  287. ;                  GETCH
  288. ; return:  AH = scancode
  289. ;       AL = character
  290. ;**************************************************************************
  291. getch        proc    near
  292.         mov    ah,0
  293.         int    16h
  294.         ret
  295. getch        endp
  296.  
  297. print_crlf:     mov     si,offset crlf
  298.                 call    prints
  299.                 ret
  300.  
  301. copyRight    db    'ROOTS 0.2 (C) Copyright 1994  Celso Minnitti Jr',13,10,10,0
  302. message_1       db      'The square root of ',0
  303. message_11      db      ' is: ',0
  304. message_12      db      '  .Reminder = ',0
  305.  
  306. message_2       db      'The cubic root of ',0
  307. message_21      db      ' is: ',0
  308. message_22      db      '  .Reminder = ',0
  309. crlf            db      13,10,0
  310.  
  311. vidseg        dw    0a000h
  312.  
  313. pbuff        label    byte
  314.  
  315. _TEXT    ends
  316. end    start
  317.  
  318.