Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review chapter 05 #62

Merged
merged 7 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions curs/chap-05-ihs/01-disass/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/one_var
/many_vars
19 changes: 19 additions & 0 deletions curs/chap-05-ihs/01-disass/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CFLAGS = -m32 -fno-PIC -Wall
LDFLAGS = -m32 -no-pie

.PHONY: all clean

all: one_var many_vars

one_var: one_var.o

one_var.o: one_var.c

many_vars: many_vars.o

many_vars.o: many_vars.c

clean:
-rm -f *~
-rm -f one_var.o one_var
-rm -f many_vars.o many_vars
41 changes: 41 additions & 0 deletions curs/chap-05-ihs/01-disass/many_vars.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>

Check failure on line 1 in curs/chap-05-ihs/01-disass/many_vars.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: Missing or malformed SPDX-License-Identifier tag in line 1
#include <string.h>

unsigned int age = 10;
unsigned short new_age;

struct {
unsigned int age;
unsigned short occupation;
unsigned int new_age;
unsigned char location;
} s;

unsigned int v[4] = {10, 20, 30, 40};
unsigned int *p;

void g(void)
{
s.age = 10;
s.occupation = 20;
s.new_age = 30;
s.location = 40;

v[2] = 1000;
p = &v[2];
}

void f(void)
{
new_age = age + 4;
}

unsigned long long alfa(void)
{
return 119328984948393;
}

int main(void)
{
return 0;
}
13 changes: 13 additions & 0 deletions curs/chap-05-ihs/01-disass/one_var.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>

Check failure on line 1 in curs/chap-05-ihs/01-disass/one_var.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: Missing or malformed SPDX-License-Identifier tag in line 1

unsigned int age = 32;

void f(void)
{
age = 64;
}

int main(void)
{
return 0;
}
2 changes: 2 additions & 0 deletions curs/chap-05-ihs/02-jmp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/je
/jmp
19 changes: 19 additions & 0 deletions curs/chap-05-ihs/02-jmp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CFLAGS = -m32 -fno-PIC -Wall
LDFLAGS = -m32 -no-pie

.PHONY: all clean

all: jmp je

jmp: jmp.o

jmp.o: jmp.c

je: je.o

je.o: je.c

clean:
-rm -f *~
-rm -f jmp.o jmp
-rm -f je.o je
12 changes: 12 additions & 0 deletions curs/chap-05-ihs/02-jmp/je.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
unsigned int i;

Check failure on line 1 in curs/chap-05-ihs/02-jmp/je.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: Missing or malformed SPDX-License-Identifier tag in line 1

int main(void)
{
i = 0;

if (i > 0)
i += 1;
i += 2;

return 0;
}
10 changes: 10 additions & 0 deletions curs/chap-05-ihs/02-jmp/jmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
unsigned int i;

Check failure on line 1 in curs/chap-05-ihs/02-jmp/jmp.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: Missing or malformed SPDX-License-Identifier tag in line 1

int main(void)
{
i = 0;
start:
i += 2;
goto start;
i += 6;
}
1 change: 1 addition & 0 deletions curs/chap-05-ihs/03-flags/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/flags
21 changes: 21 additions & 0 deletions curs/chap-05-ihs/03-flags/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
NASM = nasm
AFILES = flags.asm
OBJS = $(AFILES:.asm=.o)
ASM_FLAGS = -f elf32 -g -F dwarf
LD = gcc
LDFLAGS = -m32 -g -fno-PIC -no-pie
BINARIES = flags

.PHONY: all clean

all : $(BINARIES)

%.o : %.asm
$(NASM) $(ASM_FLAGS) -o $@ $<

%: %.o
$(LD) $(LDFLAGS) -o $@ $^

clean:
-rm -f *.o $(BINARIES) *.s
-rm -f *~
78 changes: 78 additions & 0 deletions curs/chap-05-ihs/03-flags/flags.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
section .text
global main

main:
push ebp
mov ebp, esp

; ZF = 1
; cmp eax, eax
; xor eax, eax
sub eax, eax

; ZF = 0
inc eax

; CF = 1
shr eax, 1

; CF = 0
shr eax, 1

; SF = 1
dec eax ; eax <- 0xffffffff

; SF = 0
inc eax

; OF = 1

; OF = 0

; ZF = 1, CF = 1

; ZF = 0, CF = 0

; ZF = 0, CF = 1

; ZF = 1, CF = 0

; SF = 1, OF = 1

; SF = 0, OF = 0

; SF = 0, OF = 1
mov eax, 0x80000000 ; signed int = -2 ^ 31
sub eax, 1

; SF = 1, OF = 0

; SF = 1, ZF = 1
; N/A

; SF = 0, ZF = 0

