Skip to content
Open
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
1 change: 1 addition & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
pull_request:
branches:
- main
- x86_64

jobs:
checkpatch:
Expand Down
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.associations": {
"*.c": "c",
"param.h": "c"
}
}
2 changes: 1 addition & 1 deletion labs/lab-02/media/arit.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions labs/lab-02/reading/memory-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ For example:
char *char_ptr = 1000;
short *short_ptr = 2000;
int *int_ptr = 3000;
long long *long_long_ptr = 4000;

++char_ptr; /* char_ptr will point to address 1001 */
++short_ptr; /* short_ptr points to address 2002 */
++int_ptr; /* int_ptr points to address 3004 */
++long_long_ptr; /* long_long_ptr points to address 4008 */
```

![A diagram which visualizes arithmetic operations on pointers](../media/arit.svg)
Expand Down
11 changes: 6 additions & 5 deletions labs/lab-02/tasks/iterate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ Here is the given piece of C code:
#include <stdio.h>

int main() {
int v[] = {0xCAFEBABE, 0xDEADBEEF, 0x0B00B135, 0xBAADF00D, 0xDEADC0DE};
int v[] = {0xCAFEBABE, 0xDEADBEEF, 0x0B00B135, 0xBAADF00D, 0xDEADC0DE, 0x1EE71EE7};

return 0;
}
```

Display the addresses of the elements in the `v` array along with their values.
Iterate through the addresses in `v` byte by byte, two bytes at a time, and four bytes at a time.
Iterate through the addresses in `v` byte by byte, two bytes at a time, four bytes at a time, and eight bytes at a time.

> **TIP:** You can iterate through memory byte by byte starting from a specific address using a pointer of type `unsigned char*` (since the `char` type is represented by one byte).
>
Expand All @@ -43,9 +43,10 @@ make check
In case of a correct solution, you will get an output such as:

```text
test_chars ........................ passed ... 33
test_shorts ........................ passed ... 33
test_ints ........................ passed ... 34
test_chars ........................ passed ... 25
test_shorts ........................ passed ... 25
test_ints ........................ passed ... 25
test_long_longs ........................ passed ... 25

Total: 100/100
```
Expand Down
2 changes: 1 addition & 1 deletion labs/lab-02/tasks/iterate/solution/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

#include "array.h"

int v[5] = {0xCAFEBABE, 0xDEADBEEF, 0x0B00B135, 0xBAADF00D, 0xDEADC0DE};
int v[6] = {0xCAFEBABE, 0xDEADBEEF, 0x0B00B135, 0xBAADF00D, 0xDEADC0DE, 0x1EE71EE7};
2 changes: 1 addition & 1 deletion labs/lab-02/tasks/iterate/solution/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
#ifndef ARRAY_H
#define ARRAY_H 1

extern int v[5];
extern int v[6];

#endif // ARRAY_H
15 changes: 15 additions & 0 deletions labs/lab-02/tasks/iterate/solution/iterate.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,18 @@ void print_ints_ref(void)
}
printf("-------------------------------\n");
}

void print_long_longs_ref(void)
{
unsigned long long *long_long_ptr = (unsigned long long *) &v;
unsigned long long i;
/**
* Iterate through 8 bytes at a time, we have only a quarter of the steps because we are
* displaying 8 bytes at each step.
*/
for (i = 0 ; i < sizeof(v) / sizeof((*long_long_ptr)); ++i) {
printf("%p -> %p\n", long_long_ptr, *long_long_ptr);
++long_long_ptr;
}
printf("-------------------------------\n");
}
1 change: 1 addition & 0 deletions labs/lab-02/tasks/iterate/solution/iterate.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
void print_chars_ref(void);
void print_shorts_ref(void);
void print_ints_ref(void);
void print_long_longs_ref(void);

#endif // ITERATE_REF_H
1 change: 1 addition & 0 deletions labs/lab-02/tasks/iterate/solution/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ int main(void)
print_chars_ref();
print_shorts_ref();
print_ints_ref();
print_long_longs_ref();

return 0;
}
2 changes: 1 addition & 1 deletion labs/lab-02/tasks/iterate/support/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

#include "array.h"

int v[5] = {0xCAFEBABE, 0xDEADBEEF, 0x0B00B135, 0xBAADF00D, 0xDEADC0DE};
int v[6] = {0xCAFEBABE, 0xDEADBEEF, 0x0B00B135, 0xBAADF00D, 0xDEADC0DE, 0x1EE71EE7};
2 changes: 1 addition & 1 deletion labs/lab-02/tasks/iterate/support/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
#ifndef ARRAY_H
#define ARRAY_H 1

