Skip to content

Commit 53e8ddf

Browse files
committed
define some macros
1 parent 7e3cddc commit 53e8ddf

File tree

5 files changed

+150
-90
lines changed

5 files changed

+150
-90
lines changed

cat/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
default:
22
$(CC) cat.s -c -g
3-
$(LD) -o cat cat.o
3+
$(LD) -o cat cat.o -e main
44

55
check:
66
./cat | md5sum | grep -q ^06aa985f3 && printf "\033[1;32m[OK]\033[0m\n"

cat/cat.s

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
1-
.equ READ, 0
2-
.equ STDOUT, 1
3-
.equ CLOSE, 3
4-
.equ WRITE, 1
5-
.equ OPEN, 2
6-
.equ FSTAT, 5
7-
.equ EXIT, 60
8-
.equ MMAP, 9
9-
.equ MUNMAP, 11
10-
.equ OFFSET_SIZE, 48
11-
.equ CHAR_O, 0x4f
12-
.equ CHAR_U, 0x55
13-
.equ CHAR_C, 0x43
14-
.equ CHAR_H, 0x48
15-
.equ BANG, 0x21
16-
.equ NEWLINE, 0xa
1+
.equ READ, 0
2+
.equ STDOUT, 1
3+
.equ CLOSE, 3
4+
.equ WRITE, 1
5+
.equ OPEN, 2
6+
.equ FSTAT, 5
7+
.equ EXIT, 60
8+
.equ MMAP, 9
9+
.equ MUNMAP, 11
10+
.equ OFFSET_SIZE, 48
11+
.equ CHAR_O, 0x4f
12+
.equ CHAR_U, 0x55
13+
.equ CHAR_C, 0x43
14+
.equ CHAR_H, 0x48
15+
.equ BANG, 0x21
16+
.equ NEWLINE, 0xa
17+
18+
.macro sys_enter
19+
push %rcx
20+
push %r11
21+
.endm
22+
23+
.macro sys_leave
24+
pop %r11
25+
pop %rcx
26+
.endm
27+
28+
.macro frame_enter
29+
push %rbp
30+
mov %rsp, %rbp
31+
.endm
32+
33+
.macro frame_leave
34+
mov %rbp, %rsp
35+
pop %rbp
36+
.endm
1737

1838
.data
1939

@@ -32,9 +52,9 @@ file:
3252
.asciz "data.txt"
3353

3454
.text
35-
.globl _start
55+
.globl main
3656

37-
_mmap:
57+
mmap:
3858
movq $MMAP, %rax
3959
movq $0, %rdi #addr
4060
movq $3, %rdx #prot r=1 w=2
@@ -46,13 +66,13 @@ _mmap:
4666
movq %rax, buffer(%rip) #store buffer
4767
ret
4868

49-
_exit:
69+
exit:
5070
movq $EXIT, %rax
5171
movq $0, %rdi
5272
syscall
5373
ret
5474

55-
_print:
75+
print:
5676
movq $WRITE, %rax
5777
pushq %rdi #print contents of rdi
5878
movq $STDOUT, %rdi
@@ -63,22 +83,22 @@ _print:
6383
popq %rdi
6484
ret
6585

66-
_print_ouch:
86+
print_ouch:
6787
movq $CHAR_O, %rdi
68-
call _print
88+
call print
6989
movq $CHAR_U, %rdi
70-
call _print
90+
call print
7191
movq $CHAR_C, %rdi
72-
call _print
92+
call print
7393
movq $CHAR_H, %rdi
74-
call _print
94+
call print
7595
movq $BANG, %rdi
76-
call _print
96+
call print
7797
movq $NEWLINE, %rdi
78-
call _print
98+
call print
7999
ret
80100

81-
_open:
101+
open:
82102
movq $OPEN, %rax
83103
movq $file, %rdi #filename
84104
movq $0, %rsi #readonly
@@ -87,7 +107,7 @@ _open:
87107
movq %rax, fh(%rip) #store fh
88108
ret
89109

90-
_stat:
110+
stat:
91111
movq $FSTAT, %rax
92112
movq fh(%rip), %rdi #load fh
93113
leaq st, %rsi #into st
@@ -96,44 +116,44 @@ _stat:
96116
movq %rbx, siz(%rip)
97117
ret
98118

99-
_read:
119+
read:
100120
movq $READ, %rax
101121
movq fh(%rip), %rdi #int fd
102122
movq buffer(%rip), %rsi #char *buf
103123
movq siz(%rip), %rdx #len
104124
syscall
105125
ret
106126

107-
_write:
127+
write:
108128
movq $WRITE, %rax
109129
movq $STDOUT, %rdi #int fd
110130
movq buffer(%rip), %rsi #char *buf
111131
movq siz(%rip), %rdx #len
112132
syscall
113133
ret
114134

115-
_close:
135+
close:
116136
movq $CLOSE, %rax #close
117137
leaq fh, %rdi #fh
118138
syscall
119139
ret
120140

121-
_munmap:
141+
munmap:
122142
movq $MUNMAP, %rax #munmap
123143
leaq buffer, %rdi #buffer
124144
movq siz(%rip), %rsi #len
125145
syscall
126146
ret
127147

128-
_start:
148+
main:
129149
pushq %rbp
130150
movq %rsp, %rbp
131151

