88.equ MMAP, 9
99.equ MUNMAP, 11
1010.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
1117
12- .data
18+ .data
1319
1420fh:
1521 .quad 0
1622buffer:
1723 .quad 0
24+ siz:
25+ .quad 0
1826st:
1927 .zero 1024
2028
21- .section .rodata
29+ .section .rodata
2230
2331file:
2432 .asciz "data.txt"
2533
26- .text
27-
28- .globl _start
34+ .text
35+ .globl _start
2936
3037_mmap:
3138 movq $MMAP, %rax
32- movq $0 , %rdi #addr
33- movq $3 , %rdx #prot r=1 w=2
34- leaq st, %rbx
35- movq 48 (%rbx ), %rsi #len
36- movq $-1 , %r8 #fd
37- movq $0 , %r9 #offset
38- movq $34 , %r10 #flags map_private=0x02, map_anonymous=0x20
39+ movq $0 , %rdi #addr
40+ movq $3 , %rdx #prot r=1 w=2
41+ movq siz(%rip ), %rsi #len
42+ movq $-1 , %r8 #fd
43+ movq $0 , %r9 #offset
44+ movq $34 , %r10 #flags map_private=0x02, map_anonymous=0x20
3945 syscall
4046 movq %rax , buffer(%rip ) #store buffer
4147 ret
@@ -48,27 +54,27 @@ _exit:
4854
4955_print:
5056 movq $WRITE, %rax
51- pushq %rdi #print contents of rdi
57+ pushq %rdi #print contents of rdi
5258 movq $STDOUT, %rdi
53- pushq %rsp #push leaves RSP pointing to the data that was pushed
54- popq %rsi #copy RSP to RSI
55- movq $1 , %rdx #size
59+ pushq %rsp #push leaves RSP pointing to the data that was pushed
60+ popq %rsi #copy RSP to RSI
61+ movq $1 , %rdx #size
5662 syscall
5763 popq %rdi
5864 ret
5965
6066_print_ouch:
61- movq $0x4f , %rdi
67+ movq $CHAR_O , %rdi
6268 call _print
63- movq $0x55 , %rdi
69+ movq $CHAR_U , %rdi
6470 call _print
65- movq $0x43 , %rdi
71+ movq $CHAR_C , %rdi
6672 call _print
67- movq $0x48 , %rdi
73+ movq $CHAR_H , %rdi
6874 call _print
69- movq $0x21 , %rdi
75+ movq $BANG , %rdi
7076 call _print
71- movq $0xa , %rdi
77+ movq $NEWLINE , %rdi
7278 call _print
7379 ret
7480
@@ -86,23 +92,23 @@ _stat:
8692 movq fh(%rip ), %rdi #load fh
8793 leaq st, %rsi #into st
8894 syscall
95+ movq 48 (%rsi ), %rbx #store size
96+ movq %rbx , siz(%rip )
8997 ret
9098
9199_read:
92100 movq $READ, %rax
93101 movq fh(%rip ), %rdi #int fd
94102 movq buffer(%rip ), %rsi #char *buf
95- leaq st, %rbx
96- movq 48 (%rbx ), %rdx #len
103+ movq siz(%rip ), %rdx #len
97104 syscall
98105 ret
99106
100107_write:
101108 movq $WRITE, %rax
102109 movq $STDOUT, %rdi #int fd
103110 movq buffer(%rip ), %rsi #char *buf
104- leaq st, %rbx
105- movq 48 (%rbx ), %rdx #len
111+ movq siz(%rip ), %rdx #len
106112 syscall
107113 ret
108114
@@ -115,8 +121,7 @@ _close:
115121_munmap:
116122 movq $MUNMAP, %rax #munmap
117123 leaq buffer, %rdi #buffer
118- leaq st, %rbx
119- movq 48 (%rbx ), %rsi #len
124+ movq siz(%rip ), %rsi #len
120125 syscall
121126 ret
122127
@@ -132,4 +137,3 @@ _start:
132137 call _close
133138 call _munmap
134139 call _exit
135- leave
0 commit comments