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