Skip to content

Commit bdbd92b

Browse files
PetruCristianteodutu
authored andcommitted
labs/lab-07: Add x86_64 version of lab 7
Reading material was modified to include x86_64 registers and instructions. Solutions, support code and tests were modified to work on x86_64 assembly and use the extended ISA. Signed-off-by: PetruCristian <[email protected]>
1 parent de830ab commit bdbd92b

File tree

45 files changed

+960
-583
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+960
-583
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ _site/
6060

6161
.view/
6262

63+
*.ref
64+
.gdb_history
65+
6366
# Ruby files for the local Jekyll server
6467
.sass-cache/
6568
.jekyll-cache/

labs/lab-07/guides/stack-addressing/README.md

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,46 @@ The `stack_addressing.asm` file demonstrates how data is stored on the stack, an
1010
Here's what an usual output for the compiled program would be:
1111

1212
```c
13-
0xff99fba8: 0xf7f46020
14-
0xff99fba4: 0xa
15-
0xff99fba0: 0xb
16-
0xff99fb9c: 0xc
17-
0xff99fb98: 0xd
13+
0x7fff124f4830: 0x7fff124f48d0
14+
0x7fff124f4828: 0xa
15+
0x7fff124f4820: 0xb
16+
0x7fff124f4818: 0xc
17+
0x7fff124f4810: 0xd
1818
```
1919

2020
> **Note:** The last 4 values are the ones we pushed on stack.
2121
> What is the first one?
2222
>
23-
> **Answer:** It is the old EBP we push at the start of the function.
23+
> **Answer:** It is the old RBP we push at the start of the function.
2424
2525
For convenience, here's the contents of the file.
2626
To play around with it, download the lab locally.
2727

2828
```assembly
29-
%include "printf32.asm"
29+
%include "printf64.asm"
3030
3131
section .text
3232
3333
extern printf
3434
global main
3535
main:
36-
push ebp
37-
mov ebp, esp
36+
push rbp
37+
mov rbp, rsp
3838
39-
push dword 10
40-
push dword 11
41-
push dword 12
42-
push dword 13
39+
push qword 10
40+
push qword 11
41+
push qword 12
42+
push qword 13
4343
44-
mov eax, ebp
44+
mov rax, rbp
4545
print_stack:
46-
PRINTF32 `0x\x0`
47-
PRINTF32 `%x\x0`, eax
48-
PRINTF32 `: 0x\x0`
49-
PRINTF32 `%x\n\x0`, [eax]
46+
PRINTF64 `%p: %p\n\x0`, rax, [rax]
5047
51-
sub eax, 4
52-
cmp eax, esp
48+
sub rax, 8
49+
cmp rax, rsp
5350
jge print_stack
5451
55-
xor eax, eax
52+
xor rax, rax
5653
leave
5754
ret
5855
```

labs/lab-07/guides/stack-addressing/support/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ OBJS := $(SRCS:.asm=.o)
77

88
UTILSDIR := ../utils/
99

10-
ASFLAGS ?= -f elf32 -F dwarf -I "$(UTILSDIR)"
10+
ASFLAGS ?= -f elf64 -F dwarf -I "$(UTILSDIR)"
1111
CFLAGS ?= -Wall
12-
LDFLAGS ?= -m32 -no-pie
12+
LDFLAGS ?= -m64 -no-pie
1313

1414
TARGET_EXEC = stack-addressing
1515

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
%include "printf32.asm"
1+
%include "printf64.asm"
22

33
section .text
44

55
extern printf
66
global main
77
main:
8-
push ebp
9-
mov ebp, esp
8+
push rbp
9+
mov rbp, rsp
1010

11-
push dword 10
12-
push dword 11
13-
push dword 12
14-
push dword 13
11+
push qword 10
12+
push qword 11
13+
push qword 12
14+
push qword 13
1515

16-
mov eax, ebp
16+
mov rax, rbp
1717
print_stack:
18-
PRINTF32 `%p: %p\n\x0`, eax, [eax]
18+
PRINTF64 `%p: %p\n\x0`, rax, qword [rax]
1919

20-
sub eax, 4
21-
cmp eax, esp
20+
sub rax, 8
21+
cmp rax, rsp
2222
jge print_stack
2323

24-
xor eax, eax
24+
xor rax, rax
2525
leave
2626
ret

labs/lab-07/guides/stack-addressing/utils/printf32.asm

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2+
;; no floating point support
3+
;; all parameters need to be 64bit wide
4+
;; format string int8_t=%hhx int16_t=%hx int32_t=%x int64_t=%lx
5+
;;
6+
%macro PRINTF64 1-*
7+
jmp %%endstr
8+
%%str db %1, 0
9+
%%endstr:
10+
pushfq
11+
push rax
12+
push rcx
13+
push rdx
14+
push rsi
15+
push rdi
16+
push r8
17+
push r9
18+
push r10
19+
push r11
20+
21+
push %%str
22+
%if %0 >= 2
23+
push %2
24+
%endif
25+
%if %0 >= 3
26+
push %3
27+
%endif
28+
%if %0 >= 4
29+
push %4
30+
%endif
31+
%if %0 >= 5
32+
push %5
33+
%endif
34+
%if %0 == 6
35+
push %6
36+
%endif
37+
%if %0 > 6
38+
%error "PRINTF64 accepts at most 6 arguments"
39+
%endif
40+
%if %0 == 6
41+
pop r9
42+
%endif
43+
%if %0 >= 5
44+
pop r8
45+
%endif
46+
%if %0 >= 4
47+
pop rcx
48+
%endif
49+
%if %0 >= 3
50+
pop rdx
51+
%endif
52+
%if %0 >= 2
53+
pop rsi
54+
%endif
55+
pop rdi
56+
xor eax, eax
57+
58+
call printf
59+
60+
61+
pop r11
62+
pop r10
63+
pop r9
64+
pop r8
65+
pop rdi
66+
pop rsi
67+
pop rdx
68+
pop rcx
69+
pop rax
70+
popfq
71+
%endmacro

