From 9153c5c0700cc9c04ea4095a8b424e0beb3eac05 Mon Sep 17 00:00:00 2001 From: old Date: Sun, 12 Oct 2025 14:32:54 +0100 Subject: [PATCH] feat: add typewriter clock tutorial in x86 assembly --- basic/typewriter_clock/Makefile | 8 +++ basic/typewriter_clock/README.md | 13 ++++ basic/typewriter_clock/typewriter_clock.asm | 71 +++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 basic/typewriter_clock/Makefile create mode 100644 basic/typewriter_clock/README.md create mode 100644 basic/typewriter_clock/typewriter_clock.asm diff --git a/basic/typewriter_clock/Makefile b/basic/typewriter_clock/Makefile new file mode 100644 index 0000000..d4865f0 --- /dev/null +++ b/basic/typewriter_clock/Makefile @@ -0,0 +1,8 @@ +all: typewriter_clock + +typewriter_clock: typewriter_clock.asm + nasm -f elf32 typewriter_clock.asm -o typewriter_clock.o + ld -m elf_i386 typewriter_clock.o -o typewriter_clock + +clean: + rm -f *.o typewriter_clock diff --git a/basic/typewriter_clock/README.md b/basic/typewriter_clock/README.md new file mode 100644 index 0000000..8fef4e9 --- /dev/null +++ b/basic/typewriter_clock/README.md @@ -0,0 +1,13 @@ +# Typewriter Clock - x86 Assembly Tutorial + +## Description +This is a simple x86 Assembly program that prints the current time in the terminal with a typewriter effect. + +## Requirements +- Linux (tested on Debian/Ubuntu) +- NASM assembler + +## Build and Run +```bash +make +./typewriter_clock diff --git a/basic/typewriter_clock/typewriter_clock.asm b/basic/typewriter_clock/typewriter_clock.asm new file mode 100644 index 0000000..dc5c172 --- /dev/null +++ b/basic/typewriter_clock/typewriter_clock.asm @@ -0,0 +1,71 @@ +; typewriter_clock.asm - x86 Assembly (Linux ELF32) +; Typewriter clock: prints current time in terminal +; Compile: make +; Run: ./typewriter_clock + +section .data + msg db "00:00:00", 0 ; placeholder for HH:MM:SS + len equ $ - msg + newline db 0x0A, 0 ; newline + +section .bss + timebuf resb 9 ; HH:MM:SS\0 + +section .text +global _start + +_start: +.loop: + ; get current time from system + mov eax, 13 ; sys_time + xor ebx, ebx ; time_t *tloc = NULL + int 0x80 + + ; simple conversion to HH:MM:SS (simplified) + mov ecx, 3600 ; seconds per hour + xor edx, edx + div ecx ; eax/3600 -> quotient=hours, remainder=minutes+seconds + add al, '0' + mov [timebuf], al + + ; minutes + mov eax, edx + mov ecx, 60 + xor edx, edx + div ecx + add al, '0' + mov [timebuf+3], al + + ; seconds + mov al, dl + add al, '0' + mov [timebuf+6], al + + ; print with typewriter effect + mov esi, timebuf +.print_loop: + lodsb + cmp al, 0 + je .done + mov eax, 4 + mov ebx, 1 + mov ecx, esi + dec ecx + mov edx, 1 + int 0x80 + + ; small delay + mov ecx, 50000 +.delay: + loop .delay + jmp .print_loop + +.done: + ; new line + mov eax, 4 + mov ebx, 1 + mov ecx, newline + mov edx, 1 + int 0x80 + + jmp .loop