mov eax, 1
; SF = 0, ZF = 1
xor eax, eax
;shr eax, N
;dec eax
;sub eax, 1

; SF = 1, ZF = 0

; SF = 1, CF = 1

mov eax, 1
; SF = 0, CF = 0
shl eax, 1
add eax, 1
sub eax, 1
xor eax, eax

; SF = 0, CF = 1

; SF = 1, CF = 0

pop ebp
ret
1 change: 1 addition & 0 deletions curs/chap-05-ihs/04-sum/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/sum
21 changes: 21 additions & 0 deletions curs/chap-05-ihs/04-sum/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
NASM = nasm
AFILES = hello_world.asm
OBJS = $(AFILES:.asm=.o)
ASM_FLAGS = -f elf32 -g -F dwarf
LD = gcc
LDFLAGS = -m32 -g -fno-PIC -no-pie
BINARIES = sum

.PHONY: all clean

all : $(BINARIES)

%.o : %.asm
$(NASM) $(ASM_FLAGS) -o $@ $<

%: %.o
$(LD) $(LDFLAGS) -o $@ $^

clean:
-rm -f *.o $(BINARIES) *.s
-rm -f *~
27 changes: 27 additions & 0 deletions curs/chap-05-ihs/04-sum/sum.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
%include "../io.mac"

section .bss
sum: resd 1

section .text
global main
extern printf

main:
push ebp
mov ebp, esp
mov eax, 0 ; eax is the loop counter
mov edx, 0 ; edx is sum
begin:
cmp eax, 100
ja out
add edx, eax
add eax, 1
jmp begin
out:
mov [sum], edx

PRINTF32 `%d\n\x0`, [sum]
nop
pop ebp
ret
25 changes: 25 additions & 0 deletions curs/chap-05-ihs/05-hello-world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# chap-03-demo

1. Demo gdb
- make
- gdb hello
- Comands: b main, r, n
- set $eax = 0xffffffff
- set $eip = main

2. Demo gdb
step through hello.asm to inspect the changes to the following registers:
* EAX, AX, AH, AL
* EFLAGS
* EIP
Note: Instructions to whatch are: MOV, ADD, JMP

3. Demo Inspect ~/.gdbinit

Check failure on line 17 in curs/chap-05-ihs/05-hello-world/README.md

View workflow job for this annotation

GitHub Actions / checkpatch review

ERROR: trailing whitespace
- less ~/.gdbinit

cleanup:
set disassembly-flavor intel
set history save on



78 changes: 78 additions & 0 deletions curs/chap-05-ihs/06-dandamudi/ijump.sasm.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
;; adaptat cu macrourile SASM - ușor diferite !
;Sample indirect jump example IJUMP.ASM
;
; Objective: To demonstrate the use of indirect jump.
; Input: Requests a digit character from the user.
; Output: Appropriate class selection message.
%include "io.inc"

section .data
jump_table dd code_for_0 ; indirect jump pointer table
dd code_for_1
dd code_for_2
dd default_code ; default code for digits 3-9
dd default_code
dd default_code
dd default_code
dd default_code
dd default_code
dd default_code

prompt_msg db "Type a digit: ",0
msg_0 db "Economy class selected.",0
msg_1 db "Business class selected.",0
msg_2 db "First class selected.",0
msg_default db "Not a valid code!",0
msg_nodigit db "Not a digit! Try again.",0
key db 0

section .text
global main
main:
mov ebp, esp; for correct debugging
read_again:
PRINT_STRING prompt_msg ; request a digit
sub EAX,EAX ; EAX = 0
GET_CHAR key
mov AL, [key] ; read input digit and
cmp AL,'0' ; check to see if it is a digit
jb not_digit
cmp AL,'9'
ja not_digit
; if digit, proceed
sub AL,'0' ; convert to numeric equivalent
mov ESI,EAX ; ESI is index into jump table
add ESI,ESI ; ESI = ESI * 4
add ESI,ESI
jmp [jump_table+ESI] ; indirect jump based on ESI
test_termination:
cmp AL,2
ja done
jmp read_again
code_for_0:
PRINT_STRING msg_0
NEWLINE
jmp test_termination
code_for_1:
PRINT_STRING msg_1
NEWLINE
jmp test_termination
code_for_2:
PRINT_STRING msg_2
NEWLINE
jmp test_termination
default_code:
PRINT_STRING msg_default
NEWLINE
jmp test_termination

not_digit:
PRINT_STRING msg_nodigit
NEWLINE
jmp read_again
done:
mov eax, 1
mov ebx, 0
int 0x80 ; exit(0)
File renamed without changes.
3 changes: 3 additions & 0 deletions curs/chap-05-ihs/08-data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/data_c
/data_asm
/data_c.s
Loading
Loading