Vistas de página en total

Mostrando entradas con la etiqueta divi. Mostrar todas las entradas
Mostrando entradas con la etiqueta divi. Mostrar todas las entradas

jueves, 6 de junio de 2013

DIVIDIR


Función dividir de la librería matemática Math.



DIVI.ASM
; Copyright (C) 2013  José Ángel Moneo Fernández

;    This program is free software: you can redistribute it and/or modify
;    it under the terms of the GNU General Public License as published by
;    the Free Software Foundation, either version 3 of the License, or
;   (at your option) any later version.

;    This program is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;    GNU General Public License for more details.

;    You should have received a copy of the GNU General Public License
;    along with this program.  If not, see <http://www.gnu.org/licenses/>.

;; solo divide por numeros menores de ffffh
include main.mac
_modelo exe

_code
rutina _divi dividendo,divisor,resultado
        push bx
        push dx
        push cx
        push si
        push di
        mov si,[bp].dividendo
        mov di,[bp].resultado
        mov bx,[bp].divisor
        mov word ptr [di+2],0
        mov word ptr [di],0
        xor dx,dx
        cmp word ptr [bx+2],0
        jz normal
        mov ax,[si+2]
        cmp ax,[bx+2]
        jb fin
        mov cx,[bx+2]
        div cx
        mov [di],ax
comprueba:
        mul word ptr [bx]
        mov aux,ax
        mov aux+2,dx
        mov ax,[di]
        mul word ptr [bx+2]
        add aux+2,ax
        mov ax,aux+2
        cmp ax,[si+2]
        ja no_cabe
        jne fin
        mov ax,aux
        cmp ax,[si]
        jbe fin
no_cabe:
        dec word ptr [di]
        mov ax,[di]
        jmp comprueba

  normal:
        mov cx,[bx]
        mov ax,[si+2]
        div cx
        mov [di+2],ax
        mov ax,[si]
        div cx
        mov [di],ax
   fin:
        pop di
        pop si
        pop cx
        pop dx
        pop bx
        pop bp
        ret
_divi endp
_data
aux dw 0,0
_end






jueves, 23 de mayo de 2013

DECLARACIÓN FUNCIONES MATEMÁTICAS

    Ya que en la anterior entrada hemos colocado una función matemática, la función fac, continuaremos con la librería matemática.
   En este cas, como en todos, si el código no supera un determinado tamaño no se pasa a librería y se implementa como macro.
 ¿Cual es el tamaño para el cambio?. Si el tamaño que resulta de guardar los datos en pila, la inicialización de pila, y recuperación de la pila,  es mayor que tamaño en si del código de la función.

   Así pues, todas estas funciones son implementadas mediante macros que llaman a funciones, menos la comparación.

     En todas la macros existe la instrucción ifndef, para que no implemente el compilador la función en el código mas que una vez y no declare la función como externa si no es usada. De esta forma el linkador no cargará la rutina de la librería, pues no ha sido declarada.


MATH.MAC
; Copyright (C) 2013  José Ángel Moneo Fernández

;    This program is free software: you can redistribute it and/or modify
;    it under the terms of the GNU General Public License as published by
;    the Free Software Foundation, either version 3 of the License, or
;   (at your option) any later version.

;    This program is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;    GNU General Public License for more details.

;    You should have received a copy of the GNU General Public License
;    along with this program.  If not, see <http://www.gnu.org/licenses/>.
; cabecera funciones matemáticas

@b = 2  ;binario
@o = 8   ;octal
@d = 10  ;decimal
@h = 16 ;hexadecimal


; res=ope1 + ope2
suma macro ope1,ope2,res
ifndef _suma
       extrn _suma:near     
endif
        lea ax,res
        push ax
        lea ax,ope2
        push ax
        lea ax,ope1
        push ax
        call _suma
        add sp,6
     endm

; res=ope1 -ope2
resta macro ope1,ope2,res
ifndef _resta
       extrn _resta:near
endif
        lea ax,res
        push ax
        lea ax,ope2
        push ax
        lea ax,ope1
        push ax
        call _resta
        add sp,6
     endm

; res=ope1 * ope2
mult macro ope1,ope2,res
ifndef _mult
       extrn _mult:near
endif
        lea ax,res
        push ax
        lea ax,ope2
        push ax
        lea ax,ope1
        push ax
        call _mult
        add sp,6
     endm

; res=ope1 / ope2
divi macro ope1,ope2,res
ifndef _divi
       extrn _divi:near
endif
        lea ax,res
        push ax
        lea ax,ope2
        push ax
        lea ax,ope1
        push ax
        call _divi
        add sp,6
     endm

;sqr devuelve en ax el resultado de la raiz
sqr macro oper
ifndef _sqr
        extrn _sqr:near
endif
        lea ax,oper
        push ax
        call _sqr
        add sp,2
      endm

;fac calcula el factorial del int fuente y lo devuelve en destino
;destino=factorial(fuente)
fac macro fuente,destino
ifndef _fac
       extrn _fac:near
endif
        lea ax,destino
        push ax
        lea ax,fuente
        push ax
        call _fac
        add sp,4
  endm

;pot calcula x a la n siendo x=fuente,n=potenc y devuelve el resultado en destino
pot macro fuente,potenc,destino
ifndef _pot
       extrn _pot:near
endif
        lea ax,destino
        push ax
        mov ax,potenc
        push ax
        lea ax,fuente
        push ax
        call _pot
        add sp,6
  endm

; destino=
exp macro potenc,destino
ifndef _exp
       extrn _exp:near
endif
        lea ax,destino
        push ax
        mov ax,potenc
        push ax
        call _exp
        add sp,4
  endm

cmpi macro destino,fuente
local fin

     push ax
     mov ax,fuente+2
     cmp destino+2,ax
     jne fin
     mov ax,fuente
     cmp destino,ax
 fin:
     pop ax
     endm