Skip to content

Commit b96effe

Browse files
authored
chapter 13, problem 3
1 parent 5b390f1 commit b96effe

1 file changed

Lines changed: 185 additions & 0 deletions

File tree

fileWrite.asm

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
section .data
2+
3+
; Standard constants
4+
5+
LF equ 10 ; line feed
6+
NULL equ 0 ; end of string
7+
TRUE equ 1
8+
FALSE equ 0
9+
EXIT_SUCCESS equ 0 ; success code
10+
STDIN equ 0 ; standard input
11+
STDOUT equ 1 ; standard output
12+
STDERR equ 2; standard error
13+
14+
SYS_read equ 0 ; read
15+
SYS_write equ 1 ; write
16+
SYS_open equ 2 ; open
17+
SYS_close equ 3 ; close
18+
SYS_fork equ 57 ; fork
19+
SYS_exit equ 60 ; terminate
20+
SYS_creat equ 85 ; file open/create
21+
SYS_time equ 201 ; get time
22+
23+
O_CREAT equ 0x40
24+
O_TRUNC equ 0x200
25+
O_APPEND equ 0x400
26+
27+
O_RDONLY equ 000000q ; read only
28+
O_WRONLY equ 000001q ; write only
29+
O_RDWR equ 000002q ; read and write
30+
31+
S_IRUSR equ 00400q
32+
S_IWUSR equ 00200q
33+
S_IXUSR equ 00100q
34+
35+
;--------------------------------------------------
36+
; Variables which will be used in main
37+
38+
newLine db LF, NULL
39+
header db LF, "File Write Example.", LF, LF, NULL
40+
fileName db "./password_storage.txt", NULL
41+
password db "mamamieeli!123", NULL
42+
len dq 0
43+
44+
writeDone db "Write Completed", LF, NULL
45+
fileDesc dq 0
46+
errMsgOpen db "Error opening file.", LF, NULL
47+
errMsgWrite db "Error writing to file.", LF, NULL
48+
49+
;--------------------------------------------------
50+
51+
section .text
52+
global _start
53+
_start:
54+
55+
; Display header
56+
57+
mov rdi, fileName
58+
mov rsi, password
59+
call writeToFile
60+
61+
exit:
62+
mov rax, SYS_exit
63+
mov rdi, EXIT_SUCCESS
64+
syscall
65+
66+
67+
writeToFile:
68+
69+
mov r8, rdi ; store address of the file in r8
70+
mov r9, rsi ; store the password in r9
71+
72+
push rdi
73+
mov rdi, header
74+
call printString
75+
pop rdi
76+
77+
; Open file
78+
79+
push rsi
80+
openInputFile:
81+
mov rax, SYS_creat ; file open/create
82+
;mov rdi, fileName ; file name string
83+
mov rsi, S_IRUSR | S_IWUSR | S_IXUSR ; allow read and write
84+
syscall
85+
86+
cmp rax, 0 ; check for success
87+
jl errorOnOpen
88+
89+
mov qword[fileDesc], rax ; save descriptor
90+
91+
; Write to file
92+
pop rsi
93+
push rdi
94+
mov rdi, rsi
95+
call strLen
96+
pop rdi ; get the length of the password in rax
97+
98+
mov rdx, rax ; set parameters for writing in the file
99+
mov rax, SYS_write
100+
mov rdi, qword[fileDesc]
101+
mov rsi, r9
102+
syscall
103+
104+
cmp rax, 0
105+
jl errorOnWrite
106+
107+
mov rdi, writeDone;
108+
call printString
109+
110+
; Close file
111+
mov rax, SYS_close
112+
mov rdi, qword[fileDesc]
113+
syscall
114+
115+
jmp exampleDone
116+
117+
; Error on open
118+
errorOnOpen:
119+
mov rdi, errMsgOpen
120+
call printString
121+
jmp exampleDone
122+
123+
; Error on write
124+
errorOnWrite:
125+
mov rdi, errMsgWrite
126+
call printString
127+
jmp exampleDone
128+
129+
exampleDone:
130+
ret
131+
132+
133+
; print NULL terminated string
134+
135+
global printString
136+
printString:
137+
push rbp
138+
mov rbp, rsp
139+
push rbx
140+
141+
; Count chars in string
142+
143+
mov rbx, rdi
144+
mov rdx, 0
145+
146+
strCountLoop:
147+
cmp byte[rbx], NULL
148+
je strCountDone
149+
inc rdx
150+
inc rbx
151+
jmp strCountLoop
152+
153+
strCountDone:
154+
cmp rdx, 0
155+
je prtDone
156+
157+
mov rax, SYS_write
158+
mov rsi, rdi
159+
mov rdi, STDOUT
160+
syscall
161+
162+
prtDone:
163+
pop rbx
164+
pop rbp
165+
ret
166+
167+
; find the length of a string stored on address in rdi
168+
strLen:
169+
push rbx
170+
push rdx
171+
mov rbx, rdi
172+
mov rdx, 0
173+
174+
CountLoop:
175+
cmp byte[rbx], NULL
176+
je funcStrLenDone
177+
inc rdx
178+
inc rbx
179+
jmp CountLoop
180+
181+
funcStrLenDone:
182+
mov rax, rdx ; store the string length in rax
183+
pop rdx
184+
pop rbx
185+
ret

0 commit comments

Comments
 (0)