From 4844f4be1bf8b204c043c605d02157dbe4f746ec Mon Sep 17 00:00:00 2001 From: lohhiiccc Date: Fri, 29 May 2026 01:56:08 +0200 Subject: [PATCH] WIP --- Makefile | 20 ++++++-- src/Colleen.s | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 src/Colleen.s diff --git a/Makefile b/Makefile index 7b3a721..e079547 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,12 @@ COLLEEN = Colleen GRACE = Grace SULLY = Sully +COLLEEN_ASM_SRCS = $(SRC_DIR)/Colleen.s +COLLEEN_ASM = Colleen_asm +NASM = nasm +NASMFLAGS = -f elf64 +LD = ld + .DEFAULT_GOAL := all MAKEFLAGS += --no-print-directory @@ -26,8 +32,10 @@ GRACE_DEPS = $(GRACE_OBJS:.o=.d) SULLY_OBJS = $(SULLY_SRCS:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) SULLY_DEPS = $(SULLY_OBJS:.o=.d) +COLLEEN_ASM_OBJS = $(COLLEEN_ASM_SRCS:$(SRC_DIR)/%.s=$(OBJ_DIR)/%.asm.o) + .PHONY: all -all: $(COLLEEN) $(GRACE) $(SULLY) +all: $(COLLEEN) $(GRACE) $(SULLY) $(COLLEEN_ASM) $(GRACE): $(GRACE_OBJS) $(CC) $(LDFLAGS) -o $@ $^ @@ -38,10 +46,17 @@ $(COLLEEN): $(COLLEEN_OBJS) $(SULLY): $(SULLY_OBJS) $(CC) $(LDFLAGS) -o $@ $^ +$(COLLEEN_ASM): $(COLLEEN_ASM_OBJS) + $(LD) -o $@ $^ + $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c @mkdir -p $(dir $@) $(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -c $< -o $@ +$(OBJ_DIR)/%.asm.o: $(SRC_DIR)/%.s + @mkdir -p $(dir $@) + $(NASM) $(NASMFLAGS) $< -o $@ + -include $(COLLEEN_DEPS) -include $(GRACE_DEPS) -include $(SULLY_DEPS) @@ -52,9 +67,8 @@ clean: .PHONY: fclean fclean: clean - $(RM) $(COLLEEN) $(GRACE) $(SULLY) + $(RM) $(COLLEEN) $(GRACE) $(SULLY) $(COLLEEN_ASM) .PHONY: re re: fclean $(MAKE) all - diff --git a/src/Colleen.s b/src/Colleen.s new file mode 100644 index 0000000..80ef142 --- /dev/null +++ b/src/Colleen.s @@ -0,0 +1,138 @@ +global _start + +section .rodata +src: db 103, 108, 111, 98, 97, 108, 32, 95, 115, 116, 97, 114, 116, 10, 10, 115, 101, 99, 116, 105, 111, 110, 32, 46, 114, 111, 100, 97, 116, 97, 10, 115, 114, 99, 58, 32, 100, 98, 32, 34, "~", 34, 10, 115, 114, 99, 95, 108, 101, 110, 58, 32, 101, 113, 117, 32, 36, 32, 45, 32, 115, 114, 99, 10, 10, 115, 101, 99, 116, 105, 111, 110, 32, 46, 116, 101, 120, 116, 10, 10, 59, 112, 114, 105, 110, 116, 95, 110, 117, 109, 98, 101, 114, 58, 10, 9, 10, 10, 95, 115, 116, 97, 114, 116, 58, 10, 9, 112, 117, 115, 104, 32, 114, 49, 52, 59, 32, 105, 110, 116, 32, 105, 10, 9, 112, 117, 115, 104, 32, 114, 49, 51, 10, 9, 120, 111, 114, 32, 114, 49, 52, 44, 32, 114, 49, 52, 32, 59, 32, 105, 32, 61, 32, 48, 10, 10, 9, 46, 108, 111, 111, 112, 58, 10, 9, 59, 32, 105, 102, 32, 105, 32, 62, 32, 115, 114, 99, 95, 108, 101, 110, 10, 9, 99, 109, 112, 32, 114, 49, 52, 44, 32, 115, 114, 99, 95, 108, 101, 110, 10, 9, 106, 103, 32, 46, 100, 111, 110, 101, 32, 59, 32, 98, 114, 101, 97, 107, 59, 10, 10, 9, 59, 114, 49, 51, 32, 61, 32, 97, 100, 100, 114, 32, 111, 102, 32, 115, 114, 99, 32, 43, 32, 105, 10, 9, 108, 101, 97, 32, 114, 49, 51, 44, 32, 91, 114, 101, 108, 32, 115, 114, 99, 93, 10, 9, 97, 100, 100, 32, 114, 49, 51, 44, 32, 114, 49, 52, 10, 9, 10, 9, 99, 109, 112, 32, 98, 121, 116, 101, 32, 91, 114, 49, 51, 93, 44, 32, 49, 50, 54, 10, 9, 106, 101, 32, 46, 101, 115, 99, 97, 112, 101, 10, 10, 10, 9, 59, 32, 119, 114, 105, 116, 101, 32, 49, 44, 32, 114, 49, 51, 44, 32, 49, 10, 9, 109, 111, 118, 32, 101, 97, 120, 44, 32, 49, 10, 9, 109, 111, 118, 32, 114, 100, 105, 44, 32, 49, 10, 9, 109, 111, 118, 32, 114, 115, 105, 44, 32, 114, 49, 51, 10, 9, 109, 111, 118, 32, 114, 100, 120, 44, 32, 49, 10, 9, 115, 121, 115, 99, 97, 108, 108, 10, 9, 106, 109, 112, 32, 46, 101, 115, 99, 97, 112, 101, 95, 100, 111, 110, 101, 10, 10, 9, 46, 101, 115, 99, 97, 112, 101, 58, 10, 9, 112, 117, 115, 104, 32, 114, 49, 49, 10, 9, 120, 111, 114, 32, 114, 49, 49, 44, 32, 114, 49, 49, 32, 59, 32, 106, 32, 61, 32, 48, 10, 9, 46, 101, 95, 108, 111, 111, 112, 58, 10, 9, 99, 109, 112, 32, 114, 49, 49, 44, 32, 115, 114, 99, 95, 108, 101, 110, 10, 9, 106, 103, 32, 46, 101, 115, 99, 97, 112, 101, 95, 100, 111, 110, 101, 10, 10, 9, 109, 111, 118, 32, 101, 97, 120, 44, 32, 49, 10, 9, 109, 111, 118, 32, 114, 100, 105, 44, 32, 49, 10, 9, 109, 111, 118, 32, 114, 115, 105, 44, 32, 34, 44, 32, 34, 10, 9, 109, 111, 118, 32, 114, 100, 120, 44, 32, 50, 10, 9, 115, 121, 115, 99, 97, 108, 108, 10, 10, 9, 108, 101, 97, 32, 114, 97, 120, 44, 32, 91, 114, 101, 108, 32, 115, 114, 99, 93, 10, 9, 97, 100, 100, 32, 114, 97, 120, 44, 32, 114, 49, 49, 10, 9, 109, 111, 118, 32, 97, 108, 44, 32, 91, 114, 97, 120, 93, 10, 9, 59, 99, 97, 108, 108, 32, 112, 114, 105, 110, 116, 95, 110, 117, 109, 98, 101, 114, 10, 10, 9, 105, 110, 99, 32, 114, 49, 49, 10, 9, 106, 109, 112, 32, 46, 101, 95, 108, 111, 111, 112, 10, 9, 46, 101, 115, 99, 97, 112, 101, 95, 100, 111, 110, 101, 58, 10, 9, 112, 111, 112, 32, 114, 49, 49, 10, 10, 9, 105, 110, 99, 32, 114, 49, 52, 32, 59, 32, 43, 43, 105, 10, 9, 106, 109, 112, 32, 46, 108, 111, 111, 112, 10, 10, 9, 46, 100, 111, 110, 101, 58, 10, 9, 109, 111, 118, 32, 101, 97, 120, 44, 32, 54, 48, 32, 9, 9, 9, 9, 9, 59, 32, 83, 89, 83, 95, 101, 120, 105, 116, 10, 9, 120, 111, 114, 32, 101, 100, 105, 44, 32, 101, 100, 105, 9, 9, 9, 9, 9, 59, 32, 99, 111, 100, 101, 32, 61, 32, 48, 10, 9, 115, 121, 115, 99, 97, 108, 108, 10 +src_len: equ $ - src +sep: db ", " +zero_str: db "0" + +section .text + +;void putnumber(uint8_t n) +;{ +; char buffer[3]; +; int len; +; int i; +; +; if (n == 0) +; { +; write(1, "0", 1); +; return ; +; } +; len = 0; +; while (n > 0) +; { +; buffer[len] = (n % 10); + ;buffer[len] += '0' +; n /= 10; +; len++; +; } +; i = len - 1; +; write(1, &buffer[i], len); +print_number: + push rbp + mov rbp, rsp + sub rsp, 16 + ; al = n (uint8_t) + ; [rbp - 1] = buffer[0] + ; [rbp - 2] = buffer[1] + ; [rbp - 3] = buffer[2] + ; [rbp - 4] = len (int) + ; [rbp - 8] = i (int) + + cmp al, 0 + jne .e_write_0 + mov eax, 1 + mov rdi, 1 + lea rsi, [rel zero_str] + mov rdx, 1 + syscall + jmp .end + .e_write_0: + + mov dword [rbp - 4], 0 ; len = 0 + .loop: ; while n > 0 + cmp al, 0 + jle .done + + ;lea rax, [rbp - 1] + ;add rax, [rbp - 4] + ; + + + movzx rax, al ; rax <- n + xor rdx, rdx ; rdx = 0 + mov rcx, 10 ; rxc = 10 + div rcx ; n / 10 + + inc dword [rbp - 4] ; len ++ + jmp .loop + + .done: + ; write 1 buffer len + mov eax, 1 + mov rdi, 1 + lea rsi, [rbp - 1] + mov edx, [rbp - 4] + syscall + + .end: + mov rsp, rbp + pop rbp + ret + +_start: + push r14; int i + push r13 + xor r14, r14 ; i = 0 + + .loop: + ; if i > src_len + cmp r14, src_len + jg .done ; break; + + ;r13 = addr of src + i + lea r13, [rel src] + add r13, r14 + + cmp byte [r13], 126 + je .escape + + + ; write 1, r13, 1 + mov eax, 1 + mov rdi, 1 + mov rsi, r13 + mov rdx, 1 + syscall + jmp .escape_done + + .escape: + push r11 + xor r11, r11 ; j = 0 + .e_loop: + cmp r11, src_len + jg .escape_done + + mov eax, 1 + mov rdi, 1 + lea rsi, [rel sep] + mov rdx, 2 + syscall + + lea rax, [rel src] + add rax, r11 + mov al, [rax] + call print_number + + inc r11 + jmp .e_loop + .escape_done: + + inc r14 ; ++i + jmp .loop + + .done: + mov eax, 60 ; SYS_exit + xor edi, edi ; code = 0 + syscall