132-
call _open
133-
call _stat
134-
call _mmap
135-
call _read
136-
call _write
137-
call _close
138-
call _munmap
139-
call _exit
152+
call open
153+
call stat
154+
call mmap
155+
call read
156+
call write
157+
call close
158+
call munmap
159+
call exit

group3/Makefile

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

55
clean:
66
@rm -f group3.o group3

group3/group3.s

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
1-
.equ READ, 0
2-
.equ STDOUT, 1
3-
.equ CLOSE, 3
4-
.equ WRITE, 1
5-
.equ OPEN, 2
6-
.equ FSTAT, 5
7-
.equ EXIT, 60
8-
.equ MMAP, 9
9-
.equ MUNMAP, 11
10-
.equ OFFSET_SIZE, 48
11-
.equ CHAR_O, 0x4f
12-
.equ CHAR_U, 0x55
13-
.equ CHAR_C, 0x43
14-
.equ CHAR_H, 0x48
15-
.equ BANG, 0x21
16-
.equ NEWLINE, 0xa
1+
.equ READ, 0
2+
.equ STDOUT, 1
3+
.equ CLOSE, 3
4+
.equ WRITE, 1
5+
.equ OPEN, 2
6+
.equ FSTAT, 5
7+
.equ EXIT, 60
8+
.equ MMAP, 9
9+
.equ MUNMAP, 11
10+
.equ OFFSET_SIZE, 48
11+
.equ CHAR_O, 0x4f
12+
.equ CHAR_U, 0x55
13+
.equ CHAR_C, 0x43
14+
.equ CHAR_H, 0x48
15+
.equ BANG, 0x21
16+
.equ NEWLINE, 0xa
17+
18+
.macro sys_enter
19+
push %rcx
20+
push %r11
21+
.endm
22+
23+
.macro sys_leave
24+
pop %r11
25+
pop %rcx
26+
.endm
27+
28+
.macro frame_enter
29+
push %rbp
30+
mov %rsp, %rbp
31+
.endm
32+
33+
.macro frame_leave
34+
mov %rbp, %rsp
35+
pop %rbp
36+
.endm
1737

1838
.data
1939

@@ -32,9 +52,9 @@ file:
3252
.asciz "data.txt"
3353

3454
.text
35-
.globl _start
55+
.globl main
3656

37-
_mmap:
57+
mmap:
3858
movq $MMAP, %rax
3959
movq $0, %rdi #addr
4060
movq $3, %rdx #prot r=1 w=2
@@ -46,13 +66,13 @@ _mmap:
4666
movq %rax, buffer(%rip) #store buffer
4767
ret
4868

49-
_exit:
69+
exit:
5070
movq $EXIT, %rax
5171
movq $0, %rdi
5272
syscall
5373
ret
5474

55-
_print:
75+
print:
5676
movq $WRITE, %rax
5777
pushq %rdi #print contents of rdi
5878
movq $STDOUT, %rdi
@@ -63,22 +83,22 @@ _print:
6383
popq %rdi
6484
ret
6585

66-
_print_ouch:
86+
print_ouch:
6787
movq $CHAR_O, %rdi
68-
call _print
88+
call print
6989
movq $CHAR_U, %rdi
70-
call _print
90+
call print
7191
movq $CHAR_C, %rdi
72-
call _print
92+
call print
7393
movq $CHAR_H, %rdi
74-
call _print
94+
call print
7595
movq $BANG, %rdi
76-
call _print
96+
call print
7797
movq $NEWLINE, %rdi
78-
call _print
98+
call print
7999
ret
80100

81-
_open:
101+
open:
82102
movq $OPEN, %rax
83103
movq $file, %rdi #filename
84104
movq $0, %rsi #readonly
@@ -87,7 +107,7 @@ _open:
87107
movq %rax, fh(%rip) #store fh
88108
ret
89109

90-
_stat:
110+
stat:
91111
movq $FSTAT, %rax
92112
movq fh(%rip), %rdi #load fh
93113
leaq st, %rsi #into st
@@ -96,44 +116,44 @@ _stat:
96116
movq %rbx, siz(%rip)
97117
ret
98118

99-
_read:
119+
read:
100120
movq $READ, %rax
101121
movq fh(%rip), %rdi #int fd
102122
movq buffer(%rip), %rsi #char *buf
103123
movq siz(%rip), %rdx #len
104124
syscall
105125
ret
106126

107-
_write:
127+
write:
108128
movq $WRITE, %rax
109129
movq $STDOUT, %rdi #int fd
110130
movq buffer(%rip), %rsi #char *buf
111131
movq siz(%rip), %rdx #len
112132
syscall
113133
ret
114134

115-
_close:
135+
close:
116136
movq $CLOSE, %rax #close
117137
leaq fh, %rdi #fh
118138
syscall
119139
ret
120140

121-
_munmap:
141+
munmap:
122142
movq $MUNMAP, %rax #munmap
123143
leaq buffer, %rdi #buffer
124144
movq siz(%rip), %rsi #len
125145
syscall
126146
ret
127147

128-
_start:
148+
main:
129149
pushq %rbp
130150
movq %rsp, %rbp
131151

132-
call _open
133-
call _stat
134-
call _mmap
135-
call _read
136-
call _write
137-
call _close
138-
call _munmap
139-
call _exit
152+
call open
153+
call stat
154+
call mmap
155+
call read
156+
call write
157+
call close
158+
call munmap
159+
call exit

0 commit comments

Comments
 (0)