Adds int2str() and uint2str() (=>itoa)
This commit is contained in:
@@ -1 +1,99 @@
|
||||
%include "src/constants.asm"
|
||||
|
||||
section .bss
|
||||
cnvtBuff resb 21
|
||||
section .text
|
||||
global int2str
|
||||
global uint2str
|
||||
|
||||
;----- int2str(num) -----;
|
||||
; Converts a signed integer to a string
|
||||
; Return value: Pointer to string containing the converted number
|
||||
; Used registers:
|
||||
; rax* num stored for div >> (ret) pointer to cnvtBuff[]
|
||||
; rcx* Counts length of created string
|
||||
; rdx* modulo as calculated by div >> character storage for cnvtBuff
|
||||
; rdi* (arg) number to convert to string >> Remembers if num is negative
|
||||
; rsi* Points to cnvtBuff for writing characters
|
||||
; r8* Dividor for div
|
||||
int2str:
|
||||
mov rsi, cnvtBuff
|
||||
test rdi, rdi
|
||||
jnz .notZero
|
||||
mov byte [rsi], '0'
|
||||
mov byte [rsi+1], EOS
|
||||
jmp .quit
|
||||
|
||||
.notZero:
|
||||
mov rax, rdi
|
||||
xor dil, dil
|
||||
xor rcx, rcx
|
||||
test rax, rax
|
||||
jns .convert
|
||||
neg rax
|
||||
mov dil, 1
|
||||
.convert:
|
||||
xor rdx, rdx
|
||||
mov r8, 10
|
||||
div r8
|
||||
add rdx, '0'
|
||||
push rdx
|
||||
inc rcx
|
||||
test rax, rax
|
||||
jnz .convert
|
||||
test dil, dil
|
||||
jz .makeString
|
||||
push byte '-'
|
||||
inc rcx
|
||||
.makeString:
|
||||
pop rdx
|
||||
mov byte [rsi], dl
|
||||
inc rsi
|
||||
loop .makeString
|
||||
mov byte [rsi], EOS
|
||||
|
||||
.quit:
|
||||
mov rax, cnvtBuff
|
||||
ret
|
||||
|
||||
;----- uint2str(num) -----;
|
||||
; Converts an unsigned integer to a string
|
||||
; Return value: Pointer to string containing the converted number
|
||||
; Used registers:
|
||||
; rax* num stored for div >> (ret) pointer to cnvtBuff[]
|
||||
; rcx* Counts length of created string
|
||||
; rdx* modulo as calculated by div >> character storage for cnvtBuff
|
||||
; rdi* (arg) number to convert to string
|
||||
; rsi* Points to cnvtBuff for writing characters
|
||||
; r8* Dividor for div
|
||||
uint2str:
|
||||
mov rsi, cnvtBuff
|
||||
test rdi, rdi
|
||||
jnz .notZero
|
||||
mov byte [rsi], '0'
|
||||
mov byte [rsi+1], EOS
|
||||
jmp .quit
|
||||
|
||||
.notZero:
|
||||
mov rax, rdi
|
||||
xor dil, dil
|
||||
xor rcx, rcx
|
||||
.convert:
|
||||
xor rdx, rdx
|
||||
mov r8, 10
|
||||
div r8
|
||||
add rdx, '0'
|
||||
push rdx
|
||||
inc rcx
|
||||
test rax, rax
|
||||
jnz .convert
|
||||
.makeString:
|
||||
pop rdx
|
||||
mov byte [rsi], dl
|
||||
inc rsi
|
||||
loop .makeString
|
||||
mov byte [rsi], EOS
|
||||
|
||||
.quit:
|
||||
mov rax, cnvtBuff
|
||||
ret
|
||||
|
Reference in New Issue
Block a user