extern int v[5];
extern int v[6];

#endif // ARRAY_H
10 changes: 10 additions & 0 deletions labs/lab-02/tasks/iterate/support/iterate.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ void print_ints(void)

printf("-------------------------------\n");
}

void print_long_longs(void)
{
/**
* TODO: Implement function
*/
(void) v;

printf("-------------------------------\n");
}
1 change: 1 addition & 0 deletions labs/lab-02/tasks/iterate/support/iterate.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
void print_chars(void);
void print_shorts(void);
void print_ints(void);
void print_long_longs(void);

#endif // ITERATE_H
1 change: 1 addition & 0 deletions labs/lab-02/tasks/iterate/support/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ int main(void)
print_chars();
print_shorts();
print_ints();
print_long_longs();

return 0;
}
27 changes: 24 additions & 3 deletions labs/lab-02/tasks/iterate/tests/test_iterate.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
#define CHAR_OUTPUT "char.out"
#define SHORT_OUTPUT "short.out"
#define INT_OUTPUT "int.out"
#define LONG_LONG_OUTPUT "long_long.out"
#define CHAR_REF "char.ref"
#define SHORT_REF "short.ref"
#define INT_REF "int.ref"
#define LONG_LONG_REF "long_long.ref"

static int fd, stdout_fd;

Expand Down Expand Up @@ -120,10 +122,29 @@ static int test_ints(void)
return status == 0 ? 1 : 0;
}

static int test_ints(void)
{
int status;

prep_io(LONG_LONG_OUTPUT);
print_long_longs();
fflush(stdout);
restore_io();

prep_io(LONG_LONG_REF);
print_long_longs_ref();
fflush(stdout);
restore_io();

status = system("diff -q " LONG_LONG_OUTPUT " " LONG_LONG_REF);
return status == 0 ? 1 : 0;
}

static struct graded_test all_tests[] = {
{ test_chars, "test_chars", 33 },
{ test_shorts, "test_shorts", 33},
{ test_ints, "test_ints", 34},
{ test_chars, "test_chars", 25 },
{ test_shorts, "test_shorts", 25},
{ test_ints, "test_ints", 25},
{ test_long_longs, "test_long_long", 25},
};

int main(void)
Expand Down
38 changes: 19 additions & 19 deletions labs/lab-04/guides/discovering-assembly/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ nav_order: 10
To follow this guide, you will need to navigate to the `guides/discovering-assembly/support` directory.

1. Open the `ex1.asm` file and read the comments.
Assemble it by using the `make` utility and run it.
Using gdb, go through the program line by line (the `start` command followed by `next`) and observe the changes in register values after executing the `mov` and `add` instructions.
Ignore the sequence of `PRINTF32` instructions.
Assemble it by using the `make` utility and run it.
Using gdb, go through the program line by line (the `start` command followed by `next`) and observe the changes in register values after executing the `mov` and `add` instructions.
Ignore the sequence of `PRINTF64` instructions.

1. Open the `ex2.asm` file and read the comments.
Assemble it by using the `make` utility and run it.
Using gdb, observe the change in the `eip` register when executing the `jmp` instruction.
To skip the `PRINTF32` instructions, add a breakpoint at the `jump_incoming` label (the `break` command followed by `run`).
Assemble it by using the `make` utility and run it.
Using gdb, observe the change in the `rip` register when executing the `jmp` instruction.
To skip the `PRINTF64` instructions, add a breakpoint at the `jump_incoming` label (the `break` command followed by `run`).

1. Open the `ex3.asm` file and read the comments.
Assemble it by using the `make` utility and run it.
Using gdb, navigate through the program using breakpoints.
Follow the program flow.
Why is `15` displayed first and then `3`?
Because of the jump at line 9.
Where does the jump at line 25 point to?
To the `zone1` label.
Assemble it by using the `make` utility and run it.
Using gdb, navigate through the program using breakpoints.
Follow the program flow.
Why is `15` displayed first and then `3`?
Because of the jump at line 9.
Where does the jump at line 25 point to?
To the `zone1` label.

