From fb071d2000298f02236a20e86cfb348f712c068e Mon Sep 17 00:00:00 2001 From: Joe Tretter Date: Fri, 29 Aug 2025 17:44:03 -0500 Subject: [PATCH] add counter2 program that is more efficient --- counter/compile | 3 +- counter2/compile | 4 ++ counter2/counter.asm | 100 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100755 counter2/compile create mode 100644 counter2/counter.asm diff --git a/counter/compile b/counter/compile index f572889..1f1d22b 100755 --- a/counter/compile +++ b/counter/compile @@ -1,3 +1,4 @@ - nasm -f elf64 counter.asm -o counter.o + #nasm -f elf64 counter.asm -o counter.o + nasm -f elf64 -g -F dwarf counter.asm -o counter.o ld counter.o -o counter ./counter diff --git a/counter2/compile b/counter2/compile new file mode 100755 index 0000000..1f1d22b --- /dev/null +++ b/counter2/compile @@ -0,0 +1,4 @@ + #nasm -f elf64 counter.asm -o counter.o + nasm -f elf64 -g -F dwarf counter.asm -o counter.o + ld counter.o -o counter + ./counter diff --git a/counter2/counter.asm b/counter2/counter.asm new file mode 100644 index 0000000..1523b02 --- /dev/null +++ b/counter2/counter.asm @@ -0,0 +1,100 @@ +; nasm -f elf64 count.asm -o count.o +; ld count.o -o count +; ./count + +BITS 64 +SECTION .data + prefix db "Line ", 0 ; constant prefix string + newline db 10, 0 ; newline character "\n" + +SECTION .bss + numbuf resb 16 ; buffer to hold number string + +SECTION .text + global _start + +;---------------------------------------- +; _start: Entry point +;---------------------------------------- +_start: + mov rcx, 100 ; use rcx / loop as an easy counter. + +print_loop: + ; Print "Line " + mov rsi, prefix + call print_str + + ; Convert counter to string + mov rax, 101 ; as the rcx couns backwards we have to subtract it from 100 to have ascending order + sub rax, rcx + mov rdi, numbuf + call int_to_str + + ; Print number + mov rsi, numbuf + call print_str + + ; Print newline + mov rsi, newline + call print_str + + loop print_loop ; will automatically decrement rcx and stop looping if it's 0 + + ; Exit syscall + mov rax, 60 + xor rdi, rdi + syscall + +;---------------------------------------- +; print_str: rsi = null-terminated string +;---------------------------------------- +print_str: + mov r8, rcx + push rsi + mov rdi, rsi + xor rcx, rcx +.find_len: + cmp byte [rdi+rcx], 0 + je .got_len + inc rcx + jmp .find_len +.got_len: + mov rax, 1 ; sys_write + mov rdi, 1 ; fd = stdout + mov rdx, rcx ; length + mov rsi, rsi ; buffer + syscall + pop rsi + mov rcx, r8 + ret + +;---------------------------------------- +; int_to_str: rax = number, rdi = buffer +; Converts integer in RAX to ASCII string +; (null-terminated, left-justified) +;---------------------------------------- +int_to_str: + mov r8, rcx + mov rcx, 0 + mov rbx, 10 + +.int_div_loop: + xor rdx, rdx + div rbx ; quotient in rax, remainder in rdx + push rdx + inc rcx + test rax, rax + jnz .int_div_loop + + ; Write digits to buffer + mov rsi, rdi +.write_digits: + pop rdx + add dl, '0' + mov [rsi], dl + inc rsi + loop .write_digits + + mov byte [rsi], 0 ; null terminator + mov rcx, r8 + ret