PROWAREtech

articles » current » assembly » x64 » procedures » atoi

x64 Assembly: atoi Procedure

ASCII string to int - Convert strings to numbers using x86-64 (64-bit) assembly.

This code has been compiled and tested on Visual Studio 2022.

Use the atoi_asm_x64 procedure to convert a C-string to a 64-bit long long (ASCII to int).


TITLE 'extern "C" long long atoi_asm_x64(const char *sz);'

PUBLIC	atoi_asm_x64

_TEXT	SEGMENT
atoi_asm_x64	PROC

label1:

; skip white space

	mov  al, BYTE PTR [rcx]
	cmp  al, 32 ; ' '
	je   SHORT label2
	cmp  al, 9  ; '\t'
	je   SHORT label2
	cmp  al, 13 ; '\r'
	je   SHORT label2
	cmp  al, 10 ; '\n'
	jne  SHORT label3

label2:

	inc  rcx
	jmp  SHORT label1
	
label3:

	xor  rdx, rdx
	mov  dl, BYTE PTR [rcx]
	inc  rcx

	cmp  dl, 45 ; '-'
	mov  r8b, dl
	je   SHORT label4
	cmp  dl, 43 ; '+'
	jne  SHORT label5
	
label4:

	xor  rdx, rdx
	mov  cl, BYTE PTR [rcx]
	inc  rcx
	
label5:

	xor  rax, rax

	cmp  dl, 48 ; '0'
	jl   SHORT label7
	
label6:

	cmp  dl, 57 ; '9'
	jg   SHORT label7

	lea  rax, DWORD PTR [rax+rax*4]
	lea  rax, DWORD PTR [rdx+rax*2-48]

	xor  rdx, rdx
	mov  dl, BYTE PTR [rcx]
	inc  rcx
	cmp  dl, 48 ; '0'
	jge  SHORT label6

label7:

	cmp  r8b, 45 ; '-'
	jne  SHORT label8
	neg  rax
	
label8:

	ret  0

atoi_asm_x64	ENDP
_TEXT	ENDS
END

PROWAREtech

Hello there! How can I help you today?
Ask any question

PROWAREtech

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
ACCEPT REJECT