File tree Expand file tree Collapse file tree 6 files changed +63
-26
lines changed Expand file tree Collapse file tree 6 files changed +63
-26
lines changed Original file line number Diff line number Diff line change 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```
Original file line number Diff line number Diff line change 1+ CFLAGS += -x
2+ CFLAGS += assembler-with-cpp
3+
14default :
2- $(CC ) group3.s -g -o group3 -e main
5+ $(CC ) $( CFLAGS ) group3.s -g -o group3 -e main
36
47clean :
58 @rm -f group3.o group3
Original file line number Diff line number Diff line change 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
4035get_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
6358open_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:
7570stat_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:
8782init_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:
144139close:
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:
153148munmap:
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:])
175170print_lines:
176171 enter
177172 sub $128 , %rsp
Original file line number Diff line number Diff line change 1+ CFLAGS += -x
2+ CFLAGS += assembler-with-cpp
3+
14print_int :
2- $(CC ) main.s -o print_int
5+ $(CC ) $( CFLAGS ) main.s -o print_int
36
47check : print_int
58 ./print_int | md5sum | grep -q ^3cca2fa && printf " \033[1;32m[OK]\033[0m\n"
69
710write :
8- $(CC ) write.s -o write
11+ $(CC ) $( CFLAGS ) write.s -o write
912
1013check_write : write
1114 ./write | grep -q ' ^ABC$$' && printf " \033[1;32m[OK]\033[0m\n"
1215
1316strcmp :
14- $(CC ) strcmp.s -o strcmp
17+ $(CC ) $( CFLAGS ) strcmp.s -o strcmp
1518
1619check_strcmp : strcmp
1720 ./strcmp | md5sum | grep -q ^00350de && printf " \033[1;32m[OK]\033[0m\n"
Original file line number Diff line number Diff line change 1+ #include <sys/syscall.h>
12 .text
23 .globl main
34
4445 call exit
4546
4647exit:
47- movq $EXIT , %rax
48- movq $0 , %rdi
48+ mov $SYS_exit_group , %rax
49+ mov $0 , %rdi
4950 syscall
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments