Skip to content

Lab 07 x86 64 version #131

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

Open
wants to merge 1 commit into
base: x86_64
Choose a base branch
from
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
39 changes: 18 additions & 21 deletions labs/lab-07/guides/stack-addressing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,46 @@ The `stack_addressing.asm` file demonstrates how data is stored on the stack, an
Here's what an usual output for the compiled program would be:

```c
0xff99fba8: 0xf7f46020
0xff99fba4: 0xa
0xff99fba0: 0xb
0xff99fb9c: 0xc
0xff99fb98: 0xd
0x7fff124f4830: 0x7fff124f48d0
0x7fff124f4828: 0xa
0x7fff124f4820: 0xb
0x7fff124f4818: 0xc
0x7fff124f4810: 0xd
```

> **Note:** The last 4 values are the ones we pushed on stack.
> What is the first one?
>
> **Answer:** It is the old EBP we push at the start of the function.
> **Answer:** It is the old RBP we push at the start of the function.

For convenience, here's the contents of the file.
To play around with it, download the lab locally.

```assembly
%include "printf32.asm"
%include "printf64.asm"

section .text

extern printf
global main
main:
push ebp
mov ebp, esp
push rbp
mov rbp, rsp

push dword 10
push dword 11
push dword 12
push dword 13
push qword 10
push qword 11
push qword 12
push qword 13

mov eax, ebp
mov rax, rbp
print_stack:
PRINTF32 `0x\x0`
PRINTF32 `%x\x0`, eax
PRINTF32 `: 0x\x0`
PRINTF32 `%x\n\x0`, [eax]
PRINTF64 `%p: %p\n\x0`, rax, [rax]

sub eax, 4
cmp eax, esp
sub rax, 8
cmp rax, rsp
jge print_stack

xor eax, eax
xor rax, rax
leave
ret
```
4 changes: 2 additions & 2 deletions labs/lab-07/guides/stack-addressing/support/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ OBJS := $(SRCS:.asm=.o)

UTILSDIR := ../utils/

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

TARGET_EXEC = stack-addressing

Expand Down
24 changes: 12 additions & 12 deletions labs/lab-07/guides/stack-addressing/support/stack-addressing.asm
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
%include "printf32.asm"
%include "printf64.asm"

section .text

extern printf
global main
main:
push ebp
mov ebp, esp
push rbp
mov rbp, rsp

push dword 10
push dword 11
push dword 12
push dword 13
push qword 10
push qword 11
push qword 12
push qword 13

mov eax, ebp
mov rax, rbp
print_stack:
PRINTF32 `%p: %p\n\x0`, eax, [eax]
PRINTF64 `%p: %p\n\x0`, rax, qword [rax]

sub eax, 4
cmp eax, esp
sub rax, 8
cmp rax, rsp
jge print_stack

xor eax, eax
xor rax, rax
leave
ret
24 changes: 0 additions & 24 deletions labs/lab-07/guides/stack-addressing/utils/printf32.asm

This file was deleted.

73 changes: 73 additions & 0 deletions labs/lab-07/guides/stack-addressing/utils/printf64.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; no floating point support
;; all parameters need to be 64bit wide
;; format string int8_t=%hhx int16_t=%hx int32_t=%x int64_t=%lx
;;
%macro PRINTF64 1-*
jmp %%endstr
%%str db %1, 0
%%endstr:
;sub rsp, 8
pushfq
push rax
push rcx
push rdx
push rsi
push rdi
push r8
push r9
push r10
push r11

push %%str
%if %0 >= 2
push %2
%endif
%if %0 >= 3
push %3
%endif
%if %0 >= 4
push %4
%endif
%if %0 >= 5
push %5
%endif
%if %0 == 6
push %6
%endif
%if %0 > 6
%error "PRINTF64 accepts at most 6 arguments"
%endif
%if %0 == 6
pop r9
%endif
%if %0 >= 5
pop r8
%endif
%if %0 >= 4
pop rcx
%endif
%if %0 >= 3
pop rdx
%endif
%if %0 >= 2
pop rsi
%endif
pop rdi
xor eax, eax

call printf


pop r11
pop r10
pop r9
pop r8
pop rdi
pop rsi
pop rdx
pop rcx
pop rax
popfq
;add rsp, 8
%endmacro
60 changes: 28 additions & 32 deletions labs/lab-07/guides/stack-operations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,57 @@ For convenience, here's the contents of the file.
To play around with it, download the lab locally.

```assembly
%include "printf32.asm"
%include "printf64.asm"

section .data
var: dd ?
var: dq ?

section .text

; esp -> stack pointer
; ebp -> base pointer
; rsp -> stack pointer
; rbp -> base pointer

extern printf
global main
main:
push ebp
mov ebp, esp
push rbp
mov rbp, rsp

push dword 10 ; sub esp, 4; mov [esp], 10;
push dword 11 ; sub esp, 4; mov [esp], 11;
push dword 12 ; sub esp, 4; mov [esp], 12;
push dword 13 ; sub esp, 4; mov [esp], 13;
push dword 14 ; sub esp, 4; mov [esp], 13;


pusha ; push all registers on the stack
popa ; pop all registers from the stack
push qword 10 ; same as: sub rsp, 8 followed by: mov [rsp], 10
push qword 11 ; same as: sub rsp, 8 followed by: mov [rsp], 11
push qword 12 ; same as: sub rsp, 8 followed by: mov [rsp], 12
push qword 13 ; same as: sub rsp, 8 followed by: mov [rsp], 13
push qword 14 ; same as: sub rsp, 8 followed by: mov [rsp], 13

; Version 1
pop eax; ; mov eax, [esp]; add esp, 4
pop eax; ; mov eax, [esp]; add esp, 4
pop eax; ; mov eax, [esp]; add esp, 4
pop eax; ; mov eax, [esp]; add esp, 4
pop eax; ; mov eax, [esp]; add esp, 4
pop rax ; same as: mov rax, [rsp] followed by: add rsp, 8
pop rax ; same as: mov rax, [rsp] followed by: add rsp, 8
pop rax ; same as: mov rax, [rsp] followed by: add rsp, 8
pop rax ; same as: mov rax, [rsp] followed by: add rsp, 8
pop rax ; same as: mov rax, [rsp] followed by: add rsp, 8

; Version 2
; add esp, 20 ; 4 * number_of_push
; add rsp, 40 ; 8 * number_of_push

; Version 3
; mov esp, ebp
; mov rsp, rbp

; sub esp <-> add esp -> use to allocate/deallocate memory
; sub rsp <-> add rsp -> use to allocate/deallocate memory

; Aloc 8 bytes <-> 2 int
; sub esp, 8
; mov [esp], 10
; mov [esp + 4], 12
; Aloc 16 bytes <-> 2 long
; sub rsp, 16
; mov [rsp], 10
; mov [rsp + 8], 12

; Push/Pop from global variable

mov dword [var], 1337
mov qword [var], 1337

push dword [var]
pop dword [var]
push qword [var]
pop qword [var]

mov eax, [var]
PRINTF32 `VAR: %d\n\x0`, eax
mov rax, [var]
PRINTF64 `VAR: %d\n\x0`, rax


leave
Expand Down
4 changes: 2 additions & 2 deletions labs/lab-07/guides/stack-operations/support/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ OBJS := $(SRCS:.asm=.o)

UTILSDIR := ../utils/

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

TARGET_EXEC = stack-operations

Expand Down
Loading