From af34e3208c45e3a83de63a7d871edeee3268e601 Mon Sep 17 00:00:00 2001 From: Kwarde Date: Wed, 9 Jul 2025 11:16:04 +0200 Subject: [PATCH] Adds hex2str --- src/convert.asm | 77 ++++++++++++++++++++++++++++++++++++++++++++----- src/tests.asm | 36 +++++++++++++++++++++++ 2 files changed, 105 insertions(+), 8 deletions(-) diff --git a/src/convert.asm b/src/convert.asm index 6591b24..b945631 100644 --- a/src/convert.asm +++ b/src/convert.asm @@ -5,15 +5,16 @@ section .bss section .text global dec2str global udec2str + global hex2str -;----- dec2str(num) -----; +;----- dec2str(int) -----; ; Converts a signed integer to a string (decimal output) -; Return value: Pointer to string containing the converted number +; Return value: Pointer to string containing the converted integer ; Used registers: -; rax* num stored for div >> (ret) pointer to cnvtBuff[] +; rax* int 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 +; rdi* (arg) integer to convert to string >> Remembers if num is negative ; rsi* Points to cnvtBuff for writing characters ; r8* Dividor for div dec2str: @@ -56,14 +57,14 @@ dec2str: mov rax, cnvtBuff ret -;----- udec2str(num) -----; +;----- udec2str(uint) -----; ; Converts an unsigned integer to a string (decimal output) -; Return value: Pointer to string containing the converted number +; Return value: Pointer to string containing the converted integer ; Used registers: -; rax* num stored for div >> (ret) pointer to cnvtBuff[] +; rax* uint 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 +; rdi* (arg) integer to convert to string ; rsi* Points to cnvtBuff for writing characters ; r8* Dividor for div udec2str: @@ -97,3 +98,63 @@ udec2str: .quit: mov rax, cnvtBuff ret + +;----- hex2str(uint, bool uppercase) -----; +; Converts an unsigned integer to a string (hexadecimal output) +; Return value: Pointer to string containing the converted integer +; Used registers: +; rax* uint stored for div >> (ret) Pointer to cnvtBuff[] +; rdi (arg) Integer to convert +; rsi* (arg) 0=convert lowercase, (any other value)=convert uppercase >> Points to cnvtBuff for writing characters +; rdx* module as calculated by div +; rcx* Counts length of created string +; r8* Dividor for div +; r9* Store arg rsi (uppercase) +hex2str: + mov r9, rsi + mov rsi, cnvtBuff + test rdi, rdi + jnz .notZero + mov word [rsi], '0x' + mov byte [rsi + 2], '0' + mov byte [rsi + 3], EOS + jmp .quit + + .notZero: + mov rax, rdi + xor dil, dil + xor rcx, rcx + .convert: + xor rdx, rdx + mov r8, 16 + div r8 + cmp rdx, 10 + jb .num + cmp r9, 1 + jne .lower + ;.upper: + add rdx, 55 + jmp .push + .lower: + add rdx, 87 + jmp .push + .num: + add rdx, '0' + .push: + push rdx + inc rcx + test rax, rax + jnz .convert + + mov word [rsi], '0x' + add rsi, 2 + .makeString: + pop rdx + mov byte [rsi], dl + inc rsi + loop .makeString + mov byte [rsi], EOS + + .quit: + mov rax, cnvtBuff + ret diff --git a/src/tests.asm b/src/tests.asm index d386af6..fb51025 100644 --- a/src/tests.asm +++ b/src/tests.asm @@ -14,11 +14,13 @@ extern strclr ; convert.asm extern dec2str extern udec2str +extern hex2str section .rodata TEST_print equ 1 TEST_puts equ 1 TEST_dec2str equ 1 ;includes udec2str + TEST_hex2str equ 1 TEST_printf equ 1 TEST_strlen equ 1 TEST_strcpy equ 1 @@ -125,6 +127,40 @@ _start: call udec2str %endif +;--- +;--- hex2str() +;--- +%if TEST_hex2str + ; TEST 1 + xor rdi, rdi + mov rsi, 0 + call hex2str + mov rdi, -1 + mov rsi, 0 + call hex2str + + ; TEST 2 + xor rdi, rdi + mov rsi, 1 + call hex2str + mov rdi, -1 + mov rsi, 1 + call hex2str + + ; TEST 3 + mov rdi, 0x123456789ABCDEF + mov rsi, 0 + call hex2str + mov rdi, 0x123456789ABCDEF + mov rsi, 1 + call hex2str + + ; TEST 4 + mov rdi, 128 + mov rsi, 0 + call hex2str +%endif + ;--- ;--- printf() ;---