From 4273bdaeae1686bd4f411e283b7c9848046ffcae Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Sun, 12 Oct 2025 21:25:54 -0400 Subject: [PATCH] External include files --- Makefile | 1 + README.md | 2 +- functions.asm | 39 +++++++++++++++++++++++++++++++++++++++ helloworld.asm | 46 +++++++++++----------------------------------- 4 files changed, 52 insertions(+), 36 deletions(-) create mode 100644 functions.asm diff --git a/Makefile b/Makefile index b58f7a6..1d8d4dc 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ .PHONY: all clean SRC := $(wildcard *.asm) +SRC := $(filter-out functions.asm, $(SRC)) OBJDIR := build OBJS := $(SRC:%.asm=$(OBJDIR)/%.o) BINS := $(SRC:%.asm=$(OBJDIR)/%) diff --git a/README.md b/README.md index bb5df86..c1d1ed9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # asm -Build x86_64 assembly examples from [asmtutor.com](https://asmtutor.com) +x86 assembly exercises usingĀ [asmtutor.com](https://asmtutor.com) diff --git a/functions.asm b/functions.asm new file mode 100644 index 0000000..3f85c2e --- /dev/null +++ b/functions.asm @@ -0,0 +1,39 @@ +SYS_EXIT equ 1 +SYS_WRITE equ 4 +STDOUT equ 1 + +strlen: + push ebx + mov ebx, eax +nextchar: + cmp byte[eax], 0 + jz finished + inc eax + jmp nextchar +finished: + sub eax, ebx + pop ebx + ret + +print: + push ebx + push ecx + push edx + + mov ebx, eax + call strlen + mov edx, eax + mov ecx, ebx + mov ebx, STDOUT + mov eax, SYS_WRITE + int 0x80 + + pop edx + pop ecx + pop ebx + ret + +quit: + mov ebx, 0 + mov eax, SYS_EXIT + int 0x80 diff --git a/helloworld.asm b/helloworld.asm index 67c8edd..4fbd93b 100644 --- a/helloworld.asm +++ b/helloworld.asm @@ -1,39 +1,15 @@ -; Hello World Program - asmtutor.com -; Compile with: nasm -f elf helloworld.asm -; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 helloworld.o -o helloworld -; Run with: ./helloworld +%include 'functions.asm' -SECTION .data -msg db 'Hello, brave new world!', 0Ah ; assign msg variable with your message string +section .data +msg1 db "Hello, world!", 0Ah +msg2 db "Display a line of text", 0Ah -SECTION .text -global _start +section .text +global _start _start: - mov eax, msg - call strlen - - mov edx, eax ; our function leaves the result in EAX - mov ecx, msg - mov ebx, 1 ; STDOUT - mov eax, 4 ; SYS_WRITE - int 80h - - mov ebx, 0 ; return 0 - mov eax, 1 ; SYS_EXIT - int 80h - -strlen: - push ebx ; preserve EBX while we use in this function - mov ebx, eax ; move the address in EAX into EBX - -nextchar: - cmp byte [eax], 0 ; compare the byte pointed to by EAX at this address against zero (Zero is an end of string delimiter) - jz finished ; jump (if the zero flagged has been set) to the point in the code labeled 'finished' - inc eax ; increment the address in EAX by one byte (if the zero flagged has NOT been set) - jmp nextchar ; jump to the point in the code labeled 'nextchar' - -finished: - sub eax, ebx - pop ebx - ret + mov eax, msg1 + call print + mov eax, msg2 + call print + call quit