Skip to content

Commit

Permalink
chap-05: Add introductory assembly examples
Browse files Browse the repository at this point in the history
  • Loading branch information
RazvanN7 committed Apr 8, 2024
1 parent 8068505 commit 365d275
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 0 deletions.
46 changes: 46 additions & 0 deletions curs/chap-05-ihs/00-intro/Makefile
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

Check failure on line 7 in curs/chap-05-ihs/00-intro/Makefile

View workflow job for this annotation

GitHub Actions / checkpatch review

ERROR: trailing whitespace

sum_array: sum_array.o

Check failure on line 9 in curs/chap-05-ihs/00-intro/Makefile

View workflow job for this annotation

GitHub Actions / checkpatch review

ERROR: trailing whitespace
$(CC) -m32 -no-pie -o sum_array sum_array.o


mul: mul.o

Check failure on line 13 in curs/chap-05-ihs/00-intro/Makefile

View workflow job for this annotation

GitHub Actions / checkpatch review

ERROR: trailing whitespace
$(CC) -m32 -no-pie -o mul mul.o

Check failure on line 14 in curs/chap-05-ihs/00-intro/Makefile

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: Possible repeated word: 'mul'

subreg: subreg.o

Check failure on line 16 in curs/chap-05-ihs/00-intro/Makefile

View workflow job for this annotation

GitHub Actions / checkpatch review

ERROR: trailing whitespace
$(CC) -m32 -no-pie -o subreg subreg.o

Check failure on line 17 in curs/chap-05-ihs/00-intro/Makefile

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: Possible repeated word: 'subreg'

declarations: declarations.o

Check failure on line 19 in curs/chap-05-ihs/00-intro/Makefile

View workflow job for this annotation

GitHub Actions / checkpatch review

ERROR: trailing whitespace
$(CC) -m32 -no-pie -o declarations declarations.o

Check failure on line 20 in curs/chap-05-ihs/00-intro/Makefile

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: Possible repeated word: 'declarations'

decl: decl.o

Check failure on line 22 in curs/chap-05-ihs/00-intro/Makefile

View workflow job for this annotation

GitHub Actions / checkpatch review

ERROR: trailing whitespace
$(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
11 changes: 11 additions & 0 deletions curs/chap-05-ihs/00-intro/decl.c
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;
}
52 changes: 52 additions & 0 deletions curs/chap-05-ihs/00-intro/declarations.asm
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
36 changes: 36 additions & 0 deletions curs/chap-05-ihs/00-intro/mul.asm
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
11 changes: 11 additions & 0 deletions curs/chap-05-ihs/00-intro/subreg.asm
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

0 comments on commit 365d275

Please sign in to comment.