Vistas de página en total

viernes, 14 de junio de 2013

CADENA A ENTERO

  Esta función, que es parte de la cabecera STRING. Nos permitirá convertir cualquier cadena de caracteres, que represente un número en cualquier base, a su valor numérico en un valor entero.

    En este caso como puede verse, nos hemos apoyado en la función anterior _may, la cual no se llamada a través de la macro strupr definida en STRING.MAC, sino  copiando el código de la macro.
    Esto es debido que la macro usa lea para tomar la dirección de la variable, y en este caso la dirección la tomamos del dato que ya se ha pasado por pila.

    Como ya habíamos definido la cabecera STRING.MAC, ahora podemos usarla en esta función, para directamente usar la función que necesitamos y que ya hemos implementado. Es decir strupr. por ello al comienzo de la función incluimos las cabeceras main ( para usar rutina) y string ( para usar strpur).

ATOI.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/>.

include main.mac
include string.mac
_modelo exe
_code

      extrn _may:near
rutina _atoi dest,text,base
        push bx
        push cx
        push dx
        push si
        push di
        mov di,offset numero
   nuevo:

       ;strupr [bp].text
         ifndef _may
              extrn _may:near
         endif
        mov ax,[bp].text
        push ax
        call _may
        pop ax

        mov bx,ax
        cmp word ptr [bp].base,10
        jne otro
        cmp byte ptr [bx],'-'            ;;compruevo si es negativo
        jne otro
        inc bx
 otro:
        mov cx,[bp].base                 ;busqueda de caracter entre los definidos
        mov si,offset caracteres         ;en la base de numeracion
        mov al,[bx]
    busca:
        cmp al,[si]
        jz encont
        inc si
        loop busca
    encont:
        or cx,cx
        jz analisis
        mov ax,si
        sub ax,offset caracteres
        mov [di],al
        inc di
        inc bx
        cmp byte ptr  [bx],0
        jnz otro
   analisis:
        dec di
        mov bx,di
        mov di,[bp].dest        ;direccion de la variable dword
        mov word ptr [di],0
        mov word ptr [di+2],0
        mov multl,1               ;peso del bit 1,2,4,8
        mov multh,0
        mov cx,[bp].base         ;base de numeracion

  acum:
        mov al,[bx]
        cbw
        mov si,ax
        mov ax,multl
        mul si
        add [di],ax
        adc [di+2],dx
        mov ax,multh
        mul si
        add [di+2],ax
        mov ax,multh
        mul cx
        mov multh,ax
        mov ax,multl
        mul cx
        mov multl,ax
        add multh,dx

        dec bx                   ;sigiente numero
        cmp bx,offset numero-1
        je fin
        cmp byte ptr [bx],'-'
        jne acum
        xor word ptr [di],0ffffh
        xor word ptr [di+2],0ffffh
        add word ptr [di],1
        adc word ptr [di+2],0
  fin:  pop di
        pop si
        pop dx
        pop cx
        pop bx
        pop bp
        ret
_atoi endp

_data
caracteres db "0123456789ABCDEF"
numero db 33 dup (0)
multl dw 0
multh dw 0
_end




No hay comentarios:

Publicar un comentario

Si tienes algún comentario, duda o sugerencia, o si quieres aportar algún código creado a partir de las librerías expuestas aquí, por favor, indícamelo.