1. Open the `ex4.asm` file and read the comments.
Assemble it by using the `make` utility and run it.
Using gdb, go through the program.
Why isn't the jump at line 12 taken?
Because the `je` instruction jumps if the `ZF` bit in the `FLAGS` register is set.
This bit is set by the `cmp` instruction, which calculates the difference between the values of the `eax` and `ebx` registers without storing the result.
However, the `add` instruction at line 11 clears this flag because the result of the operation is different from 0.
Assemble it by using the `make` utility and run it.
Using gdb, go through the program.
Why isn't the jump at line 12 taken?
Because the `je` instruction jumps if the `ZF` bit in the `FLAGS` register is set.
This bit is set by the `cmp` instruction, which calculates the difference between the values of the `eax` and `ebx` registers without storing the result.
However, the `add` instruction at line 11 clears this flag because the result of the operation is different from 0.
6 changes: 3 additions & 3 deletions labs/lab-04/guides/discovering-assembly/support/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ RM = rm
SRCS := $(shell find . -name "*.asm")
OBJS := $(SRCS:.asm=.o)

UTILSDIR := ..utils/
UTILSDIR := ../utils/

ASFLAGS ?= -f elf32 -F dwarf -I "$(UTILSDIR)"
ASFLAGS ?= -f elf64 -F dwarf -I "$(UTILSDIR)"
CFLAGS ?= -Wall
LDFLAGS ?= -m32 -no-pie
LDFLAGS ?= -no-pie

all: ex1 ex2 ex3 ex4

Expand Down
16 changes: 11 additions & 5 deletions labs/lab-04/guides/discovering-assembly/support/ex1.asm
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
; SPDX-License-Identifier: BSD-3-Clause

%include "printf32.asm"
%include "printf64.asm"

section .text
global main
extern printf

main:
mov eax, 7 ; load the eax register with the value 7
mov ebx, 8 ; load the ebx register with the value 8
add eax, ebx ; add the value from ebx to the value from eax
push rbp
mov rbp, rsp

mov rax, 7 ; load the eax register with the value 7
mov r8, 8 ; load the ebx register with the value 8
add rax, r8 ; add the value from ebx to the value from eax
; and store the result in eax
PRINTF32 `%d\n\x0`, eax ; print the value from the eax register
PRINTF64 `%d\n\x0`, rax ; print the value from the eax register

leave
ret
18 changes: 11 additions & 7 deletions labs/lab-04/guides/discovering-assembly/support/ex2.asm
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
; SPDX-License-Identifier: BSD-3-Clause

%include "printf32.asm"
%include "printf64.asm"

section .text
global main
extern printf

main:
mov eax, 4
PRINTF32 `%d\n\x0`, eax
push rbp
mov rbp, rsp

mov r9, 4
PRINTF64 `%d\n\x0`, r9

jump_incoming:
jmp exit ; unconditional jump to the "exit" label

mov eax, 7 ; this code is unreachable, therefore not executed
mov ebx, 8
add eax, ebx
PRINTF32 `%d\n\x0`, eax
mov r9, 7 ; this code is unreachable, therefore not executed
mov rbx, 8
add r9, rbx
PRINTF64 `%d\n\x0`, r9

exit:
leave
ret
28 changes: 16 additions & 12 deletions labs/lab-04/guides/discovering-assembly/support/ex3.asm
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
; SPDX-License-Identifier: BSD-3-Clause

%include "printf32.asm"
%include "printf64.asm"

section .text
global main
extern printf

main:
mov eax, zone2
jmp eax ; unconditional jump to the address stored
push rbp
mov rbp, rsp

mov rax, zone2
jmp rax ; unconditional jump to the address stored
; in the eax register
zone1:
mov eax, 1
mov ebx, 2
add eax, ebx
PRINTF32 `%d\n\x0`, eax
mov rax, 1
mov r10, 2
add rax, r10
PRINTF64 `%d\n\x0`, rax
jump1:
jmp exit

zone2:
mov eax, 7
mov ebx, 8
add eax, ebx
PRINTF32 `%d\n\x0`, eax
mov rax, 7
mov r10, 8
add rax, r10
PRINTF64 `%d\n\x0`, rax
jump2:
jmp $-0x4A ; relative jump with a negative offset
jmp $-0x82 ; relative jump with a negative offset
; to the address of the previous instruction

exit:
leave
ret
19 changes: 13 additions & 6 deletions labs/lab-04/guides/discovering-assembly/support/ex4.asm
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
; SPDX-License-Identifier: BSD-3-Clause

%include "printf32.asm"
%include "printf64.asm"

section .text
global main
extern printf

main:
mov eax, 1
mov ebx, 1
cmp eax, ebx
add ecx, 1 ; Comment out this line
push rbp
mov rbp, rsp

mov rax, 1
mov rbx, 1
cmp rax, rbx
add r15, 1 ; Comment out this line
je print

leave
ret

print:
PRINTF32 `%d\n\x0`, eax
PRINTF64 `%d\n\x0`, rax

leave
ret
Loading
Loading