Skip to content

Commit d12dc9f

Browse files
author
Leonhard Bromoloron
committed
print ascii reverse
1 parent 7d228d2 commit d12dc9f

7 files changed

Lines changed: 84 additions & 9 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

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)