Skip to content

Commit f4721cb

Browse files
committed
use preprocessor
1 parent f0ce47f commit f4721cb

File tree

6 files changed

+63
-26
lines changed

6 files changed

+63
-26
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+
[Including header in assembly](https://stackoverflow.com/questions/60192138/including-header-file-in-assembly-file)
14+
1315
```
1416
/usr/include/asm-generic/fcntl.h
1517
```

group3/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
CFLAGS += -x
2+
CFLAGS += assembler-with-cpp
3+
14
default:
2-
$(CC) group3.s -g -o group3 -e main
5+
$(CC) $(CFLAGS) group3.s -g -o group3 -e main
36

47
clean:
58
@rm -f group3.o group3

group3/group3.s

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
.equ READ, 0
2-
.equ CLOSE, 3
3-
.equ OPEN, 2
4-
.equ FSTAT, 5
5-
.equ MMAP, 9
6-
.equ MUNMAP, 11
1+
#include <sys/syscall.h>
72
.equ OFFSET_SIZE, 48 # struct stat
83

94
.data
@@ -40,7 +35,7 @@ file:
4035
get_memory:
4136
enter
4237
push_all
43-
mov $MMAP, %rax
38+
mov $SYS_mmap, %rax
4439
mov $0, %rdi #addr
4540
mov $3, %rdx #prot r=1 w=2
4641
mov file_size(%rip), %rsi #len
@@ -63,7 +58,7 @@ exit:
6358
open_file:
6459
enter
6560
push_all
66-
mov $OPEN, %rax
61+
mov $SYS_open, %rax
6762
mov $file, %rdi
6863
mov $0, %rsi
6964
mov $0644, %rdx
@@ -75,7 +70,7 @@ open_file:
7570
stat_file:
7671
enter
7772
push_all
78-
mov $FSTAT, %rax
73+
mov $SYS_fstat, %rax
7974
mov fh(%rip), %rdi
8075
lea st, %rsi
8176
syscall
@@ -87,7 +82,7 @@ stat_file:
8782
init_file:
8883
enter
8984
push_all
90-
mov $READ, %rax
85+
mov $SYS_read, %rax
9186
mov fh(%rip), %rdi
9287
mov inbuf(%rip), %rsi
9388
mov file_size(%rip), %rdx
@@ -144,7 +139,7 @@ read_line_done:
144139
close:
145140
enter
146141
push_all
147-
mov $CLOSE, %rax
142+
mov $SYS_close, %rax
148143
mov fh, %rdi
149144
syscall
150145
pop_all
@@ -153,25 +148,25 @@ close:
153148
munmap:
154149
enter
155150
push_all
156-
mov $MUNMAP, %rax
151+
mov $SYS_munmap, %rax
157152
mov inbuf, %rdi
158153
mov file_size(%rip), %rsi
159154
syscall
160-
mov $MUNMAP, %rax
155+
mov $SYS_munmap, %rax
161156
mov out, %rdi
162157
mov file_size(%rip), %rsi
163158
syscall
164159
pop_all
165160
return
166161

167162

168-
# tmp = '___'
169-
# with open('data.txt') as file:
170-
# while line := file.readline():
171-
# if not line.startswith(tmp):
172-
# print(line[:3])
173-
# tmp = line[:3]
174-
# print('\t' + line.rstrip()[3:])
163+
#. tmp = '___'
164+
#. with open('data.txt') as file:
165+
#. while line := file.readline():
166+
#. if not line.startswith(tmp):
167+
#. print(line[:3])
168+
#. tmp = line[:3]
169+
#. print('\t' + line.rstrip()[3:])
175170
print_lines:
176171
enter
177172
sub $128, %rsp

print_int/Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
CFLAGS += -x
2+
CFLAGS += assembler-with-cpp
3+
14
print_int:
2-
$(CC) main.s -o print_int
5+
$(CC) $(CFLAGS) main.s -o print_int
36

47
check: print_int
58
./print_int | md5sum | grep -q ^3cca2fa && printf "\033[1;32m[OK]\033[0m\n"
69

710
write:
8-
$(CC) write.s -o write
11+
$(CC) $(CFLAGS) write.s -o write
912

1013
check_write: write
1114
./write | grep -q '^ABC$$' && printf "\033[1;32m[OK]\033[0m\n"
1215

1316
strcmp:
14-
$(CC) strcmp.s -o strcmp
17+
$(CC) $(CFLAGS) strcmp.s -o strcmp
1518

1619
check_strcmp: strcmp
1720
./strcmp | md5sum | grep -q ^00350de && printf "\033[1;32m[OK]\033[0m\n"

print_int/main.s

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <sys/syscall.h>
12
.text
23
.globl main
34

@@ -44,6 +45,6 @@ main:
4445
call exit
4546

4647
exit:
47-
movq $EXIT, %rax
48-
movq $0, %rdi
48+
mov $SYS_exit_group, %rax
49+
mov $0, %rdi
4950
syscall

print_int/print_int.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
#include <unistd.h>
4+
#include <sys/syscall.h>
5+
#include <sys/types.h>
6+
7+
const char *format = "0x%.*s\n";
8+
9+
void print_int(uint64_t n)
10+
{
11+
char tmp[16], output[16];
12+
uint8_t count = 0;
13+
do {
14+
char c = 0xf & n;
15+
c += 0x30; // c now contains ascii "0"-"9"
16+
if (c > 0x39) {
17+
c += 0x27; // adjust for ascii "a"-"f"
18+
}
19+
tmp[count++] = c;
20+
n = n >> 4;
21+
} while(n != 0);
22+
for (uint8_t i = 0; i < count; i++) {
23+
output[i] = tmp[count - i - 1];
24+
}
25+
printf(format, count, output);
26+
}
27+
28+
int main(int argc, char *argv[])
29+
{
30+
print_int(0x9084a412);
31+
print_int(0);
32+
return 0;
33+
}

0 commit comments

Comments
 (0)