Skip to content

Instantly share code, notes, and snippets.

@tnewman
Last active August 24, 2025 10:38
Show Gist options
  • Select an option

  • Save tnewman/63b64284196301c4569f750a08ef52b2 to your computer and use it in GitHub Desktop.

Select an option

Save tnewman/63b64284196301c4569f750a08ef52b2 to your computer and use it in GitHub Desktop.
atoi in Assembly - Convert a number string to a 64 bit integer.
global atoi
section .text
atoi:
mov rax, 0 ; Set initial total to 0
convert:
movzx rsi, byte [rdi] ; Get the current character
test rsi, rsi ; Check for \0
je done
cmp rsi, 48 ; Anything less than 0 is invalid
jl error
cmp rsi, 57 ; Anything greater than 9 is invalid
jg error
sub rsi, 48 ; Convert from ASCII to decimal
imul rax, 10 ; Multiply total by 10
add rax, rsi ; Add current digit to total
inc rdi ; Get the address of the next character
jmp convert
error:
mov rax, -1 ; Return -1 on error
done:
ret ; Return total or error code
@RamyMakram

Copy link
Copy Markdown

thank you very much

@yoss5u

yoss5u commented Apr 25, 2020

Copy link
Copy Markdown

This works for numbers bigger than 9?

@B1rby

B1rby commented Nov 2, 2021

Copy link
Copy Markdown

This works for numbers bigger than 9?

No. As the code is stated ; Anything greater than 9 is invalid

@PhilosophicalNinja

PhilosophicalNinja commented Oct 4, 2023

Copy link
Copy Markdown

This works for numbers bigger than 9?

No. As the code is stated ; Anything greater than 9 is invalid

It works for numbers bigger than 9. The point of the statement was that no individual character can be greater than 9.
(It's an old thread, but this is for anyone else who stumbles here)

@B1rby

B1rby commented Oct 4, 2023

Copy link
Copy Markdown

This works for numbers bigger than 9?

No. As the code is stated ; Anything greater than 9 is invalid

I did one that can do it for numbers greater than 9 so you can check it out. It has been such a long time that I commented here so weird to see that you actually replied lol

@marcusRain

Copy link
Copy Markdown

This works for numbers bigger than 9?

No. As the code is stated ; Anything greater than 9 is invalid

I did one that can do it for numbers greater than 9 so you can check it out. It has been such a long time that I commented here so weird to see that you actually replied lol

this code does work for strings that represent numbers greater than 9. Line 16 means any ASCII character greater than 57 (i.e., 9), is invalid--b/c it doesn't represent a number.

@tnewman

tnewman commented Dec 29, 2023

Copy link
Copy Markdown
Author

This works for numbers bigger than 9?

No. As the code is stated ; Anything greater than 9 is invalid

I did one that can do it for numbers greater than 9 so you can check it out. It has been such a long time that I commented here so weird to see that you actually replied lol

this code does work for strings that represent numbers greater than 9. Line 16 means any ASCII character greater than 57 (i.e., 9), is invalid--b/c it doesn't represent a number.

This is correct. These lines are for error handling in case the caller supplied a string containing characters other than the digits 0-9. It does not mean only strings representing 0-9 are supported.

This error handling code is for the current character in the string, so it will reject any characters that are not digits.

    cmp rsi, 48             ; Anything less than 0 is invalid
    jl error
    
    cmp rsi, 57             ; Anything greater than 9 is invalid
    jg error

@Zeykouille

Copy link
Copy Markdown

Hey, how to act when we enter a negative number to convert ? Don't we need to compare rsi, 45 ? Otherwise we just get -1 returned, not what we want

@tnewman

tnewman commented Jan 2, 2024

Copy link
Copy Markdown
Author

Negative numbers are not supported in this snippet. You'd have to check for a leading - and negate the final result if negative.

@Zeykouille

Copy link
Copy Markdown

Negative numbers are not supported in this snippet. You'd have to check for a leading - and negate the final result if negative.

That's what I did, thx

@tetram2674562

Copy link
Copy Markdown

Nice code ;)

@a2kvarnstrom

Copy link
Copy Markdown

quick (somewhat unrelated) newbie question, if the code starts at the atoi subroutine, how does the rest of the code get called?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment