Skip to content

Instantly share code, notes, and snippets.

@opentokix
Created November 6, 2014 09:32
Show Gist options
  • Save opentokix/c148f7e22386d0c27ab6 to your computer and use it in GitHub Desktop.
Save opentokix/c148f7e22386d0c27ab6 to your computer and use it in GitHub Desktop.
1024
[BITS 64]
[ORG 0x0000000000200000]
%INCLUDE "bmdev.asm"
start:
mov RAX, 24
add RAX, 1000
mov RDI, tstring
call int_to_string
mov RSI, tstring
call [b_output]
ret
; -----------------------------------------------------------------------------
; int_to_string -- Convert a binary interger into an string
; IN: RAX = binary integer
; RDI = location to store string
; OUT: RDI = points to end of string
; All other registers preserved
; Min return value is 0 and max return value is 18446744073709551615 so your
; string needs to be able to store at least 21 characters (20 for the digits
; and 1 for the string terminator).
; Adapted from http://www.cs.usfca.edu/~cruse/cs210s09/rax2uint.s
int_to_string:
push rdx
push rcx
push rbx
push rax
mov rbx, 10 ; base of the decimal system
xor ecx, ecx ; number of digits generated
int_to_string_next_divide:
xor edx, edx ; RAX extended to (RDX,RAX)
div rbx ; divide by the number-base
push rdx ; save remainder on the stack
inc rcx ; and count this remainder
cmp rax, 0 ; was the quotient zero?
jne int_to_string_next_divide ; no, do another division
int_to_string_next_digit:
pop rax ; else pop recent remainder
add al, '0' ; and convert to a numeral
stosb ; store to memory-buffer
loop int_to_string_next_digit ; again for other remainders
xor al, al
stosb ; Store the null terminator at the end of the string
pop rax
pop rbx
pop rcx
pop rdx
ret
; -----------------------------------------------------------------------------
tstring: times 50 db 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment