-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chap-05: Add introductory assembly examples
- Loading branch information
Showing
5 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
CC = gcc | ||
CFLAGS = -O2 -m32 -g -fno-PIC | ||
NASM = nasm | ||
ASM_FLAGS = -f elf32 -g -F dwarf | ||
LD = ld -melf_i386 -no-pie | ||
|
||
all : hello | ||
|
||
sum_array: sum_array.o | ||
$(CC) -m32 -no-pie -o sum_array sum_array.o | ||
|
||
|
||
mul: mul.o | ||
$(CC) -m32 -no-pie -o mul mul.o | ||
|
||
subreg: subreg.o | ||
$(CC) -m32 -no-pie -o subreg subreg.o | ||
|
||
declarations: declarations.o | ||
$(CC) -m32 -no-pie -o declarations declarations.o | ||
|
||
decl: decl.o | ||
$(CC) -m32 -no-pie -o decl decl.o | ||
|
||
%.o : %.c | ||
$(warning CC=$(CC) FLAGS=$(CFLAGS)) | ||
$(CC) -c $(CFLAGS) -o $@ $< | ||
|
||
sum_array.o : sum_array.asm | ||
$(warning NASM=$(NASM) FLAGS=$(ASM_FLAGS)) | ||
$(NASM) $(ASM_FLAGS) -o $@ $< | ||
|
||
|
||
declarations.o : declarations.asm | ||
$(warning NASM=$(NASM) FLAGS=$(ASM_FLAGS)) | ||
$(NASM) $(ASM_FLAGS) -o $@ $< | ||
|
||
mul.o : mul.asm | ||
$(warning NASM=$(NASM) FLAGS=$(ASM_FLAGS)) | ||
$(NASM) $(ASM_FLAGS) -o $@ $< | ||
|
||
subreg.o : subreg.asm | ||
$(warning NASM=$(NASM) FLAGS=$(ASM_FLAGS)) | ||
$(NASM) $(ASM_FLAGS) -o $@ $< | ||
clean: | ||
rm -f *.o subreg declarations decl mul sum_array |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
int a = 9; | ||
int b; | ||
|
||
char t = 1; | ||
|
||
void main() | ||
{ | ||
int a, b; | ||
a = 0; | ||
b = 7; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
section .data | ||
var1: db 9 | ||
var2: dw 10 | ||
var3: dd 11 | ||
arr1: db 8, 9, 10 | ||
arr2: dw 8, 9, 10 | ||
arr3: dd 0xAABBCCDD, 0x11223344, 0x55667788 | ||
str: db "Hello World",0 | ||
|
||
section .bss | ||
a: resb 4 | ||
|
||
section .rodata | ||
b: dd 9 | ||
|
||
section .text | ||
global main | ||
|
||
main: | ||
xor eax, eax | ||
mov al, [var1] | ||
mov ah, [var1] | ||
mov ax, [var2] | ||
mov eax, [var3] | ||
|
||
xor ebx, ebx | ||
mov bl, [arr1] | ||
mov bh, [arr1 + 1] | ||
mov bl, [arr1 + 2] | ||
|
||
mov bx, [arr2] | ||
mov bx, [arr2 + 2] | ||
mov bx, [arr2 + 3] | ||
|
||
mov ebx, [arr3 + 4] | ||
|
||
mov cl, [str] | ||
mov ecx, [str] | ||
|
||
mov eax, [a] | ||
mov ebx, [b] | ||
|
||
mov ax, [var1] | ||
mov ax, [var1 + 1] | ||
mov eax, [var1] | ||
mov eax, [arr3 + 2] | ||
|
||
mov dword[a], 0xaabbccdd | ||
;mov [b], ebx | ||
mov dword[a], 20 | ||
|
||
ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
section .data | ||
num1: dd 10 | ||
num2: dd 40 | ||
|
||
num3: dw 500 | ||
num4: dw 3 | ||
|
||
num5: dw 1 | ||
|
||
section .bss | ||
a: resb 4 | ||
|
||
section .text | ||
global main | ||
|
||
main: | ||
|
||
mov al, [num1] | ||
mov bl, [num2] | ||
|
||
mul bl | ||
|
||
mov ax, [num3] | ||
xor dx, dx | ||
mov cx, [num5] | ||
div cx | ||
|
||
mov ax, [num3] | ||
mov cl, [num4] | ||
div cl | ||
|
||
;mov ax, [num3] | ||
;mov cl, [num5] | ||
;div cl | ||
|
||
ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
section .text | ||
global main | ||
|
||
main: | ||
mov eax, 1 | ||
shl eax, 28 | ||
mov al, 10 | ||
mov ah, 10 | ||
mov eax, 11 | ||
ret | ||
|