summaryrefslogtreecommitdiff
path: root/PGU/CHAP6_7/write_records.s
blob: e52d7e00b1607cfd9b76026a1a893ef36bfb71a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
.include "linux.s"
.include "record-def.s"

.section .data

# Constant data of the records we want to write
# Each data item is padded to the proper length
# With null (i.e 0) bytes

# .rept is used to pad each item. .rept tells the assembler to repeat the
# section between .rept and .endr the number of times specified.
# This is used in this program to add extra null chars at the end of each field
# to fill the record up to the record size.

# START OF RECORDS
record1:
	# First name
	.ascii "Fredrick\0"
	.rept 31 # Padding to 40 bytes
	.byte 0
	.endr

	# Last name
	.ascii "Bardlet\0"
	.rept 31 # Padding to 40 bytes
	.byte 0
	.endr

	# Address
	.ascii "4242 S Prairie\nTulsa, OK 55555\0"
	.rept 209 # Padding to 240 bytes
	.byte 0
	.endr

	# Age
	.long 45 # Field is 8 bytes long (1 word)

record2:
	# First name
	.ascii "Marilyn\0"
	.rept 32 # Padding to 40 bytes
	.byte 0
	.endr

	# Last name
	.ascii "Taylor\0"
	.rept 33 # Padding to 40 bytes
	.byte 0
	.endr

	# Address
	.ascii "2224 S Johannan St\nChicago, IL 12345\0"
	.rept 203 # Padding to 240 bytes
	.byte 0
	.endr

	# Age
	.long 29 # Field is 8 bytes long (1 word)

record3:
	# First name
	.ascii "Derrick\0"
	.rept 32 # Padding to 40 bytes
	.byte 0
	.endr

	# Last name
	.ascii "McIntire\0"
	.rept 31 # Padding to 40 bytes
	.byte 0
	.endr

	# Address
	.ascii "500 W Oakland\nSan Diego, CA 54321\0"
	.rept 206 # Padding to 240 bytes
	.byte 0
	.endr

	# Age
	.long 36 # Field is 8 bytes long (1 word)

# END OF RECORDS


.section .text
# File we'll be writing to
file_name:
	.ascii "test.dat\0"

.equ ST_FILE_DESCRIPTOR, -8

.globl _start
.global write_record

_start:
	movq %rsp, %rbp
	subq $8, %rsp	# Allocate space in stack for FD

	# Open file
	movq $SYS_OPEN, %rax
	movq $file_name, %rdi
	movq $0101, %rsi	# Open for writing, create if it doesn't exist
	movq $0666, %rdx
	syscall

	# Store FD:
	movq %rax, ST_FILE_DESCRIPTOR(%rbp)

	# Write first record
	pushq ST_FILE_DESCRIPTOR(%rbp)
	pushq $record1
	call write_record
	addq $16, %rsp		#cleanup stack

	# Write second record
	pushq ST_FILE_DESCRIPTOR(%rbp)
	pushq $record2
	call write_record
	addq $16, %rsp

	# Write third record
	pushq ST_FILE_DESCRIPTOR(%rbp)
	pushq $record3
	call write_record
	addq $16, %rsp

	# Close FD
	movq $SYS_CLOSE, %rax
	movq ST_FILE_DESCRIPTOR(%rbp), %rdi
	syscall

	# We are done, just exit
	movq $SYS_EXIT, %rax
	movq $0, %rdi
	syscall