IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

x86 16-bits Assembleur Discussion :

Fonctions atoi et itoa


Sujet :

x86 16-bits Assembleur

  1. #1
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    1
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2007
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Fonctions atoi et itoa
    Je recherche ces deux fonctions-ci.
    Si quelqu'un peut m'aider.
    Merci d'avance

  2. #2
    Responsable Pascal, Lazarus et Assembleur


    Avatar de Alcatîz
    Homme Profil pro
    Ressources humaines
    Inscrit en
    Mars 2003
    Messages
    7 939
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 57
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ressources humaines
    Secteur : Service public

    Informations forums :
    Inscription : Mars 2003
    Messages : 7 939
    Points : 59 409
    Points
    59 409
    Billets dans le blog
    2
    Par défaut
    Bonjour et bienvenue,

    Voici par exemple en Assembleur x86 16 bits, syntaxe MASM :

    atoi :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    ; ATOI.ASM --- Convert ASCII string to 16-bit decimal integer.
    ;
    ; By Ray Duncan
    ;
    ; Call with:	DS:SI = address of string,
    ;		where 'string' is in the form
    ;		   [whitespace][sign][digits]
    ;
    ; Returns:	AX    = result
    ;		DS:SI = address+1 of terminator
    ;
    ; Destroys:	Nothing
    ;
    ; Like the C runtime library function 'atoi', this routine gives no
    ; warning of overflow, and terminates on the first invalid character.
    _TEXT	segment 	word public 'CODE'
    _TEXT	ends
    _DATA	segment	              word public 'DATA'                               
    _DATA	ends                                                                           
    STACK	segment	              para stack 'STACK'                              
    STACK	ends                                                                            
    DGROUP	group	_DATA,STACK
    	assume	cs:_TEXT,ds:DGROUP,ss:DGROUP,es:DGROUP
     
     
    blank	equ	20h		; ASCII blank character
    tab	equ	09h		; ASCII tab character
     
    _TEXT	segment
     
    	public	atoi
    atoi	proc	near
     
    	push	bx			; save registers
    	push	cx
    	push	dx
     
    	xor	bx,bx			; initialize forming answer
    	xor	cx,cx			; initialize sign flag
     
    atoi1:	lodsb				; scan off whitespace
    	cmp	al,blank			; ignore leading blanks
    	je	atoi1
    	cmp	al,tab			; ignore leading tabs
    	je	atoi1
     
    	cmp	al,'+'			; if + sign, proceed
    	je	atoi2
    	cmp	al,'-'			; is it - sign?
    	jne	atoi3			; no, test if numeric
    	dec	cx			; was - sign, set flag
    					; for negative result
     
    atoi2:	lodsb				; get next character
     
    atoi3:	cmp	al,'0'			; is character valid?
    	jb	atoi4			; jump if not '0' to '9'
    	cmp	al,'9'
    	ja	atoi4			; jump if not '0' to '9'
     
    	and	ax,0fh			; isolate lower four bits
     
    	xchg	bx,ax			; previous answer x 10
    	mov	dx,10
    	mul	dx
     
    	add	bx,ax			; add this digit
     
    	jmp	atoi2			; convert next digit
     
    atoi4:	mov	ax,bx			; put result into AX
    	jcxz	atoi5			; jump if sign flag clear
    	neg	ax			; make result negative
     
    atoi5:	pop	dx			; restore registers
    	pop	cx
    	pop	bx
    	ret				; back to caller
     
    atoi	endp
     
    _TEXT	ends
     
    	end
    itoa :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    ; ITOA.ASM --- Convert 16-bit integer to ASCII string
    ;
    ; By Ray Duncan
    ;
    ; Call with:	AX    = 16-bit integer
    ;		DS:SI = buffer to receive string,
    ;			must be at least 6 bytes long
    ;		CX    = radix
    ;
    ; Returns:	DS:SI = address of converted string
    ;		AX    = length of string
    ;
    ; Destroy:	Nothing
    ;
    ; Since test for value = zero is made after a digit
    ; has been stored, the resulting string will always
    ; contain at least one significant digit.
    _TEXT	segment 	word public 'CODE'
    _TEXT	ends
    _DATA	segment	              word public 'DATA'                               
    _DATA	ends                                                                           
    STACK	segment	              para stack 'STACK'                              
    STACK	ends                                                                            
    DGROUP	group	_DATA,STACK
    	assume	cs:_TEXT,ds:DGROUP,ss:DGROUP,es:DGROUP
     
    _TEXT	segment
     
    	public	itoa
    itoa	proc	near
    	push dx
    	add	si,6			; advance to end of buffer
    	push	si			; and save that address.
    	or	ax,ax			; test sign of 16-bit value,
    	pushf				; and save sign on stack.
    	jns	itoa1			; jump if value was positive.
    	neg	ax			; find absolute value.
     
    itoa1:	mov	dx,0			; divide value by radix to extract
    	div	cx			; next digit for forming string
    	add	dl,'0'			; convert remainder to ASCII digit
    	cmp	dl,'9'			; in case converting to hex ASCII,
    	jle	itoa2			; jump if in range 0-9,
    	add	dl,'A'-'9'-1		; correct digit if in range A-F
     
    itoa2:	dec	si			; back up through buffer
    	mov	[si],dl 		; store this character into string
    	or	ax,ax
    	jnz	itoa1			; no, convert another digit
     
    	popf				; was original value negative?
    	jns	itoa3			; no, jump
    	dec	si			; yes, store sign into output
    	mov	byte ptr [si],'-'
     
    itoa3:	pop	ax			; calculate length of string
    	sub	ax,si
    	pop	dx
    	ret				; back to caller
     
    itoa	endp
     
    _TEXT	ends
     
    	end
    Règles du forum
    Cours et tutoriels Pascal, Delphi, Lazarus et Assembleur
    Avant de poser une question, consultez les FAQ Pascal, Delphi, Lazarus et Assembleur
    Mes tutoriels et sources Pascal

    Le problème en ce bas monde est que les imbéciles sont sûrs d'eux et fiers comme des coqs de basse cour, alors que les gens intelligents sont emplis de doute. [Bertrand Russell]
    La tolérance atteindra un tel niveau que les personnes intelligentes seront interdites de toute réflexion afin de ne pas offenser les imbéciles. [Fiodor Mikhaïlovitch Dostoïevski]

Discussions similaires

  1. probleme avec la fonction atoi()
    Par abdou monta dans le forum Débuter
    Réponses: 17
    Dernier message: 14/12/2013, 15h17
  2. la fonction ATOI
    Par alvanoto dans le forum Débuter
    Réponses: 4
    Dernier message: 27/11/2009, 18h51
  3. Fonction atoi et pointeurs
    Par lucaordi dans le forum C
    Réponses: 16
    Dernier message: 06/03/2009, 18h46
  4. Pb avec fonction atoi
    Par cjacquel dans le forum MFC
    Réponses: 2
    Dernier message: 19/02/2008, 17h27
  5. atoi et itoa en c++
    Par mathher dans le forum C++
    Réponses: 8
    Dernier message: 24/04/2006, 20h20

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo