Vistas de página en total

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

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




lunes, 10 de junio de 2013

DECLARACIÓN FUNCIONES DE CADENA

        Vamos a ver y crear parte de la librería STRING, la cual servirá para el manejo de cadenas. Es importante hacer parte de esta librería enseguida pues en ella tenemos la función itoa y atoi, que vamos a necesitar en las funciones Scanf y Prinf de la librería básica STDIO.
     Por ello comenzaré con la  cabecera STRING.MAC y posteriormente las funciones atoi y itoa, para poder realizar una parte muy importante de la librería que necesitaremos posteriormente para la librerías STDIO.
    Con esta parte de String y la posterior STDIO, estaremos ya en condiciones de hacer nuestros primeros programas de entrada y salida de texto y valores por pantalla.
 
     Como veis, no hace falta decir mucho de las funciones, pues son estructuras iguales a las anteriores, para las llamadas a las funciones auxiliares. Los nombres son parecidos a los usados en C.
      Con estas funciones podremos manejar los textos que entremos por teclado y presentarlos de forma adecuada. Igualmente, cuando más tarde creemos la librería FILE y IO, podremos crear bases de datos con sus ALTAS-BAJAS-MODIFICACIONES.
       Como podemos crear la macro, pero no usarla, podremos hacer el fichero .MAC con todas las funciones, pero solo implementar en la librería itoa y atoi. Lo haremos así, dejando el resto para después de los primeros ejemplos.


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

;pasa el caracter al a mayusculas
; se le puede pasar o no parámetro.
; Si no se le pasa parámetro asume que se pasa a mayusculas al
upr macro letra 
local salir
ifnb <letra>
        mov al,letra

endif
        cmp  al,'a'
        jb salir
        cmp al,'z'
        ja salir
        sub al,'a'-'A'
salir:
ifnb <letra>
        mov letra,al

endif
        endm

;pasa la cadena a mayusculas
strupr macro direccion
ifndef _may
    extrn _may:near
endif
        lea ax,direccion
        push ax
        call _may
        pop ax
endm

;devuelve la longitud de la cadena
STRLEN MACRO CADENA
ifndef _strlen
    extrn _strlen:near
endif
       lea AX,CADENA
       PUSH AX
       CALL _STRLEN
       ADD SP,2
ENDM

;compara dos cadenas devuelve las banderas como en una comparacion normal
strcmp macro destino,fuente
ifndef _strcmp
    extrn _strcmp:near
endif
        lea ax,fuente
        push ax
        lea ax,destino
        push ax
        call _strcmp
        pop ax
        pop ax
 endm

strcpy macro destino,fuente
ifndef _strcpy
    extrn _strcpy:near
endif
        lea ax,fuente
        push ax
        lea ax,destino
        push ax
        call _strcpy
        pop ax
        pop ax
 endm

strcut macro destino,inic,fin,fuente
ifndef _strcut
     extrn _strcut:near
endif
        lea ax,fuente
        push ax
        mov ax,fin
        push ax
        mov ax,inic
        push ax
        lea ax,destino
        push ax
        call _strcut
        add sp,8
 endm

strcat macro destino,fuente1,fuente2
 ifndef _strcat
      extrn _strcat:near
 endif
        lea ax,fuente2
        push ax
        lea ax,fuente1
        push ax
        lea ax,destino
        push ax
        call _strcat
        add sp,6
  endm


atoi macro destino,texto,base
  ifndef _atoi
        extrn _atoi:near
  endif
  ifb <base>
        mov ax,10
  else
        mov ax,base
  endif
        push ax
        lea ax,texto
        push ax
        lea ax,destino
        push ax
        call _atoi
        add sp,6
   endm


itoa macro destino,numero,base
ifndef _itoa
        extrn _itoa:near
endif
ifb <base>
        mov ax,10
else
        mov ax,base
endif
        push ax
        lea ax,numero
        push ax
        lea ax,destino
        push ax
        call _itoa
        add sp,6
 endm