Skip to content

Commit 2e02a4c

Browse files
committed
hello deadbeef
1 parent 7d228d2 commit 2e02a4c

10 files changed

Lines changed: 120 additions & 39 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
[Linux System Call Table](https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64)
44

5+
[x86 registers](https://wiki.osdev.org/CPU_Registers_x86)
6+
7+
[x86 assembly language](https://en.wikipedia.org/wiki/X86_assembly_language)
8+
59
```
610
/usr/include/asm-generic/fcntl.h
711
```

draft/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CFLAGS += -D_GNU_SOURCE
22
CFLAGS += -Wall
33
CFLAGS += -pedantic-errors
4+
CFLAGS += -Og
45

56
draft: draft.c
67
$(CC) $(CFLAGS) -fno-omit-frame-pointer -g -o draft $^

draft/count_memi.s

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
.file "draft.c"
2+
.text
3+
.globl main
4+
.type main, @function
5+
main:
6+
.LFB23:
7+
.cfi_startproc
8+
pushq %rbp
9+
.cfi_def_cfa_offset 16
10+
.cfi_offset 6, -16
11+
movq %rsp, %rbp
12+
.cfi_def_cfa_register 6
13+
subq $16, %rsp
14+
movl i(%rip), %eax
15+
leal 1(%rax), %ecx
16+
movl %ecx, i(%rip)
17+
movslq %eax, %rdx
18+
movb $48, -5(%rbp,%rdx)
19+
leal 2(%rax), %edx
20+
movl %edx, i(%rip)
21+
movslq %ecx, %rcx
22+
movb $120, -5(%rbp,%rcx)
23+
leal 3(%rax), %ecx
24+
movl %ecx, i(%rip)
25+
movslq %edx, %rdx
26+
movb $52, -5(%rbp,%rdx)
27+
leal 4(%rax), %edx
28+
movl %edx, i(%rip)
29+
movslq %ecx, %rcx
30+
movb $49, -5(%rbp,%rcx)
31+
addl $5, %eax
32+
movl %eax, i(%rip)
33+
movslq %edx, %rdx
34+
movb $10, -5(%rbp,%rdx)
35+
leaq -5(%rbp), %rdx
36+
movl $5, %ecx
37+
movl $1, %esi
38+
movl $1, %edi
39+
movl $0, %eax
40+
call syscall@PLT
41+
movl $0, %eax
42+
leave
43+
.cfi_def_cfa 7, 8
44+
ret
45+
.cfi_endproc
46+
.LFE23:
47+
.size main, .-main
48+
.globl i
49+
.bss
50+
.align 4
51+
.type i, @object
52+
.size i, 4
53+
i:
54+
.zero 4
55+
.globl fh
56+
.align 4
57+
.type fh, @object
58+
.size fh, 4
59+
fh:
60+
.zero 4
61+
.ident "GCC: (Debian 14.2.0-19) 14.2.0"
62+
.section .note.GNU-stack,"",@progbits

draft/draft.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66
#include <sys/syscall.h>
77
#include <sys/types.h>
88

9-
int fh = 0;
10-
int i = 0;
11-
129
int main(int argc, char *argv[])
1310
{
14-
char out[5];
15-
out[i++] = '0';
16-
out[i++] = 'x';
17-
out[i++] = '4';
18-
out[i++] = '1';
19-
out[i++] = '\n';
20-
syscall(__NR_write, 1, out, 5);
11+
char out[11];
12+
out[0] = '0';
13+
out[1] = 'x';
14+
out[2] = 'd';
15+
out[3] = 'e';
16+
out[4] = 'a';
17+
out[5] = 'd';
18+
out[6] = 'b';
19+
out[7] = 'e';
20+
out[8] = 'e';
21+
out[9] = 'f';
22+
out[10] = '\n';
23+
syscall(__NR_write, 1, out, 11);
2124
return 0;
2225
}

hello/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ default:
22
$(CC) hello.s -c -g
33
$(LD) -o hello hello.o
44

5+
check:
6+
./hello | md5sum | grep -q ^823c843e5 && printf "\033[1;32m[OK]\033[0m\n"
7+
58
run:
69
./hello

hello/hello.s

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
#https://www.youtube.com/watch?v=3nYHV5zIQGA
2-
.globl _start
3-
4-
.hello.str:
5-
.asciz "12345678\n"
6-
7-
.text
8-
9-
# void print_chars(int {rsi}, int {rdx});
10-
print_chars:
11-
movq $1, %rax
12-
movq $1, %rdi
13-
syscall
14-
ret
15-
1+
.text
2+
.globl _start
163
_start:
17-
#https://stackoverflow.com/questions/29790175/assembly-x86-leave-instruction
184
pushq %rbp
195
movq %rsp, %rbp
6+
movb $48, -11(%rbp)
7+
movb $120, -10(%rbp)
8+
movb $100, -9(%rbp)
9+
movb $101, -8(%rbp)
10+
movb $97, -7(%rbp)
11+
movb $100, -6(%rbp)
12+
movb $98, -5(%rbp)
13+
movb $101, -4(%rbp)
14+
movb $101, -3(%rbp)
15+
movb $102, -2(%rbp)
16+
movb $10, -1(%rbp)
17+
mov $11, %rdx
18+
leaq -11(%rbp), %rsi
19+
mov $1, %rdi
20+
mov $1, %rax
21+
syscall
22+
jmp exit
2023

21-
leaq .hello.str, %rsi
22-
movq $10, %rdx
23-
call print_chars
24-
24+
exit:
2525
movq $60, %rax
2626
movq $0, %rdi
2727
syscall

print_int/.gdbinit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ lay asm
33
define hook-quit
44
set confirm off
55
end
6+
break le_print

print_int/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
hello
1+
/print_int

print_int/print_int

-6.09 KB
Binary file not shown.

print_int/print_int.s

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
.text
2323
.globl _start
2424

25-
# void print_chars(char *rsi, int rdx);
25+
# char *rsi, int rdx;
2626
print_chars:
2727
movq $WRITE, %rax
2828
movq $STDOUT, %rdi
@@ -35,8 +35,11 @@ print_int:
3535
push %rbp
3636
push %rsi
3737
push %rdx
38+
push %rcx
3839
mov %rsp, %rbp # save stack pointer
39-
push $0xa # "\n"
40+
mov $0, %rcx
41+
movb $0xa, -64(%rbp, %rcx)
42+
inc %rcx
4043
print_int_loop:
4144
mov %rsi, %rax
4245
and $15, %rax
@@ -45,17 +48,21 @@ print_int_loop:
4548
jle print_int_after_adjust
4649
add $39, %rax # adjust for ascii "a"-"f"
4750
print_int_after_adjust:
48-
push %rax
51+
movb %al, -64(%rbp, %rcx)
52+
inc %rcx
4953
shr $4, %rsi
5054
test %rsi, %rsi
5155
jnz print_int_loop
52-
push $0x78 # "x"
53-
push $0x30 # "0"
54-
mov %rsp, %rsi
55-
mov %rbp, %rdx
56-
sub %rsp, %rdx # print from rsp to rbp
56+
movb $0x78, -64(%rbp, %rcx)
57+
inc %rcx
58+
movb $0x30, -64(%rbp, %rcx)
59+
inc %rcx
60+
leaq -64(%rbp), %rsi
61+
mov %rcx, %rdx # len
62+
le_print:
5763
call print_chars
5864
mov %rbp, %rsp # restore stack pointer
65+
pop %rcx
5966
pop %rdx
6067
pop %rsi
6168
pop %rbp

0 commit comments

Comments
 (0)