├── .gitignore ├── 32bit ├── add42-c │ ├── add42 │ │ ├── add42.h │ │ └── add42.asm │ ├── main.c │ ├── README.md │ └── Makefile ├── print-c │ ├── print │ │ ├── print.h │ │ └── print.asm │ ├── main.c │ ├── README.md │ └── Makefile ├── print-asm │ ├── README.md │ ├── Makefile │ └── print.asm └── add42-asm │ ├── README.md │ ├── Makefile │ └── add42.asm └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | **/build/* 2 | *.o 3 | -------------------------------------------------------------------------------- /32bit/add42-c/add42/add42.h: -------------------------------------------------------------------------------- 1 | int add42(int x); 2 | -------------------------------------------------------------------------------- /32bit/print-c/print/print.h: -------------------------------------------------------------------------------- 1 | int print(char *msg, int length); 2 | -------------------------------------------------------------------------------- /32bit/print-c/main.c: -------------------------------------------------------------------------------- 1 | #include "print/print.h" 2 | 3 | int main() { 4 | print("Hi world\n", 9); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ASM playground 2 | 3 | Simple Assembler examples. 4 | 5 | 6 | ## 32bit ASM 7 | 8 | * [32bit/](32bit/) 9 | 10 | ## 64bit ASM 11 | 12 | * Todo 13 | -------------------------------------------------------------------------------- /32bit/add42-c/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "add42/add42.h" 3 | 4 | int main() { 5 | int result; 6 | result = add42(5); // Defined in "add42/add42.(asm|h)" 7 | printf("Result: %i\n", result); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /32bit/print-asm/README.md: -------------------------------------------------------------------------------- 1 | # print-asm 2 | 3 | Print pre-defined sized length message to stdout. 4 | 5 | 6 | ## Requirements 7 | 8 | ```bash 9 | sudo apt-get install nasm make 10 | ``` 11 | 12 | ## Build 13 | ```bash 14 | # This 15 | make 16 | 17 | # Or this 18 | make build 19 | ``` 20 | 21 | ## Run 22 | ```bash 23 | $ ./build/myprint 24 | Hello World! 25 | $ echo $? 26 | 0 27 | ``` 28 | 29 | ## Clean 30 | ```bash 31 | make clean 32 | ``` 33 | 34 | 35 | ## Credits 36 | 37 | https://asmtutor.com/#lesson2 38 | -------------------------------------------------------------------------------- /32bit/print-c/README.md: -------------------------------------------------------------------------------- 1 | # print-c 2 | 3 | Defines the `print` assembler function that can be used in C programs. The assembler function is able to print a string to stdout by taking the char* and length as arguments. 4 | 5 | 6 | ## Requirements 7 | 8 | ```bash 9 | sudo apt-get install gcc-multilib nasm make 10 | ``` 11 | 12 | ## Build 13 | ```bash 14 | # This 15 | make 16 | 17 | # Or this 18 | make build 19 | ``` 20 | 21 | ## Run 22 | ```bash 23 | $ ./build/print 24 | Hi world 25 | $ echo $? 26 | 0 27 | ``` 28 | 29 | ## Clean 30 | ```bash 31 | make clean 32 | ``` 33 | 34 | 35 | ## Credits 36 | 37 | https://asmtutor.com/#lesson2 38 | -------------------------------------------------------------------------------- /32bit/add42-asm/README.md: -------------------------------------------------------------------------------- 1 | # add42-asm 2 | 3 | Assembler program that pushes `5` to the stack as an argument for `add42`, which 4 | in turn adds `42` to the stack's value (`42 + 5`) and returns this as the program's exit code: `47`. 5 | 6 | 7 | ## Requirements 8 | 9 | ```bash 10 | sudo apt-get install nasm make 11 | ``` 12 | 13 | ## Build 14 | ```bash 15 | # This 16 | make 17 | 18 | # Or this 19 | make build 20 | ``` 21 | 22 | ## Run 23 | ```bash 24 | $ ./build/add42 25 | $ echo $? 26 | 47 27 | ``` 28 | 29 | ## Clean 30 | ```bash 31 | make clean 32 | ``` 33 | 34 | 35 | ## Credits 36 | 37 | https://www.youtube.com/watch?v=nNdAygFyvjY 38 | -------------------------------------------------------------------------------- /32bit/add42-asm/Makefile: -------------------------------------------------------------------------------- 1 | # Build directory 2 | PATH_BUILD = build 3 | 4 | # Assembler files 5 | NAME_ASM = add42 6 | PATH_ASM = . 7 | SRC_ASM = $(PATH_ASM)/$(NAME_ASM).asm 8 | OBJ_ASM = $(PATH_BUILD)/$(NAME_ASM).o 9 | 10 | # Binary files 11 | NAME_BIN = add42 12 | FILE_BIN = $(PATH_BUILD)/$(NAME_BIN) 13 | 14 | # Compiler Flags (32bit) 15 | NASMFLAGS = -f elf32 16 | LDFLAGS = -m elf_i386 17 | 18 | .PHONY: build 19 | 20 | build: 21 | if ! test -d $(PATH_BUILD); then mkdir $(PATH_BUILD); fi 22 | nasm $(NASMFLAGS) $(SRC_ASM) -o $(OBJ_ASM) 23 | ld $(LDFLAGS) $(OBJ_ASM) -o $(FILE_BIN) 24 | 25 | clean: 26 | rm -f $(OBJ_ASM) 27 | rm -f $(FILE_BIN) 28 | rmdir $(PATH_BUILD) 29 | -------------------------------------------------------------------------------- /32bit/add42-c/README.md: -------------------------------------------------------------------------------- 1 | # add42-c 2 | 3 | Defines an assembler function that can be used in C programs. 4 | The assembler function simply adds `42` to whatever value has been parsed to it. 5 | The C program calls this function with an argument of `5` and prints the resulting value to stdout. 6 | 7 | 8 | ## Requirements 9 | 10 | ```bash 11 | sudo apt-get install gcc-multilib nasm make 12 | ``` 13 | 14 | ## Build 15 | ```bash 16 | # This 17 | make 18 | 19 | # Or this 20 | make build 21 | ``` 22 | 23 | ## Run 24 | ```bash 25 | $ ./build/add42 26 | Result: 47 27 | ``` 28 | 29 | ## Clean 30 | ```bash 31 | make clean 32 | ``` 33 | 34 | 35 | ## Credits 36 | 37 | https://www.youtube.com/watch?v=nNdAygFyvjY 38 | -------------------------------------------------------------------------------- /32bit/add42-c/Makefile: -------------------------------------------------------------------------------- 1 | # Build directory 2 | PATH_BUILD = build 3 | 4 | # Assembler files 5 | NAME_ASM = add42 6 | PATH_ASM = add42 7 | SRC_ASM = $(PATH_ASM)/$(NAME_ASM).asm 8 | OBJ_ASM = $(PATH_BUILD)/$(NAME_ASM).o 9 | 10 | # C files 11 | FILE_C = main 12 | 13 | # Binary files 14 | NAME_BIN = add42 15 | FILE_BIN = $(PATH_BUILD)/$(NAME_BIN) 16 | 17 | # Compiler Flags (32bit) 18 | NASMFLAGS = -f elf32 19 | CFLAGS = -m32 20 | 21 | .PHONY: build 22 | 23 | build: 24 | if ! test -d $(PATH_BUILD); then mkdir $(PATH_BUILD); fi 25 | nasm $(NASMFLAGS) $(SRC_ASM) -o $(OBJ_ASM) 26 | gcc $(CFLAGS) $(OBJ_ASM) $(FILE_C).c -o $(FILE_BIN) 27 | 28 | clean: 29 | rm -f $(OBJ_ASM) 30 | rm -f $(FILE_BIN) 31 | rmdir $(PATH_BUILD) 32 | -------------------------------------------------------------------------------- /32bit/add42-c/add42/add42.asm: -------------------------------------------------------------------------------- 1 | ; vim: set ft=nasm: set syntax=nasm: 2 | 3 | ; Define usage for external programs 4 | global add42 5 | 6 | ; Define: int add42(int x) 7 | add42: 8 | ; prologue (required) 9 | push ebp ; Save base pointer on stack 10 | mov ebp, esp ; Set base pointer to current stack pointer 11 | ; function parameters 12 | mov eax, [ebp+8] ; Copy next address (where func param is) into EAX 13 | ; function & return 14 | add eax, 42 ; return (eax + 42) 15 | ; epilogue (required) 16 | mov esp, ebp ; Set stack pointer to current base pointer 17 | pop ebp ; Restore base pointer from stack 18 | ret ; pop eax && jmp eax 19 | -------------------------------------------------------------------------------- /32bit/print-asm/Makefile: -------------------------------------------------------------------------------- 1 | # Build directory 2 | PATH_BUILD = build 3 | 4 | # Assembler files 5 | NAME_ASM = print 6 | PATH_ASM = . 7 | SRC_ASM = $(PATH_ASM)/$(NAME_ASM).asm 8 | OBJ_ASM = $(PATH_BUILD)/$(NAME_ASM).o 9 | 10 | # Binary files 11 | NAME_BIN = print 12 | FILE_BIN = $(PATH_BUILD)/$(NAME_BIN) 13 | 14 | # Compiler Flags (32bit) 15 | NASMFLAGS = -f elf32 16 | LDFLAGS = -m elf_i386 17 | 18 | .PHONY: build dbuild 19 | 20 | # Normal build 21 | build: 22 | if ! test -d $(PATH_BUILD); then mkdir $(PATH_BUILD); fi 23 | nasm $(NASMFLAGS) $(SRC_ASM) -o $(OBJ_ASM) 24 | ld $(LDFLAGS) $(OBJ_ASM) -o $(FILE_BIN) 25 | 26 | # Debug build 27 | dbuild: 28 | if ! test -d $(PATH_BUILD); then mkdir $(PATH_BUILD); fi 29 | nasm -g -Fdwarf $(NASMFLAGS) $(SRC_ASM) -o $(OBJ_ASM) 30 | ld $(LDFLAGS) $(OBJ_ASM) -o $(FILE_BIN) 31 | 32 | clean: 33 | rm -f $(OBJ_ASM) 34 | rm -f $(FILE_BIN) 35 | rmdir $(PATH_BUILD) 36 | -------------------------------------------------------------------------------- /32bit/print-c/Makefile: -------------------------------------------------------------------------------- 1 | # Build directory 2 | PATH_BUILD = build 3 | 4 | # Assembler files 5 | NAME_ASM = print 6 | PATH_ASM = print 7 | SRC_ASM = $(PATH_ASM)/$(NAME_ASM).asm 8 | OBJ_ASM = $(PATH_BUILD)/$(NAME_ASM).o 9 | 10 | # C files 11 | FILE_C = main 12 | 13 | # Binary files 14 | NAME_BIN = print 15 | FILE_BIN = $(PATH_BUILD)/$(NAME_BIN) 16 | 17 | # Compiler Flags (32bit) 18 | NASMFLAGS = -f elf32 19 | CFLAGS = -m32 20 | 21 | .PHONY: build dbuild 22 | 23 | # Normal build 24 | build: 25 | if ! test -d $(PATH_BUILD); then mkdir $(PATH_BUILD); fi 26 | nasm $(NASMFLAGS) $(SRC_ASM) -o $(OBJ_ASM) 27 | gcc $(CFLAGS) $(OBJ_ASM) $(FILE_C).c -o $(FILE_BIN) 28 | 29 | # Debug build 30 | dbuild: 31 | if ! test -d $(PATH_BUILD); then mkdir $(PATH_BUILD); fi 32 | nasm -g -Fdwarf $(NASMFLAGS) $(SRC_ASM) -o $(OBJ_ASM) 33 | gcc -ggdb $(CFLAGS) $(OBJ_ASM) $(FILE_C).c -o $(FILE_BIN) 34 | 35 | clean: 36 | rm -f $(OBJ_ASM) 37 | rm -f $(FILE_BIN) 38 | rmdir $(PATH_BUILD) 39 | -------------------------------------------------------------------------------- /32bit/print-c/print/print.asm: -------------------------------------------------------------------------------- 1 | ; vim: set ft=nasm: set syntax=nasm: 2 | 3 | ; Define usage for external programs 4 | global print 5 | 6 | ;------------------------------------------ 7 | ; int print(char *msg, int length) 8 | ; Print message to stdout 9 | print: 10 | ; prologue (required) 11 | push ebp ; Save base pointer on stack 12 | mov ebp, esp ; Set base pointer to current stack pointer 13 | ; function parameters 14 | mov ebx, [ebp+0xc] ; arg2: copy arg2 from stack into EBX 15 | mov eax, [ebp+0x8] ; arg1: copy arg1 address from stack into EAX 16 | ; function 17 | mov edx, ebx ; Message length 18 | mov ecx, eax ; Get message address from EAX into ECX 19 | mov ebx, 1 ; Print to stdout (FD == 1) 20 | mov eax, 4 ; set SYSCALL value to SYS_WRITE 21 | int 0x80 ; make SYSCALL 22 | ; function return 23 | mov eax, 0 ; return 0 24 | ; epilogue (required) 25 | mov esp, ebp ; Set stack pointer to current base pointer 26 | pop ebp ; Restore base pointer from stack 27 | ret ; pop EAX && jmp EAX 28 | -------------------------------------------------------------------------------- /32bit/add42-asm/add42.asm: -------------------------------------------------------------------------------- 1 | ; vim: set ft=nasm: set syntax=nasm: 2 | 3 | SECTION .text 4 | global _start 5 | 6 | ; Entrypoint 7 | _start: 8 | push 5 9 | call add42 10 | call quit 11 | 12 | ;------------------------------------------ 13 | ; int add42(int x); 14 | ; Add 42 to whatever integer val is provided 15 | add42: 16 | ; prologue (required) 17 | push ebp ; Save base pointer on stack 18 | mov ebp, esp ; Set base pointer to current stack pointer 19 | ; function parameters 20 | mov eax, [ebp+8] ; Copy next address (where func param is) into EAX 21 | ; function & return 22 | add eax, 42 ; return (EAX + 42) 23 | ; epilogue (required) 24 | mov esp, ebp ; Set stack pointer to current base pointer 25 | pop ebp ; Restore base pointer from stack 26 | ret ; pop EAX && jmp EAX 27 | 28 | ;------------------------------------------ 29 | ; void exit(int status) 30 | ; Exit program and restore resources 31 | quit: 32 | mov ebx, eax ; return EAX value as exit code 33 | mov eax, 1 ; set SYS_EXIT (kernel opcode 1) for upcoming SYSCALL 34 | int 0x80 ; make SYSCALL 35 | ret ; pop EAX && jmp EAX 36 | -------------------------------------------------------------------------------- /32bit/print-asm/print.asm: -------------------------------------------------------------------------------- 1 | ; vim: set ft=nasm: set syntax=nasm: 2 | 3 | SECTION .data 4 | msg db 'Hello World!', 0x0A ; assign msg variable with your message string 5 | len equ $-msg ; "$" means "here" 6 | ; len is a value, not an address 7 | 8 | 9 | SECTION .text 10 | global _start 11 | 12 | ;------------------------------------------ 13 | ; Entrypoint 14 | _start: 15 | nop 16 | push len ; arg2: push string length onto the stack 17 | push msg ; arg1: push message address onto the stack 18 | call print 19 | call quit 20 | 21 | ;------------------------------------------ 22 | ; int print(char *msg, int length) 23 | ; Print message to stdout 24 | print: 25 | ; prologue (required) 26 | push ebp ; Save base pointer on stack 27 | mov ebp, esp ; Set base pointer to current stack pointer 28 | ; function parameters 29 | mov ebx, [ebp+0xC] ; arg2: copy arg2 from stack into EBX 30 | mov eax, [ebp+0x8] ; arg1: copy arg1 address from stack into EAX 31 | ; function 32 | mov edx, ebx ; Message length 33 | mov ecx, eax ; Get message address from EAX into ECX 34 | mov ebx, 1 ; Print to stdout (FD == 1) 35 | mov eax, 4 ; set SYSCALL value to SYS_WRITE 36 | int 0x80 ; make SYSCALL 37 | ; function return 38 | mov eax, 0 ; return 0 39 | ; epilogue (required) 40 | mov esp, ebp ; Set stack pointer to current base pointer 41 | pop ebp ; Restore base pointer from stack 42 | ret ; pop EAX && jmp EAX 43 | 44 | ;------------------------------------------ 45 | ; void exit(int status) 46 | ; Exit program and restore resources 47 | quit: 48 | mov ebx, eax ; return EAX value as exit code 49 | mov eax, 1 ; set SYS_EXIT (kernel opcode 1) for upcoming SYSCALL 50 | int 0x80 ; make SYSCALL 51 | ret 52 | --------------------------------------------------------------------------------