itoa
This commit is contained in:
29
input/core/itoa.asm
Normal file
29
input/core/itoa.asm
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
section .bss
|
||||||
|
buffer resb 20
|
||||||
|
|
||||||
|
section .text
|
||||||
|
global itoa
|
||||||
|
|
||||||
|
itoa:
|
||||||
|
push rbx
|
||||||
|
push rsi
|
||||||
|
push rdx
|
||||||
|
mov rax, rdi
|
||||||
|
mov rsi, buffer + 19
|
||||||
|
mov byte [rsi], 0
|
||||||
|
dec rsi
|
||||||
|
.loop:
|
||||||
|
xor rdx, rdx
|
||||||
|
mov rbx, 10
|
||||||
|
div rbx
|
||||||
|
add dl, '0'
|
||||||
|
mov [rsi], dl
|
||||||
|
dec rsi
|
||||||
|
test rax, rax
|
||||||
|
jnz .loop
|
||||||
|
inc rsi
|
||||||
|
mov rax, rsi
|
||||||
|
pop rdx
|
||||||
|
pop rsi
|
||||||
|
pop rbx
|
||||||
|
ret
|
||||||
@@ -2,14 +2,12 @@ let SYS_WRITE = 1;
|
|||||||
let STD_OUT = 1;
|
let STD_OUT = 1;
|
||||||
let STD_ERR = 2;
|
let STD_ERR = 2;
|
||||||
|
|
||||||
extern func print_int(value: int64);
|
|
||||||
|
|
||||||
func print(msg: String) {
|
func print(msg: String) {
|
||||||
syscall(SYS_WRITE, STD_OUT, msg, str_len(msg));
|
syscall(SYS_WRITE, STD_OUT, msg, str_len(msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
func print(value: int64) {
|
func print(value: int64) {
|
||||||
print_int(value);
|
print(itoa(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
func print(value: bool) {
|
func print(value: bool) {
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
section .bss
|
|
||||||
buffer resb 20 ; Reserve 20 bytes for number string
|
|
||||||
|
|
||||||
section .text
|
|
||||||
global print_int ; Make function callable from outside
|
|
||||||
|
|
||||||
print_int:
|
|
||||||
push rbx ; Save rbx (callee-saved register)
|
|
||||||
push rsi ; Save rsi (callee-saved register)
|
|
||||||
push rdx ; Save rdx (callee-saved register)
|
|
||||||
|
|
||||||
mov rax, rdi ; Move input number to rax
|
|
||||||
mov rsi, buffer + 19 ; Point to the last byte in buffer
|
|
||||||
mov byte [rsi], 0 ; Null terminator (not necessary for sys_write)
|
|
||||||
dec rsi ; Move back for digits
|
|
||||||
|
|
||||||
.loop:
|
|
||||||
xor rdx, rdx ; Clear remainder
|
|
||||||
mov rbx, 10 ; Divisor
|
|
||||||
div rbx ; RAX / 10 -> Quotient in RAX, remainder in RDX
|
|
||||||
|
|
||||||
add dl, '0' ; Convert remainder to ASCII
|
|
||||||
mov [rsi], dl ; Store character in buffer
|
|
||||||
dec rsi ; Move buffer pointer back
|
|
||||||
|
|
||||||
test rax, rax ; Check if quotient is 0
|
|
||||||
jnz .loop ; Continue if not 0
|
|
||||||
|
|
||||||
inc rsi ; Adjust pointer to first digit
|
|
||||||
|
|
||||||
; Print using sys_write
|
|
||||||
mov rax, 1 ; syscall: sys_write
|
|
||||||
mov rdi, 1 ; file descriptor: stdout
|
|
||||||
mov rdx, buffer + 20 ; End of buffer
|
|
||||||
sub rdx, rsi ; Compute actual length
|
|
||||||
syscall
|
|
||||||
|
|
||||||
; Restore registers and return
|
|
||||||
pop rdx
|
|
||||||
pop rsi
|
|
||||||
pop rbx
|
|
||||||
ret
|
|
||||||
@@ -1 +1,2 @@
|
|||||||
extern func str_len(msg: String): int64;
|
extern func str_len(msg: String): int64;
|
||||||
|
extern func itoa(value: int64): String;
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import "core";
|
import "core";
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
println(58963475689457686745867567656769845676984567698475698674589675);
|
println(69);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,6 @@
|
|||||||
nasm -g -felf64 out.asm -o out.o
|
nasm -g -felf64 out.asm -o out.o
|
||||||
nasm -g -felf64 ../input/core/str_len.asm -o str_len.o
|
nasm -g -felf64 ../input/core/str_len.asm -o str_len.o
|
||||||
nasm -g -felf64 ../input/core/arr_size.asm -o arr_size.o
|
nasm -g -felf64 ../input/core/arr_size.asm -o arr_size.o
|
||||||
nasm -g -felf64 ../input/core/print_int.asm -o print_int.o
|
nasm -g -felf64 ../input/core/itoa.asm -o itoa.o
|
||||||
|
|
||||||
ld -o out str_len.o arr_size.o print_int.o out.o
|
ld -o out str_len.o arr_size.o itoa.o out.o
|
||||||
|
|||||||
Reference in New Issue
Block a user