convert.asm: Avoid stack usage for reversing string - use second buffer instead

This commit is contained in:
2025-07-10 01:37:26 +02:00
parent d919a0be05
commit 2d1a4b9d56

View File

@@ -2,6 +2,7 @@
section .bss
cnvtBuff resb 21
cnvtBuffRev resb 21
section .text
global dec2str
global udec2str
@@ -18,9 +19,10 @@ section .text
; rsi* Points to cnvtBuff for writing characters
; r8* Dividor for div
dec2str:
lea rsi, [rel cnvtBuff]
lea rsi, [rel cnvtBuffRev]
test rdi, rdi
jnz .notZero
lea rsi, [rel cnvtBuff]
mov byte [rsi], '0'
mov byte [rsi+1], EOS
jmp .quit
@@ -38,20 +40,26 @@ dec2str:
mov r8, 10
div r8
add rdx, '0'
push rdx
mov [rsi], dl
inc rsi
inc rcx
test rax, rax
jnz .convert
test dil, dil
jz .makeString
push byte '-'
mov [rsi], byte '-'
inc rcx
inc rsi
.makeString:
pop rdx
mov byte [rsi], dl
inc rsi
loop .makeString
mov byte [rsi], EOS
lea rdi, [rel cnvtBuff]
dec rsi
.makeStringLoop:
mov al, [rsi]
mov [rdi], al
inc rdi
dec rsi
loop .makeStringLoop
mov byte [rdi], EOS
.quit:
lea rax, [rel cnvtBuff]
@@ -68,9 +76,10 @@ dec2str:
; rsi* Points to cnvtBuff for writing characters
; r8* Dividor for div
udec2str:
lea rsi, [rel cnvtBuff]
lea rsi, [rel cnvtBuffRev]
test rdi, rdi
jnz .notZero
lea rsi, [rel cnvtBuff]
mov byte [rsi], '0'
mov byte [rsi+1], EOS
jmp .quit
@@ -83,16 +92,20 @@ udec2str:
mov r8, 10
div r8
add rdx, '0'
push rdx
mov [rsi], dl
inc rsi
inc rcx
test rax, rax
jnz .convert
.makeString:
pop rdx
mov byte [rsi], dl
inc rsi
loop .makeString
mov byte [rsi], EOS
lea rdi, [rel cnvtBuff]
dec rsi
.makeStringLoop:
mov al, [rsi]
mov [rdi], al
inc rdi
dec rsi
loop .makeStringLoop
mov byte [rdi], EOS
.quit:
lea rax, [rel cnvtBuff]
@@ -112,9 +125,10 @@ udec2str:
; r11* Amount to add to number (for ASCII conversion, lowercase or uppercase)
hex2str:
mov r9, rsi
lea rsi, [rel cnvtBuff]
lea rsi, [rel cnvtBuffRev]
test rdi, rdi
jnz .notZero
lea rsi, [rel cnvtBuff]
mov word [rsi], '0x'
mov byte [rsi + 2], '0'
mov byte [rsi + 3], EOS
@@ -141,19 +155,23 @@ hex2str:
.num:
add rdx, '0'
.push:
push rdx
mov [rsi], dl
inc rsi
inc rcx
test rax, rax
jnz .convert
mov word [rsi], '0x'
add rsi, 2
mov word [rsi], 'x0'
inc rsi
add rcx, 2
lea rdi, [rel cnvtBuff]
.makeString:
pop rdx
mov byte [rsi], dl
inc rsi
mov al, [rsi]
mov [rdi], al
inc rdi
dec rsi
loop .makeString
mov byte [rsi], EOS
mov byte [rdi], EOS
.quit:
lea rax, [rel cnvtBuff]