Advertisement
Guest User

z3.asm

a guest
Jun 5th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. FLOAT_SIZE = 20
  3.  
  4. data segment
  5.     argNumbers  db  0
  6.     cmdLine     db  80h dup (?)
  7.     newLine     db  13, 10, '$'
  8.     floatTmp    db  FLOAT_SIZE dup (?)
  9.     coprocPassQ dq  ?
  10.     coprocPassD dd  ?
  11.     coprocPassW dw  ?
  12.    
  13.     RES_X       dw  320
  14.     RES_Y       dw  200
  15.     ITERATIONS  dw  1000
  16.    
  17.     ten         dd  10.0
  18.     negativeOne dd  -1.0
  19.     condition   dd  4.0
  20.     two         dd  2.0
  21.    
  22.     numbers:
  23.         xmin        dq  ?
  24.         xmax        dq  ?
  25.         ymin        dq  ?
  26.         ymax        dq  ?
  27.         cr          dq  ?
  28.         ci          dq  ?
  29.        
  30.     zr          dq  0.0
  31.     zi          dq  ?
  32.     tmp         dq  ?
  33.    
  34.     zero            dq  0.0
  35.    
  36.    
  37.     ; Messages
  38.             wrongData   db  "Wprowadzono nieprawidlowe liczby.$"
  39.             wrongNumber db  "Nalezy wprowadzic dokladnie 6 liczb w formacie 123.456.$"
  40.  
  41. data ends
  42.  
  43. code segment
  44.     .386
  45.     .387
  46.     assume cs:code, ds:data, ss:stack
  47.     start:
  48.         call    dataInit
  49.        
  50.         push    offset argNumbers
  51.         push    offset cmdLine
  52.         call    getCmdLine
  53.         add     sp, 4
  54.        
  55.         cmp     ds:[argNumbers], 6
  56.         jne     not6Numbers
  57.        
  58.         push    offset cmdLine
  59.         push    offset numbers
  60.         push    6
  61.         call    strToFloats
  62.         add     sp, 6
  63.        
  64.         cmp     ax, 0
  65.         jne     exitProgram
  66.        
  67.         call    drawSet
  68.        
  69.         cmp     al, 0
  70.         jne     exitProgram             ; ERROR CHECK
  71.        
  72.         mov     al, 0
  73.     exitProgram:
  74.         mov     ah, 4ch         ;exit code
  75.         int     21h
  76.        
  77.     not6Numbers:
  78.         mov     ah, 9h
  79.         mov     dx, offset wrongNumber
  80.         int     21h
  81.         mov     ax, 2
  82.         jmp     exitProgram
  83.    
  84.     ; |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  85.     ; |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  86.     ; |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  87.    
  88.     ; DATA INIT * * * * * * * * * * * * * * * * * * * * * * * * * * *
  89.     dataInit proc
  90.         mov     dx, seg data        ;data segment initialization
  91.         mov     ds, dx 
  92.  
  93.         mov     ax, seg stack       ;stack initialization
  94.         mov     ss, ax
  95.         mov     sp, offset topStack
  96.        
  97.         finit
  98.        
  99.     ret
  100.     dataInit endp
  101.    
  102.     ; GET CMD LINE* * * * * * * * * * * * * * * * * * * * * * * * * *
  103.     getCmdLine proc
  104.         ;[AX, CX, DX, BX, SP, BP, SI, DI][ret][command line ptr][argument numbers ptr]
  105.         ;^
  106.         pusha  
  107.         mov     bp, sp
  108.         mov     ah, 62h
  109.         int     21h         ; int 21h with 62h returns PSP structure
  110.         mov     es, bx      ; es = bx (the PSP address)
  111.  
  112.         mov     cx, es:[80h]
  113.         mov     di, [bp + 18]   ; di = command line address
  114.         cmp     cl, 0          
  115.         jz      exitGetCmdLine
  116.         mov     si, 82h         ; si = 82h - first character from command line in the PSP structure
  117.         mov     cx, 1
  118.        
  119.                        
  120.         loopCmdLine:       
  121.             mov     al, es:[si]
  122.             cmp     al, 0dh    
  123.             jz      exitGetCmdLine
  124.             cmp     al, ' '    
  125.             jz      removeWhite    
  126.             cmp     al, 9h
  127.             jz      removeWhite
  128.             mov     byte ptr [di], al
  129.             inc     di
  130.             inc     si
  131.         jmp     loopCmdLine
  132.        
  133.         exitGetCmdLine:
  134.             mov     al, 7h
  135.             mov     byte ptr ds:[di], al
  136.             mov     ax, [bp + 20]
  137.             mov     di, ax
  138.             mov     byte ptr ds:[di], cl
  139.             popa
  140.     ret
  141.        
  142.         removeWhite:
  143.             cmp     byte ptr [di-1], 7h
  144.             je      removeWhiteCon
  145.             mov     byte ptr [di], 7h
  146.             inc     di
  147.             inc     cl
  148.         removeWhiteCon:
  149.             inc     si
  150.             jmp     loopCmdLine
  151.     getCmdLine endp
  152.    
  153.     ; STR ARRAY ELEMENT * * * * * * * * * * * * * * * * * * * * * * *
  154.     getStringArrayElement proc
  155.         ;[cx][bp][di][si][ret][str to save][index][array size][source str array]
  156.         ;^
  157.         push    si
  158.         push    di
  159.         push    bp
  160.         push    cx
  161.         mov     bp, sp
  162.         mov     ax, ss:[bp+14]  ; ax = array size
  163.         dec     ax
  164.         cmp     ss:[bp+12], ax  ; cmp: index and array size [AX]
  165.         jg      getStringError  ; if index > array size => error
  166.         mov     cx, ss:[bp+12]  ; cx = index
  167.         mov     si, ss:[bp+16]  ; si = source str array
  168.         mov     di, ss:[bp+10]  ; di = str to save
  169.        
  170.         cmp     cx, 0
  171.         je      copyStringElement
  172.        
  173.         dec     si
  174.         goToIndex:              ; setting si in the source array
  175.             inc     si
  176.             cmp     byte ptr ds:[si], 7h
  177.             jne     goToIndex
  178.             dec     cx
  179.             cmp     cx, 0
  180.         jne     goToIndex
  181.        
  182.         inc     si
  183.        
  184.         copyStringElement:      ; copying array element to the string destination
  185.             mov     al, byte ptr ds:[si]
  186.             mov     byte ptr ds:[di], al
  187.             inc     si
  188.             inc     di
  189.             cmp     byte ptr ds:[si], 7h
  190.         jne     copyStringElement
  191.            
  192.         mov     byte ptr ds:[di], 7h    ; array terminator (convention)
  193.         jmp     getStringArrayElementExit
  194.        
  195.         getStringError:
  196.             mov     al, 2
  197.            
  198.         getStringArrayElementExit:
  199.             pop     cx 
  200.             pop     bp
  201.             pop     di
  202.             pop     si
  203.     ret
  204.     getStringArrayElement endp
  205.    
  206.    
  207.     ; STR TO FLOATS * * * * * * * * * * * * * * * * * * * * * * * * *
  208.     strToFloats proc
  209.         ;[dx][cx][bp][di][si][ret][elements to load][offset dst array][offset source array]
  210.         ;^     
  211.         push    si
  212.         push    di
  213.         push    bp
  214.         push    cx
  215.         push    dx
  216.        
  217.         mov     bp, sp          ; bp = sp      
  218.         mov     cx, ss:[bp+12]  ; cx = elements to load
  219.         mov     di, ss:[bp+14]  ; di = offset dst array
  220.         mov     si, ss:[bp+16]  ; si = offset source array
  221.        
  222.         mov     di, offset xmin
  223.        
  224.         mov     ax, 0
  225.        
  226.         loadArrayLoop:
  227.             push    ax
  228.            
  229.             push    si
  230.             push    80h
  231.             push    ax
  232.             push    offset floatTmp
  233.             call    getStringArrayElement
  234.             add     sp, 8
  235.  
  236.             push    di
  237.             push    offset floatTmp
  238.             call    convertStringToFloat
  239.             add     sp, 4
  240.            
  241.             cmp     ax, 0
  242.             jne     strToFloatsExitA
  243.  
  244.             add     di, 8
  245.             pop     ax
  246.             inc     ax
  247.             dec     cx
  248.         jnz loadArrayLoop
  249.        
  250.         mov     ax, 0
  251.        
  252.         jmp     strToFloatsExitB
  253.        
  254.         strToFloatsExitA:
  255.         add     sp, 2
  256.        
  257.         strToFloatsExitB:
  258.        
  259.         pop     dx
  260.         pop     cx
  261.         pop     bp
  262.         pop     di
  263.         pop     si
  264.     ret
  265.     strToFloats endp
  266.    
  267.     ; CONVERT STRING TO FLOAT * * * * * * * * * * * * * * * * * * * *
  268.     convertStringToFloat proc
  269.         ;[di][si][bp][cx][ret][offset str source][offset dst float]
  270.         ;^
  271.         push    cx
  272.         push    bp
  273.         push    si
  274.         push    di
  275.        
  276.         mov     bp, sp
  277.         mov     si, ss:[bp+10]  ; si = float number (string)
  278.         mov     di, ss:[bp+12]  ; di = destination memory for float number
  279.        
  280.         fld     ten
  281.         fldz    ; st(0) = 0.0, st(1) = 10.0
  282.        
  283.         cmp     byte ptr ds:[si], '-'   ; omitting '-' symbol
  284.         jne     str2floatA
  285.         inc     si
  286.        
  287.         str2floatA: ; integer part conversion
  288.             mov     ah, 0
  289.             mov     al, byte ptr ds:[si]
  290.            
  291.             cmp     al, '.'
  292.             je      str2floatB
  293.            
  294.             cmp     al, '0'     ; checking whether it contains only decimal digits
  295.             jl      wrongChar
  296.             cmp     al, '9'
  297.             jg      wrongChar
  298.            
  299.             sub     al, '0'
  300.             mov     word ptr ds:[coprocPassW], ax
  301.             fmul    st, st(1)
  302.             fiadd   coprocPassW
  303.             inc     si
  304.         jne     str2floatA
  305.        
  306.         str2floatB: ; getting to the end of the number
  307.             inc     si
  308.             cmp     byte ptr ds:[si], 7h
  309.         jne     str2floatB
  310.         dec     si
  311.        
  312.         fstp    st(2)   ;st => st(2)
  313.         fldz            ;st = 0, st(1) = 10.0, st(2) = old st
  314.        
  315.         str2floatC: ; decimal part conversion
  316.             mov     ah, 0
  317.             mov     al, byte ptr ds:[si]
  318.            
  319.             cmp     al, '.'
  320.             je      str2floatOut
  321.            
  322.             cmp     al, '0'     ; checking whether it contains only decimal digits
  323.             jl      wrongChar
  324.             cmp     al, '9'
  325.             jg      wrongChar
  326.            
  327.             sub     al, '0'
  328.             mov     word ptr ds:[coprocPassW], ax
  329.             fiadd   coprocPassW
  330.             fdiv    st, st(1)
  331.             dec     si
  332.         jne     str2floatC
  333.        
  334.         str2floatOut:
  335.        
  336.         fadd    st, st(2)               ; adding integer and floating point part
  337.        
  338.         mov     si, ss:[bp+10]          ; si = float number (string)
  339.         cmp     byte ptr ds:[si], '-'   ; if it's a negative number...
  340.         jne     convCont               
  341.         fmul    negativeOne             ; it's being multiplied by negative one (-1.0)
  342.        
  343.         convCont:                       ; copying number to tmp variable in memory (coprocPassQ)
  344.         fstp    coprocPassQ
  345.        
  346.         mov     si, offset coprocPassQ  ; copying number from coprocPassQ to destination variable
  347.         mov     cx, 8
  348.        
  349.         push    es
  350.         mov     ax, ds
  351.         mov     es, ax
  352.         cld
  353.         rep     movsb
  354.         pop     es
  355.        
  356.         mov     ax, 0
  357.        
  358.         exitStrToFloatConv:
  359.        
  360.         faddp   st, st
  361.        
  362.         pop     di
  363.         pop     si
  364.         pop     bp
  365.         pop     cx
  366.     ret
  367.         wrongChar:                  ; error check
  368.             mov     ah, 9h
  369.             push    dx
  370.             mov     dx, offset wrongData
  371.             int     21h
  372.             pop     dx
  373.             mov     ax, 3
  374.             jmp     exitStrToFloatConv
  375.     convertStringToFloat endp
  376.    
  377.     ; DRAW SET* * * * * * * * * * * * * * * * * * * * * * * * * * * *
  378.     drawSet proc
  379.         ;;[AX, CX, DX, BX, SP, BP, SI, DI][ret][offset numbers]
  380.         ;^
  381.        
  382.         pusha
  383.        
  384.         mov     ah, 0       ; graphic mode
  385.         mov     al, 13h
  386.         int     10h
  387.        
  388.         mov     ax, 0A000h
  389.         mov     es, ax      ; es = VGA memory
  390.        
  391.         mov     al, 0
  392.         mov     di, 0
  393.        
  394.         mov     bx, 0
  395.         loopY:
  396.            
  397.             push    bx
  398.             call    calculateIm
  399.             add     sp, 2
  400.            
  401.             mov     dx, 0
  402.             loopX:
  403.                 push    dx
  404.                 call    calculateRe
  405.                 add     sp, 2
  406.                
  407.                 call    discrepancy
  408.                
  409.                 ; 320 = 256 + 64 = 2^8 + 2^6
  410.                 ; di = 320*y + x = 2^8*y + 2^6*y + x
  411.                 push    ax
  412.                 mov     ax, bx 
  413.                 mov     di, bx
  414.                 shl     di, 8
  415.                 shl     ax, 6
  416.                 add     di, ax
  417.                 add     di, dx
  418.                 pop     ax
  419.            
  420.                 cmp     al, 1
  421.                 je      blackPixel
  422.                
  423.                 mov     al, 31
  424.                 mov     byte ptr es:[di], al    ; putting white pixel
  425.                 jmp     loopContinue
  426.                
  427.                 blackPixel:
  428.                 mov     al, 0
  429.                 mov     byte ptr es:[di], al    ; putting black pixel
  430.                
  431.                 loopContinue:
  432.                 inc     dx
  433.                 cmp     dx, word ptr ds:[RES_X]
  434.             jne loopX
  435.            
  436.             inc     bx
  437.             cmp     bx, word ptr ds:[RES_Y]
  438.         jne loopY
  439.        
  440.         mov     ah, 7h      ; "get key" interruption
  441.         int     21h
  442.        
  443.         mov     al, 3       ; getting back to text mode
  444.         mov     ah, 0
  445.         int     10h
  446.        
  447.         popa
  448.        
  449.         ret
  450.     drawSet endp
  451.    
  452.     ; CALCULATE Re* * * * * * * * * * * * * * * * * * * * * * * * * *
  453.     calculateRe proc
  454.         ;[di][si][bp][ret][x]
  455.         ;^
  456.        
  457.         push    bp
  458.         push    si
  459.         push    di
  460.        
  461.         mov     bp, sp
  462.        
  463.         mov     ax, ss:[bp+8]       ; ax = x
  464.         mov     word ptr ds:[coprocPassW], ax
  465.        
  466.         fld     xmax
  467.         fld     xmin
  468.         fild    RES_X
  469.         fild    coprocPassW
  470.         fldz    ; coproc:[ 0.0 | X | RES_X | xmin | xmax ]
  471.                 ;        [  0  | 1 |   2   |   3  |   4  ]
  472.        
  473.         ; zr = xmin + x*(xmax-xmin)/RES_X
  474.         fadd    st, st(4)
  475.         fsub    st, st(3)
  476.         fdiv    st, st(2)
  477.         fmul    st, st(1)
  478.         fadd    st, st(3)
  479.        
  480.         ; passing calculeted float to variable in tmp memory
  481.         fstp    coprocPassQ
  482.         faddp   st, st
  483.         faddp   st, st
  484.         faddp   st, st
  485.         faddp   st, st
  486.        
  487.         ; copying variable from tmp to zr
  488.         mov     si, offset coprocPassQ
  489.         mov     di, offset zr
  490.         mov     cx, 8
  491.        
  492.         push    es
  493.         mov     ax, ds
  494.         mov     es, ax
  495.         cld
  496.         rep     movsb
  497.         pop     es
  498.        
  499.        
  500.         pop     di
  501.         pop     si
  502.         pop     bp
  503.         ret
  504.     calculateRe endp
  505.    
  506.     ; CALCULATE Im* * * * * * * * * * * * * * * * * * * * * * * * * *
  507.     calculateIm proc
  508.         ;[di][si][bp][ret][y]
  509.         ;^
  510.        
  511.         push    bp
  512.         push    si
  513.         push    di
  514.        
  515.         mov     bp, sp
  516.        
  517.         mov     ax, ss:[bp+8]   ; ax = y
  518.         mov     word ptr ds:[coprocPassW], ax
  519.        
  520.         fld     ymax
  521.         fld     ymin
  522.         fild    RES_Y
  523.         fild    coprocPassW
  524.         fldz    ; coproc:[ 0.0 | Y | RES_Y | ymin | ymax ]
  525.                 ;        [  0  | 1 |   2   |   3  |   4  ]
  526.        
  527.         ; zy = ymin + y*(ymax-ymin)/RES_Y
  528.         fadd    st, st(4)
  529.         fsub    st, st(3)
  530.         fdiv    st, st(2)
  531.         fmul    st, st(1)
  532.         fadd    st, st(3)
  533.        
  534.         ; passing calculeted float to variable in tmp memory
  535.         fstp    coprocPassQ
  536.         faddp   st, st
  537.         faddp   st, st
  538.         faddp   st, st
  539.         faddp   st, st
  540.        
  541.         ; copying variable from tmp to zi
  542.         mov     si, offset coprocPassQ
  543.         mov     di, offset zi
  544.         mov     cx, 8
  545.        
  546.         push    es
  547.         mov     ax, ds
  548.         mov     es, ax
  549.         cld
  550.         rep     movsb
  551.         pop     es
  552.        
  553.         pop     di
  554.         pop     si
  555.         pop     bp
  556.         ret
  557.     calculateIm endp
  558.    
  559.     ; DISCREPANCY * * * * * * * * * * * * * * * * * * * * * * * * * *
  560.     discrepancy proc
  561.         ;[FLAGS][cx][ret]
  562.         push    cx
  563.         pushf
  564.        
  565.         mov     cx, word ptr ds:[ITERATIONS]
  566.        
  567.         fldz
  568.         fldz
  569.         fld     condition
  570.         fld     zr
  571.         fld     zi
  572.         fld     cr
  573.         fld     ci
  574.         fldz    ; coproc:[ tmp | ci | cr | zi | zr | 4.0 | x*x | y*y ]
  575.                 ;        [  0  | 1  | 2  | 3  | 4  |  5  |  6  |  7  ]
  576.                
  577.         fxch    st(6)           ;x*x
  578.         faddp   st, st
  579.         fldz
  580.         fadd    st, st(4)
  581.         fmul    st, st
  582.         fxch    st(6)
  583.        
  584.         fxch    st(7)           ;y*y
  585.         faddp   st, st
  586.         fldz
  587.         fadd    st, st(3)
  588.         fmul    st, st
  589.         fxch    st(7)
  590.        
  591.         discrLoop:
  592.             faddp   st, st
  593.             fldz
  594.            
  595.             fadd    st, st(6)       ;tmp = x*x - y*y + cr
  596.             fsub    st, st(7)
  597.             fadd    st, st(2)
  598.            
  599.             fxch    st(3)           ;zi = 2*zi*zx + ci
  600.             fmul    st, st(4)
  601.             fmul    two
  602.             fadd    st, st(1)
  603.             fxch    st(3)
  604.            
  605.             fst     st(4)           ;zr = tmp
  606.            
  607.             faddp   st, st
  608.             fldz
  609.            
  610.             fxch    st(6)           ;x*x
  611.             faddp   st, st
  612.             fldz
  613.             fadd    st, st(4)
  614.             fmul    st, st
  615.             fxch    st(6)
  616.            
  617.             fxch    st(7)           ;y*y
  618.             faddp   st, st
  619.             fldz
  620.             fadd    st, st(3)
  621.             fmul    st, st
  622.             fxch    st(7)
  623.            
  624.             fadd    st, st(6)       ;tmp = x*x + y*y
  625.             fadd    st, st(7)
  626.            
  627.             fist    coprocPassW     ;checking whether x*x + y*y > 4
  628.             mov     ax, 4
  629.             cmp     word ptr ds:[coprocPassW], ax
  630.            
  631.             jge     outDiscrLoop
  632.         loop discrLoop
  633.         mov     al, 0
  634.         jmp     exitDiscr
  635.        
  636.         outDiscrLoop:
  637.         mov     al, 1
  638.        
  639.         exitDiscr:
  640.         faddp   st, st      ; clearing the coproc stack
  641.         faddp   st, st
  642.         faddp   st, st
  643.         faddp   st, st
  644.         faddp   st, st
  645.         faddp   st, st
  646.         faddp   st, st
  647.         faddp   st, st
  648.        
  649.         popf
  650.         pop     cx
  651.         ret
  652.     discrepancy endp
  653.    
  654. code ends
  655.  
  656. stack segment stack
  657.         dw 1000h dup(?)
  658.         topStack    dw  ?
  659. stack ends
  660.  
  661. end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement