Skip to content

Commit a54e8fe

Browse files
committed
some refactoring
1 parent 53e8ddf commit a54e8fe

File tree

2 files changed

+73
-93
lines changed

2 files changed

+73
-93
lines changed

group3/group3.s

Lines changed: 68 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,33 @@
77
.equ EXIT, 60
88
.equ MMAP, 9
99
.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
10+
.equ OFFSET_SIZE, 48 # struct stat
11+
12+
.macro enter
13+
push %rbp
14+
mov %rsp, %rbp
1915
push %rcx
16+
push %r8
17+
push %r9
18+
push %r10
2019
push %r11
20+
push %rax
21+
push %rdi
22+
push %rdx
2123
.endm
2224

23-
.macro sys_leave
25+
.macro return
26+
pop %rdx
27+
pop %rdi
28+
pop %rax
2429
pop %r11
30+
pop %r10
31+
pop %r9
32+
pop %r8
2533
pop %rcx
26-
.endm
27-
28-
.macro frame_enter
29-
push %rbp
30-
mov %rsp, %rbp
31-
.endm
32-
33-
.macro frame_leave
3434
mov %rbp, %rsp
3535
pop %rbp
36+
ret
3637
.endm
3738

3839
.data
@@ -55,100 +56,79 @@ file:
5556
.globl main
5657

5758
mmap:
58-
movq $MMAP, %rax
59-
movq $0, %rdi #addr
60-
movq $3, %rdx #prot r=1 w=2
61-
movq siz(%rip), %rsi #len
62-
movq $-1, %r8 #fd
63-
movq $0, %r9 #offset
64-
movq $34, %r10 #flags map_private=0x02, map_anonymous=0x20
59+
enter
60+
mov $MMAP, %rax
61+
mov $0, %rdi #addr
62+
mov $3, %rdx #prot r=1 w=2
63+
mov siz(%rip), %rsi #len
64+
mov $-1, %r8 #fd
65+
mov $0, %r9 #offset
66+
mov $34, %r10 #flags map_private=0x02, map_anonymous=0x20
6567
syscall
66-
movq %rax, buffer(%rip) #store buffer
67-
ret
68+
mov %rax, buffer(%rip)
69+
return
6870

6971
exit:
70-
movq $EXIT, %rax
71-
movq $0, %rdi
72-
syscall
73-
ret
74-
75-
print:
76-
movq $WRITE, %rax
77-
pushq %rdi #print contents of rdi
78-
movq $STDOUT, %rdi
79-
pushq %rsp #push leaves RSP pointing to the data that was pushed
80-
popq %rsi #copy RSP to RSI
81-
movq $1, %rdx #size
72+
enter
73+
mov $EXIT, %rax
74+
mov $0, %rdi
8275
syscall
83-
popq %rdi
84-
ret
85-
86-
print_ouch:
87-
movq $CHAR_O, %rdi
88-
call print
89-
movq $CHAR_U, %rdi
90-
call print
91-
movq $CHAR_C, %rdi
92-
call print
93-
movq $CHAR_H, %rdi
94-
call print
95-
movq $BANG, %rdi
96-
call print
97-
movq $NEWLINE, %rdi
98-
call print
99-
ret
76+
return
10077

10178
open:
102-
movq $OPEN, %rax
103-
movq $file, %rdi #filename
104-
movq $0, %rsi #readonly
105-
movq $0644, %rdx #mode
79+
enter
80+
mov $OPEN, %rax
81+
mov $file, %rdi
82+
mov $0, %rsi
83+
mov $0644, %rdx
10684
syscall
107-
movq %rax, fh(%rip) #store fh
108-
ret
85+
mov %rax, fh(%rip)
86+
return
10987

11088
stat:
111-
movq $FSTAT, %rax
112-
movq fh(%rip), %rdi #load fh
113-
leaq st, %rsi #into st
89+
enter
90+
mov $FSTAT, %rax
91+
mov fh(%rip), %rdi
92+
lea st, %rsi
11493
syscall
115-
movq 48(%rsi), %rbx #store size
116-
movq %rbx, siz(%rip)
117-
ret
94+
mov 48(%rsi), %rbx
95+
mov %rbx, siz(%rip)
96+
return
11897

11998
read:
120-
movq $READ, %rax
121-
movq fh(%rip), %rdi #int fd
122-
movq buffer(%rip), %rsi #char *buf
123-
movq siz(%rip), %rdx #len
99+
enter
100+
mov $READ, %rax
101+
mov fh(%rip), %rdi
102+
mov buffer(%rip), %rsi
103+
mov siz(%rip), %rdx
124104
syscall
125-
ret
105+
return
126106

127107
write:
128-
movq $WRITE, %rax
129-
movq $STDOUT, %rdi #int fd
130-
movq buffer(%rip), %rsi #char *buf
131-
movq siz(%rip), %rdx #len
108+
enter
109+
mov $WRITE, %rax
110+
mov $STDOUT, %rdi
111+
mov buffer(%rip), %rsi
112+
mov siz(%rip), %rdx
132113
syscall
133-
ret
114+
return
134115

135116
close:
136-
movq $CLOSE, %rax #close
137-
leaq fh, %rdi #fh
117+
enter
118+
mov $CLOSE, %rax
119+
lea fh, %rdi
138120
syscall
139-
ret
121+
return
140122

141123
munmap:
142-
movq $MUNMAP, %rax #munmap
143-
leaq buffer, %rdi #buffer
144-
movq siz(%rip), %rsi #len
124+
enter
125+
mov $MUNMAP, %rax
126+
lea buffer, %rdi
127+
mov siz(%rip), %rsi
145128
syscall
146-
ret
129+
return
147130

148131
main:
149-
pushq %rbp
150-
movq %rsp, %rbp
151-
152132
call open
153133
call stat
154134
call mmap

print_int/print_int.s

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@
2828
.globl main
2929

3030
print_char: # debugging
31-
push %rbp
32-
mov %rsp, %rbp
31+
frame_enter
32+
sys_enter
3333
push %rax
3434
push %rcx
3535
push %rdx
3636
push %rsi
3737
push %rdi
3838
mov 16(%rbp), %rax
3939
movb $0x3e, -128(%rbp)
40-
mov %rax, -127(%rbp)
40+
movb %al, -127(%rbp)
4141
movb $0x3c, -126(%rbp)
4242
movb $0xa, -125(%rbp)
4343
lea -128(%rbp), %rsi
@@ -50,8 +50,8 @@ print_char: # debugging
5050
pop %rdx
5151
pop %rcx
5252
pop %rax
53-
mov %rbp, %rsp
54-
pop %rbp
53+
sys_leave
54+
frame_leave
5555
ret
5656

5757
# char *rsi, int rdx

0 commit comments

Comments
 (0)