Skip to content

Commit 46428fb

Browse files
committed
printf
1 parent 4eca3f7 commit 46428fb

File tree

7 files changed

+32
-72
lines changed

7 files changed

+32
-72
lines changed

README.md

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

1111
[Linux clobbered registers](https://stackoverflow.com/questions/69515893/when-does-linux-x86-64-syscall-clobber-r8-r9-and-r10)
1212

13+
[Calling printf in x86_64 using GNU assembler](https://stackoverflow.com/questions/38335212/calling-printf-in-x86-64-using-gnu-assembler)
14+
1315
```
1416
/usr/include/asm-generic/fcntl.h
1517
```

draft/draft.c

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,69 +7,14 @@
77
#include <sys/syscall.h>
88
#include <sys/types.h>
99

10-
char* in;
11-
char* out;
12-
13-
int get_zero() {
14-
int result = 128;
15-
while(true) {
16-
result = result / 2;
17-
if (result == 16) {
18-
return 0;
19-
}
20-
}
21-
return 1;
22-
}
23-
24-
int get_one() {
25-
int result = 128;
26-
while(true) {
27-
result = result / 2;
28-
if (result == 8) {
29-
return 1;
30-
}
31-
}
32-
return 2;
33-
}
34-
35-
int get_two() {
36-
int result = 128;
37-
while(true) {
38-
result = result / 2;
39-
if (result == 4) {
40-
return 2;
41-
}
42-
}
43-
return 1;
44-
}
45-
46-
int get_three() {
47-
int result = 128;
48-
while(true) {
49-
result = result / 2;
50-
if (result == 32) {
51-
return 3;
52-
}
53-
}
54-
return 2;
55-
}
56-
5710
int main(int argc, char *argv[])
5811
{
59-
in = malloc(4);
60-
out = malloc(4);
61-
in[0] = '0';
62-
in[1] = 'x';
63-
in[2] = 'a';
64-
in[3] = '\n';
65-
int zero = get_zero();
66-
int one = get_one();
67-
int two = get_two();
68-
int three = get_three();
69-
out[zero] = in[zero];
70-
out[one] = in[one];
71-
out[two] = in[two];
72-
out[three] = in[three];
73-
syscall(__NR_write, 1, out, 4);
74-
return 0;
12+
char message[3];
13+
message[0] = 0x41;
14+
message[1] = 0x55;
15+
message[2] = 0x41;
16+
printf("%.*s", 3, message);
17+
fflush(stdout);
18+
- syscall(__NR_exit, 0);
19+
//return 0;
7520
}

group3/.gdbinit

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

group3/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
default:
2-
$(CC) group3.s -c -g
3-
$(LD) -o group3 group3.o -e main
2+
$(CC) group3.s -ggdb -o group3
43

54
clean:
65
@rm -f group3.o group3

group3/group3.s

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ get_memory:
5151
pop_all
5252
return
5353

54+
sflush:
55+
enter
56+
push_all
57+
xor %eax, %eax
58+
mov stdout(%rip), %rdi
59+
syscall
60+
pop_all
61+
return
62+
5463
exit:
5564
enter
5665
push_all
@@ -222,6 +231,7 @@ main:
222231
mov %rax, linebuf(%rip)
223232
call init_file
224233
call print_lines
234+
call sflush
225235

226236
call close
227237
call munmap

print_int/.gdbinit

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ lay asm
33
define hook-quit
44
set confirm off
55
end
6-
break string_compare
6+
break write_string
7+
display $rsi

print_int/print_int.s

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@
8686
pop %rsi
8787
.endm
8888

89+
.data
90+
format: .asciz "%.*s"
91+
8992
.text
9093
.globl main
9194

@@ -133,11 +136,11 @@ write_string:
133136
enter
134137
push_all
135138
push %rax
136-
mov 24(%rbp), %rsi # param: address
137-
mov 16(%rbp), %rdx # param: size
138-
mov $WRITE, %rax
139-
mov $STDOUT, %rdi
140-
syscall
139+
lea format(%rip), %rdi
140+
mov 24(%rbp), %rdx # param: address
141+
mov 16(%rbp), %rsi # param: size
142+
mov $2, %al # number of vector arguments
143+
call printf
141144
pop %rax
142145
pop_all
143146
return

0 commit comments

Comments
 (0)