summaryrefslogtreecommitdiff
path: root/PGU/CHAP6_7/read_write.s
diff options
context:
space:
mode:
Diffstat (limited to 'PGU/CHAP6_7/read_write.s')
-rw-r--r--PGU/CHAP6_7/read_write.s62
1 files changed, 62 insertions, 0 deletions
diff --git a/PGU/CHAP6_7/read_write.s b/PGU/CHAP6_7/read_write.s
new file mode 100644
index 0000000..d574013
--- /dev/null
+++ b/PGU/CHAP6_7/read_write.s
@@ -0,0 +1,62 @@
+.include "record-def.s"
+.include "linux.s"
+
+# Read function
+# Reads a record from the file descriptor
+# and writes it into the buffer passed
+
+# STACK VARS - Used for both read and write functions. They don't share the
+# location, but the arguments are passed in the same position
+# for both, so, no need to create different location vars.
+
+.equ ST_BUFFER, 16 # Ret address is at %rsp+8
+.equ ST_FILEDES, 24
+
+.section .text
+
+.globl read_record
+.type read_record, @function
+
+read_record:
+ pushq %rbp
+ movq %rsp, %rbp
+
+ # READ A RECORD
+ pushq %rdi
+ movq ST_FILEDES(%rbp), %rdi
+ movq ST_BUFFER(%rbp), %rsi
+ movq $RECORD_SIZE, %rdx
+ movq $SYS_READ, %rax
+ syscall
+
+ # NOTE: %rax has the return value, which we will give back
+ # to our caller
+
+ popq %rdi
+
+ movq %rbp, %rsp
+ popq %rbp
+ ret
+
+.globl write_record
+.type write_record, @function
+
+write_record:
+ pushq %rbp
+ movq %rsp, %rbp
+
+ # WRITE A RECORD
+ pushq %rdi
+ movq ST_FILEDES(%rbp), %rdi
+ movq ST_BUFFER(%rbp), %rsi
+ movq $RECORD_SIZE, %rdx
+ movq $SYS_WRITE, %rax
+ syscall
+
+ # NOTE: %rax has the return value, which we will give back
+ # to our caller
+ popq %rdi
+
+ movq %rbp, %rsp
+ popq %rbp
+ ret