labs/lab-07/guides/stack-operations/README.md

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,64 +11,60 @@ The main focus is to show how to manipulate the stack by pushing and popping val
1111
> **Note:** Notice how `push` and `pop` are just syntactic sugar for the simpler `sub`, `add`, and `mov` instructions.
1212
1313
For convenience, here's the contents of the file.
14-
To play around with it, download the lab locally.
14+
To play around with it, clone the repository locally.
1515

1616
```assembly
17-
%include "printf32.asm"
17+
%include "printf64.asm"
1818
1919
section .data
20-
var: dd ?
20+
var: dq ?
2121
2222
section .text
2323
24-
; esp -> stack pointer
25-
; ebp -> base pointer
24+
; rsp -> stack pointer
25+
; rbp -> base pointer
2626
2727
extern printf
2828
global main
2929
main:
30-
push ebp
31-
mov ebp, esp
30+
push rbp
31+
mov rbp, rsp
3232
33-
push dword 10 ; sub esp, 4; mov [esp], 10;
34-
push dword 11 ; sub esp, 4; mov [esp], 11;
35-
push dword 12 ; sub esp, 4; mov [esp], 12;
36-
push dword 13 ; sub esp, 4; mov [esp], 13;
37-
push dword 14 ; sub esp, 4; mov [esp], 13;
38-
39-
40-
pusha ; push all registers on the stack
41-
popa ; pop all registers from the stack
33+
push qword 10 ; same as: sub rsp, 8 followed by: mov [rsp], 10
34+
push qword 11 ; same as: sub rsp, 8 followed by: mov [rsp], 11
35+
push qword 12 ; same as: sub rsp, 8 followed by: mov [rsp], 12
36+
push qword 13 ; same as: sub rsp, 8 followed by: mov [rsp], 13
37+
push qword 14 ; same as: sub rsp, 8 followed by: mov [rsp], 13
4238
4339
; Version 1
44-
pop eax; ; mov eax, [esp]; add esp, 4
45-
pop eax; ; mov eax, [esp]; add esp, 4
46-
pop eax; ; mov eax, [esp]; add esp, 4
47-
pop eax; ; mov eax, [esp]; add esp, 4
48-
pop eax; ; mov eax, [esp]; add esp, 4
40+
pop rax ; same as: mov rax, [rsp] followed by: add rsp, 8
41+
pop rax ; same as: mov rax, [rsp] followed by: add rsp, 8
42+
pop rax ; same as: mov rax, [rsp] followed by: add rsp, 8
43+
pop rax ; same as: mov rax, [rsp] followed by: add rsp, 8
44+
pop rax ; same as: mov rax, [rsp] followed by: add rsp, 8
4945
5046
; Version 2
51-
; add esp, 20 ; 4 * number_of_push
47+
; add rsp, 40 ; 8 * number_of_push
5248
5349
; Version 3
54-
; mov esp, ebp
50+
; mov rsp, rbp
5551
56-
; sub esp <-> add esp -> use to allocate/deallocate memory
52+
; sub rsp <-> add rsp -> use to allocate/deallocate memory
5753
58-
; Aloc 8 bytes <-> 2 int
59-
; sub esp, 8
60-
; mov [esp], 10
61-
; mov [esp + 4], 12
54+
; Aloc 16 bytes <-> 2 long
55+
; sub rsp, 16
56+
; mov [rsp], 10
57+
; mov [rsp + 8], 12
6258
6359
; Push/Pop from global variable
6460
65-
mov dword [var], 1337
61+
mov qword [var], 1337
6662
67-
push dword [var]
68-
pop dword [var]
63+
push qword [var]
64+
pop qword [var]
6965
70-
mov eax, [var]
71-
PRINTF32 `VAR: %d\n\x0`, eax
66+
mov rax, [var]
67+
PRINTF64 `VAR: %d\n\x0`, rax
7268
7369
7470
leave

labs/lab-07/guides/stack-operations/support/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ OBJS := $(SRCS:.asm=.o)
77

88
UTILSDIR := ../utils/
99

10-
ASFLAGS ?= -f elf32 -F dwarf -I "$(UTILSDIR)"
10+
ASFLAGS ?= -f elf64 -F dwarf -I "$(UTILSDIR)"
1111
CFLAGS ?= -Wall
12-
LDFLAGS ?= -m32 -no-pie
12+
LDFLAGS ?= -m64 -no-pie
1313

1414
TARGET_EXEC = stack-operations
1515

0 commit comments

Comments
 (0)