Last active
August 29, 2015 14:18
-
-
Save cab404/7abbdcfaa720808a9f49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; cab404: int to str, with negative numbers and stuff | |
global _start | |
section .data | |
test_data: dq -192734, 23400, -9223372036854775808, 9223372036854775807 | |
test_len: equ ($-test_data)/8 | |
section .bss | |
buf: resb 50 ; buffer, so we can print what we got. | |
buf_len: equ $-buf | |
section .text | |
_start: | |
mov rcx, test_len ; loading test data | |
mov rsi, test_data ; and its length | |
main_calcloop: | |
cld ; cleaning direction flag before loading | |
lodsq ; loading next test number into rax | |
push rsi ; preserving registers before call | |
push rcx | |
mov rdi, buf ; moving buffer to rbx | |
call int_to_str ; calling convertion | |
call printbuf ; printing buffer | |
call ln ; cleaning buffer and printing LF | |
pop rcx ; regaining registers | |
pop rsi | |
loop main_calcloop | |
call exit | |
clbuf: ; cleaning buffer | |
cld | |
mov rdi, buf | |
mov rcx, buf_len | |
xor rax, rax | |
rep stosb | |
ret | |
ln: ; cleaning buffer and writing new line | |
call clbuf | |
mov rsi, buf | |
mov [rsi], byte 10 | |
call printbuf | |
ret | |
printbuf: ; printing buffer | |
mov rax, 4 ; write command for syscall | |
mov rbx, 1 ; cout | |
mov rcx, buf ; string | |
mov rdx, buf_len ; length | |
int 80h ; interrupt 80 hex, call kernel | |
ret | |
exit: ; exits program with 0 | |
mov ebx, 0 ; exit code, 0=normal | |
mov eax, 1 ; exit command to kernel | |
int 0x80 ; interrupt 80 hex, call kernel | |
ret | |
; rax - number | |
; rdi - target string buffer | |
int_to_str: | |
mov cl, 0 ; using rcx as sign store, defaulting to 0 (+) | |
test rax, rax | |
jns int_to_str_sign_test ; checking if rax is negative | |
not rax ; converting from negative to positive | |
inc rax ; and adding one because of offset | |
mov rcx, '-' ; setting sign | |
int_to_str_sign_test: | |
push 0 ; analog of null terminator, indicates end of stack | |
mov rbx, 10 ; we're doing this at base 10 | |
int_to_str_loop: | |
xor rdx, rdx ; nulling rdx , so no garbage will bother us | |
idiv rbx ; dividing rax by 10, now rdx has remainder | |
add dl, '0' ; converting remainder to ASCII | |
push rdx ; pushing digit converted to character | |
test rax, rax ; checking if divident is zero | |
jnz int_to_str_loop | |
test rcx, rcx ; checking sign, | |
jz int_to_str_write_loop ; and if it is not zero, | |
push rcx ; then pushing it. | |
int_to_str_write_loop: | |
pop rax ; popping characters | |
test rax, rax ; until zero is returned | |
jz int_to_str_write_loop_break | |
stosb | |
jmp int_to_str_write_loop | |
int_to_str_write_loop_break: | |
ret | |
; rax - number | |
; rdi - target string buffer | |
int_to_str_ns: | |
push 0 ; analog of null terminator, indicates end of stack | |
mov rbx, 10 ; we're doing this at base 10 | |
int_to_str_ns_loop: | |
xor rdx, rdx ; nulling rdx , so no garbage will bother us | |
idiv rbx ; dividing rax by 10, now rdx has remainder | |
add dl, '0' ; converting remainder to ASCII | |
push rdx ; pushing digit converted to character | |
test rax, rax ; checking if divident is zero | |
jnz int_to_str_ns_loop | |
int_to_str_ns_write_loop: | |
pop rax ; popping characters | |
test rax, rax ; until zero is returned | |
jz int_to_str_ns_write_loop_break | |
stosb | |
jmp int_to_str_ns_write_loop | |
int_to_str_ns_write_loop_break: | |
ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; rax - number | |
; rdi - target string buffer | |
int_to_str: | |
mov cl, 0 ; using rcx as sign store, defaulting to 0 (+) | |
test rax, rax | |
jns int_to_str_sign_test ; checking if rax is negative | |
not rax ; converting from negative to positive | |
inc rax ; and adding one because of offset | |
mov rcx, '-' ; setting sign | |
int_to_str_sign_test: | |
push 0 ; analog of null terminator, indicates end of stack | |
mov rbx, 10 ; we're doing this at base 10 | |
int_to_str_loop: | |
xor rdx, rdx ; nulling rdx , so no garbage will bother us | |
idiv rbx ; dividing rax by 10, now rdx has remainder | |
add dl, '0' ; converting remainder to ASCII | |
push rdx ; pushing digit converted to character | |
test rax, rax ; checking if divident is zero | |
jnz int_to_str_loop | |
test rcx, rcx ; checking sign, | |
jz int_to_str_write_loop ; and if it is not zero, | |
push rcx ; then pushing it. | |
int_to_str_write_loop: | |
pop rax ; popping characters | |
test rax, rax ; until zero is returned | |
jz int_to_str_write_loop_break | |
stosb | |
jmp int_to_str_write_loop | |
int_to_str_write_loop_break: | |
ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; rax - number | |
; rdi - target string buffer | |
int_to_str_ns: | |
push 0 ; analog of null terminator, indicates end of stack | |
mov rbx, 10 ; we're doing this at base 10 | |
int_to_str_ns_loop: | |
xor rdx, rdx ; nulling rdx , so no garbage will bother us | |
idiv rbx ; dividing rax by 10, now rdx has remainder | |
add dl, '0' ; converting remainder to ASCII | |
push rdx ; pushing digit converted to character | |
test rax, rax ; checking if divident is zero | |
jnz int_to_str_ns_loop | |
int_to_str_ns_write_loop: | |
pop rax ; popping characters | |
test rax, rax ; until zero is returned | |
jz int_to_str_ns_write_loop_break | |
stosb | |
jmp int_to_str_ns_write_loop | |
int_to_str_ns_write_loop_break: | |
ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment