So I need to write to a file with NASM, however while I can generate the file I'm having trouble getting it to write in it. I haven't had any segmentation faults or anything of the sort, nonetheless, after my whole program ends its execution, nothing is written to the file. Here's how my program works, it asks for a file name and an amount of random characters to generate. Afterwards, it generates the file, then the random number, executes a loop, and within that loop it is supposed to write each letter to the file. However, my issue is that it doesn't write anything at all, it compiles well and all, but nothing is ever written to the file.
I suspect this is probably because of the way I have allocated memory, I'm not at all really sure about how to do that correctly. Regardless, here is my code:
%include "io.mac"
.DATA
filename_msg db 'Enter the file name: ', 0
number_prompt_msg db 'Enter the number of bases: ',0 ;asks for the number of bases to be used
finish_msg db 'Operation completed, DNA file generated',0 ;tells the user when the file is complete
error_msg db 'Operation failed, please try again', 10
file_error_msg db 'Write operation error',10
base_A db 'A',0
base_C db 'C',0
base_G db 'G',0
base_T db 'T',0
base_length equ $ - base_A
;----------------------------------------------------------------------------------------------------
.UDATA
number_of_bases rest 1 ;defined by the user
random_number resd 1
filename: resd 20 ;defined by the user
base resb 1
file_descriptor resd 1 ;used to generate the file
loop_number resd 1
;-------------------------------------------------------------------
;start of code, and message prompts for the user
.CODE
.STARTUP
;asks user for filename
ask_details:
PutStr filename_msg
GetStr filename, 300
;asks user for the number of bases
PutStr number_prompt_msg
GetLInt [number_of_bases]
;------------------------------------------------------------
;file creation
mov EAX, 8 ;creates the file
mov EBX, filename
mov ECX, 644O ;octal instruction
int 80h ;kernel interrupt
cmp EAX,0 ;throws error if something is amiss
jbe error
mov [file_descriptor],EAX
mov ECX,[number_of_bases]
;-------------------------------------------------------------
;randomization of base numbers
writing_loop:
rdtsc
mov EAX, EDX
add EAX, ECX
mov EDX, 0
div ECX
mov EDX, 0
mov EBX, 4
div EBX
mov [random_number], EDX
mov EDX, 0
mov EAX,[random_number]
cmp EAX,0
je assignment_A
cmp EAX,1
je assignment_C
cmp EAX,2
je assignment_G
cmp EAX,3
je assignment_T
write_char:
mov [base],EBX ;moves the letter to base for later use
dec ECX ;subtracts 1 to ECX to use in loop
mov [loop_number],ECX ;stores loop because ECX is required to write to file
PutStr base
;-------------------------------------------------------------------
;write to file
;also opens the file
mov EAX,4
mov EBX,[file_descriptor]
mov ECX, base
mov EDX, base_length
int 80h
cmp EAX, 0
jbe file_write_error
;close file
mov EAX,1
mov EBX, 0
;continue loop
mov ECX,[loop_number]
cmp ECX,0
jne writing_loop
jmp done
;------------------------------------------------------------
;file generation error message
error:
PutStr error_msg
jmp ask_details
;------------------------------------------------------------
;file write error
file_write_error:
PutStr file_error_msg
jmp done
;------------------------------------------------------------
done:
nwln
.EXIT
;------------------------------------------------------------
;assignments
assignment_A:
mov EBX, [base_A]
jmp write_char
assignment_C:
mov EBX, [base_C]
jmp write_char
assignment_T:
mov EBX, [base_T]
jmp write_char
assignment_G:
mov EBX, [base_G]
jmp write_char
Most of this I had to figure out on my own, I did not get much help from my teacher on this matter, I'm not sure what is really going on and why nothing is being written. When I print the output on the console it shows every character that has been selected, so the letters are working. I just need to get this done, and I'm finished with the hard part.
This is the relevant part to writing, I'm sure I put everything as it needs to be, but I'm not sure.
;write to file
;also opens the file
mov EAX,4
mov EBX,[file_descriptor]
mov ECX, base
mov EDX, base_length
int 80h
cmp EAX, 0
jbe file_write_error
;close file
mov EAX,1
mov EBX, 0
EDIT: This is what I get if I assign more memory, so this is definitely something I'm doing wrong with my memory allocations.
T 20
.txt
G 20
.txt
C 20
.txt
G 20
.txt
T 20
.txt
C 20
.txt
T
20
.txt
C 20
.txt
T 20
.txt
T
20
.txt
C 20
.txt
C 20
.txt
G 20
.txt
G 20
.txt
C 20
.txt
C 20
.txt
T 20
.txt
C 20
.txt
C 20
.txt
G 20
.txt
Empty spaces are supposed to be null.
EDIT 2: I'm working with Linux on this.