program TestFileIO {
section module {
io
}
section data {
int rax = 0
int rbx = 0
int rcx = 0
int rdx = 0
string str_0 = "test_output.txt"
string str_1 = "w"
string str_2 = "Hello from Pascal file I/O!"
string str_3 = "\n"
string str_4 = "Line two"
string str_5 = "Line three"
string str_6 = "File written successfully."
string str_7 = "r"
string str_8 = "Read: "
string str_9 = "a"
string str_10 = "Appended line"
string str_11 = "After append: "
string str_12 = "All file I/O tests completed."
string fmt_str = "%s "
string newline = "\n"
ptr f = null
ptr f_filename = null
ptr line = null
}
section code {
start:
mov f_filename, str_0
invoke fopen, f_filename, str_1
return rbx
mov f, rbx
invoke fprintf, f, fmt_str, str_2
invoke fputs, str_3, f
invoke fprintf, f, fmt_str, str_4
invoke fputs, str_3, f
invoke fprintf, f, fmt_str, str_5
invoke fputs, str_3, f
invoke fclose, f
print fmt_str, str_6
print newline
mov f_filename, str_0
invoke fopen, f_filename, str_7
return rbx
mov f, rbx
WHILE_0:
invoke feof, f
return rbx
not rbx
cmp rbx, 0
je ENDWHILE_1
invoke mxvm_fgets, f
return rcx
mov line, rcx
print fmt_str, str_8
print fmt_str, line
print newline
jmp WHILE_0
ENDWHILE_1:
invoke fclose, f
mov f_filename, str_0
invoke fopen, f_filename, str_9
return rcx
mov f, rcx
invoke fprintf, f, fmt_str, str_10
invoke fputs, str_3, f
invoke fclose, f
mov f_filename, str_0
invoke fopen, f_filename, str_7
return rcx
mov f, rcx
WHILE_2:
invoke feof, f
return rcx
not rcx
cmp rcx, 0
je ENDWHILE_3
invoke mxvm_fgets, f
return rdx
mov line, rdx
print fmt_str, str_11
print fmt_str, line
print newline
jmp WHILE_2
ENDWHILE_3:
invoke fclose, f
print fmt_str, str_12
print newline
done
}
}