Adds hex2str

This commit is contained in:
2025-07-09 11:16:04 +02:00
parent f8df1242fc
commit af34e3208c
2 changed files with 105 additions and 8 deletions

View File

@@ -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

View File

@@ -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()
;---