├── .gitignore ├── Makefile ├── README.md ├── ex1 ├── Makefile └── ex1.asm ├── ex10 ├── Makefile └── ex10.asm ├── ex11 ├── Makefile ├── add42.asm ├── add42.h └── main.c ├── ex2 ├── Makefile └── ex2.asm ├── ex3 ├── Makefile └── ex3.asm ├── ex4 ├── Makefile └── ex4.asm ├── ex5 ├── Makefile └── ex5.asm ├── ex6 ├── Makefile └── ex6.asm ├── ex7 ├── Makefile └── ex7.asm ├── ex8 ├── Makefile └── ex8.asm └── ex9 ├── Makefile └── ex9.asm /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all 2 | * 3 | 4 | # Unignore directories, .gitignore, .asm, .c, .md, and Makefiles 5 | !*/ 6 | !.gitignore 7 | !*.asm 8 | !*.c 9 | !*.h 10 | !*.md 11 | !Makefile 12 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | $(MAKE) -C ex1 all 3 | $(MAKE) -C ex2 all 4 | $(MAKE) -C ex3 all 5 | $(MAKE) -C ex4 all 6 | $(MAKE) -C ex5 all 7 | $(MAKE) -C ex6 all 8 | $(MAKE) -C ex7 all 9 | $(MAKE) -C ex8 all 10 | $(MAKE) -C ex9 all 11 | $(MAKE) -C ex10 all 12 | $(MAKE) -C ex11 all 13 | 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # assembly-intro 2 | Intro to x86 Assembly Language 3 | -------------------------------------------------------------------------------- /ex1/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | nasm -f elf32 ex1.asm -o ex1.o 3 | ld -m elf_i386 ex1.o -o ex1 4 | -------------------------------------------------------------------------------- /ex1/ex1.asm: -------------------------------------------------------------------------------- 1 | global _start 2 | 3 | _start: 4 | mov eax, 1 5 | mov ebx, 42 6 | int 0x80 7 | -------------------------------------------------------------------------------- /ex10/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | nasm -f elf32 ex10.asm -o ex10.o 3 | gcc -m32 ex10.o -o ex10 4 | -------------------------------------------------------------------------------- /ex10/ex10.asm: -------------------------------------------------------------------------------- 1 | global main 2 | 3 | extern printf 4 | 5 | section .data 6 | msg db "Testing %i...", 0x0a, 0x00 7 | 8 | main: 9 | push ebp 10 | mov ebp, esp 11 | push 123 12 | push msg 13 | call printf 14 | mov eax, 0 15 | mov esp, ebp 16 | pop ebp 17 | ret -------------------------------------------------------------------------------- /ex11/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | nasm -f elf32 add42.asm -o add42.o 3 | gcc -m32 add42.o main.c -o ex11 4 | -------------------------------------------------------------------------------- /ex11/add42.asm: -------------------------------------------------------------------------------- 1 | global add42 2 | 3 | add42: 4 | push ebp 5 | mov ebp, esp 6 | mov eax, [ebp+8] 7 | add eax, 42 8 | mov esp, ebp 9 | pop ebp 10 | ret -------------------------------------------------------------------------------- /ex11/add42.h: -------------------------------------------------------------------------------- 1 | // Function that returns x + 42 2 | int add42(int x); 3 | -------------------------------------------------------------------------------- /ex11/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "add42.h" 3 | 4 | int main() { 5 | int result; 6 | result = add42(30); 7 | printf("Result: %i\n", result); 8 | return 0; 9 | } -------------------------------------------------------------------------------- /ex2/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | nasm -f elf32 ex2.asm -o ex2.o 3 | ld -m elf_i386 ex2.o -o ex2 4 | -------------------------------------------------------------------------------- /ex2/ex2.asm: -------------------------------------------------------------------------------- 1 | global _start 2 | 3 | section .data 4 | msg db "Hello, world!", 0x0a 5 | len equ $ - msg 6 | 7 | section .text 8 | _start: 9 | mov eax, 4 ; sys_write system call 10 | mov ebx, 1 ; stdout file descriptor 11 | mov ecx, msg ; bytes to write 12 | mov edx, len ; number of bytes to write 13 | int 0x80 ; perform system call 14 | mov eax, 1 ; sys_exit system call 15 | mov ebx, 0 ; exit status is 0 16 | int 0x80 -------------------------------------------------------------------------------- /ex3/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | nasm -f elf32 ex3.asm -o ex3.o 3 | ld -m elf_i386 ex3.o -o ex3 4 | -------------------------------------------------------------------------------- /ex3/ex3.asm: -------------------------------------------------------------------------------- 1 | global _start 2 | 3 | section .text 4 | _start: 5 | mov ecx, 99 ; set ecx to 99 6 | mov ebx, 42 ; exit status is 42 7 | mov eax, 1 ; sys_exit system call 8 | cmp ecx, 100 ; compare ecx to 100 9 | jl skip ; jump if less than 10 | mov ebx, 13 ; exit status is 13 11 | skip: 12 | int 0x80 13 | -------------------------------------------------------------------------------- /ex4/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | nasm -f elf32 ex4.asm -o ex4.o 3 | ld -m elf_i386 ex4.o -o ex4 4 | -------------------------------------------------------------------------------- /ex4/ex4.asm: -------------------------------------------------------------------------------- 1 | global _start 2 | 3 | section .text 4 | _start: 5 | mov ebx, 1 ; start ebx at 1 6 | mov ecx, 6 ; number of iterations 7 | label: 8 | add ebx, ebx ; ebx += ebx 9 | dec ecx ; ecx -= 1 10 | cmp ecx, 0 ; compare ecx with 0 11 | jg label ; jump to label if greater 12 | mov eax, 1 ; sys_exit system call 13 | int 0x80 14 | -------------------------------------------------------------------------------- /ex5/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | nasm -f elf32 ex5.asm -o ex5.o 3 | ld -m elf_i386 ex5.o -o ex5 4 | -------------------------------------------------------------------------------- /ex5/ex5.asm: -------------------------------------------------------------------------------- 1 | global _start 2 | 3 | section .data 4 | addr db "yellow" 5 | 6 | section .text 7 | _start: 8 | mov [addr], byte 'H' 9 | mov [addr+5], byte '!' 10 | mov eax, 4 ; sys_write system call 11 | mov ebx, 1 ; stdout file descriptor 12 | mov ecx, addr ; bytes to write 13 | mov edx, 6 ; number of bytes to write 14 | int 0x80 ; perform system call 15 | mov eax, 1 ; sys_exit system call 16 | mov ebx, 0 ; exit status is 0 17 | int 0x80 18 | -------------------------------------------------------------------------------- /ex6/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | nasm -f elf32 ex6.asm -o ex6.o 3 | ld -m elf_i386 ex6.o -o ex6 4 | -------------------------------------------------------------------------------- /ex6/ex6.asm: -------------------------------------------------------------------------------- 1 | global _start 2 | 3 | _start: 4 | sub esp, 4 5 | mov [esp], byte 'H' 6 | mov [esp+1], byte 'e' 7 | mov [esp+2], byte 'y' 8 | mov [esp+3], byte '!' 9 | mov eax, 4 ; sys_write system call 10 | mov ebx, 1 ; stdout file descriptor 11 | mov ecx, esp ; bytes to write 12 | mov edx, 4 ; number of bytes to write 13 | int 0x80 ; perform system call 14 | mov eax, 1 ; sys_exit system call 15 | mov ebx, 0 ; exit status is 0 16 | int 0x80 17 | -------------------------------------------------------------------------------- /ex7/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | nasm -f elf32 ex7.asm -o ex7.o 3 | ld -m elf_i386 ex7.o -o ex7 4 | -------------------------------------------------------------------------------- /ex7/ex7.asm: -------------------------------------------------------------------------------- 1 | global _start 2 | 3 | _start: 4 | call func 5 | mov eax, 1 6 | int 0x80 7 | 8 | func: 9 | mov ebx, 42 10 | ret -------------------------------------------------------------------------------- /ex8/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | nasm -f elf32 ex8.asm -o ex8.o 3 | ld -m elf_i386 ex8.o -o ex8 4 | -------------------------------------------------------------------------------- /ex8/ex8.asm: -------------------------------------------------------------------------------- 1 | global _start 2 | 3 | _start: 4 | call func 5 | mov eax, 1 6 | mov ebx, 0 7 | int 0x80 8 | 9 | func: 10 | push ebp 11 | mov ebp, esp 12 | sub esp, 2 13 | mov [esp], byte 'H' 14 | mov [esp+1], byte 'i' 15 | mov eax, 4 ; sys_write system call 16 | mov ebx, 1 ; stdout file descriptor 17 | mov ecx, esp ; bytes to write 18 | mov edx, 2 ; number of bytes to write 19 | int 0x80 ; perform system call 20 | mov esp, ebp 21 | pop ebp 22 | ret -------------------------------------------------------------------------------- /ex9/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | nasm -f elf32 ex9.asm -o ex9.o 3 | ld -m elf_i386 ex9.o -o ex9 4 | -------------------------------------------------------------------------------- /ex9/ex9.asm: -------------------------------------------------------------------------------- 1 | global _start 2 | 3 | _start: 4 | push 21 5 | call times2 6 | mov ebx, eax 7 | mov eax, 1 8 | int 0x80 9 | 10 | times2: 11 | push ebp 12 | mov ebp, esp 13 | mov eax, [ebp+8] 14 | add eax, eax 15 | mov esp, ebp 16 | pop ebp 17 | ret --------------------------------------------------------------------------------