reading input file and creating multiple output files to store data
I'm rather new to assembly language my task requires me to take file name and a number as an input(ex: data.txt 100) and separating that file into multiple files that store 100 characters each (so if my data.txt has 520 chars it will create 6 new files names data1 data2... 6th file will have 20 chars.
currently i have an application which takes name of an input file and as it loops creates new files (asking for names of secondary files until loop ends)
the problem i'm facing is that i am unable to make files automatically change name(data1 data2 data3...) also i am unable to make the secondary input to be the ammount of chars to be read(so it is currently set to 1)
any type of wisdom is highly appreciated
my code:
.model small
.stack 100h
.data
handle dw ?
handle2 dw ?
filename db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
filename2 db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
prompt1 db 13,10,"ENTER FILE NAME HERE: $"
prompt2 db 13,10,"ENTER A SECONDARY FILE NAME: $"
mess1 db ' I WIN! $'
buf db ?
.code
main:
mov ax, @data ; set up addressability of data
mov ds, ax
;DISPLAY MESSAGE.
lea dx, prompt1 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;OPEN FILE TO READ FROM IT.
mov ah, 3DH
mov al, 0 ;READ MODE.
mov dx, offset filename + 2
int 21h
mov handle, ax ; save file handle
f1:
;DISPLAY MESSAGE FOR SECOND FILE.
lea dx, prompt2 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename2 ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename2 + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;CREATE FILE.
mov ah, 3ch ; dos service to create file
mov cx, 0 ;READ/WRITE MODE.
mov dx, offset filename2 + 2 ;CHARACTERS START AT BYTE 2.
int 21h
mov handle2, ax ; save file handle
;READ ALL BYTES FROM FIRST FILE AND WRITE THEM TO SECOND FILE.
reading:
;READ ONE BYTE.
mov ah, 3FH
mov bx, handle
mov cx, 1 ;HOW MANY BYTES TO READ.
mov dx, offset buf ;THE BYTE WILL BE STORED HERE.
int 21h ;NUMBER OF BYTES READ RETURNS IN AX.
;CHECK EOF (END OF FILE).
cmp ax, 0 ;IF AX == 0 THEN EOF.
je eof
;WRITE BYTE TO THE SECOND FILE.
mov ah, 40h ; write to
mov bx, handle2 ; file
mov dx, offset buf ; where to find data to write
mov cx, 1 ;LENGTH OF STRING IN CX.
int 21h
jmp f1 ;REPEAT PROCESS.
eof:
;CLOSE FILES.
mov ah, 3Eh ; close file
mov bx, handle ; which file
int 21h
mov ah, 3Eh ; close file
mov bx, handle2 ; which file
int 21h
mov ah, 4ch
int 21h
end main
assembly x86 dos
add a comment |
I'm rather new to assembly language my task requires me to take file name and a number as an input(ex: data.txt 100) and separating that file into multiple files that store 100 characters each (so if my data.txt has 520 chars it will create 6 new files names data1 data2... 6th file will have 20 chars.
currently i have an application which takes name of an input file and as it loops creates new files (asking for names of secondary files until loop ends)
the problem i'm facing is that i am unable to make files automatically change name(data1 data2 data3...) also i am unable to make the secondary input to be the ammount of chars to be read(so it is currently set to 1)
any type of wisdom is highly appreciated
my code:
.model small
.stack 100h
.data
handle dw ?
handle2 dw ?
filename db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
filename2 db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
prompt1 db 13,10,"ENTER FILE NAME HERE: $"
prompt2 db 13,10,"ENTER A SECONDARY FILE NAME: $"
mess1 db ' I WIN! $'
buf db ?
.code
main:
mov ax, @data ; set up addressability of data
mov ds, ax
;DISPLAY MESSAGE.
lea dx, prompt1 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;OPEN FILE TO READ FROM IT.
mov ah, 3DH
mov al, 0 ;READ MODE.
mov dx, offset filename + 2
int 21h
mov handle, ax ; save file handle
f1:
;DISPLAY MESSAGE FOR SECOND FILE.
lea dx, prompt2 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename2 ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename2 + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;CREATE FILE.
mov ah, 3ch ; dos service to create file
mov cx, 0 ;READ/WRITE MODE.
mov dx, offset filename2 + 2 ;CHARACTERS START AT BYTE 2.
int 21h
mov handle2, ax ; save file handle
;READ ALL BYTES FROM FIRST FILE AND WRITE THEM TO SECOND FILE.
reading:
;READ ONE BYTE.
mov ah, 3FH
mov bx, handle
mov cx, 1 ;HOW MANY BYTES TO READ.
mov dx, offset buf ;THE BYTE WILL BE STORED HERE.
int 21h ;NUMBER OF BYTES READ RETURNS IN AX.
;CHECK EOF (END OF FILE).
cmp ax, 0 ;IF AX == 0 THEN EOF.
je eof
;WRITE BYTE TO THE SECOND FILE.
mov ah, 40h ; write to
mov bx, handle2 ; file
mov dx, offset buf ; where to find data to write
mov cx, 1 ;LENGTH OF STRING IN CX.
int 21h
jmp f1 ;REPEAT PROCESS.
eof:
;CLOSE FILES.
mov ah, 3Eh ; close file
mov bx, handle ; which file
int 21h
mov ah, 3Eh ; close file
mov bx, handle2 ; which file
int 21h
mov ah, 4ch
int 21h
end main
assembly x86 dos
1
For this you will probably need string-to-number conversion and back.
– Jester
Nov 26 '18 at 19:06
i was reading quite a bit about that, however i cant seem to implement that as my registers are taken, gona have to think harder to rewrite this...
– woohoos
Nov 26 '18 at 19:18
thank you your comment gave me ideas
– woohoos
Nov 26 '18 at 19:36
2
You registers are not taken (you can reuse them). Even if they were you could just use (local) variables in memory.
– Jester
Nov 26 '18 at 19:36
add a comment |
I'm rather new to assembly language my task requires me to take file name and a number as an input(ex: data.txt 100) and separating that file into multiple files that store 100 characters each (so if my data.txt has 520 chars it will create 6 new files names data1 data2... 6th file will have 20 chars.
currently i have an application which takes name of an input file and as it loops creates new files (asking for names of secondary files until loop ends)
the problem i'm facing is that i am unable to make files automatically change name(data1 data2 data3...) also i am unable to make the secondary input to be the ammount of chars to be read(so it is currently set to 1)
any type of wisdom is highly appreciated
my code:
.model small
.stack 100h
.data
handle dw ?
handle2 dw ?
filename db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
filename2 db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
prompt1 db 13,10,"ENTER FILE NAME HERE: $"
prompt2 db 13,10,"ENTER A SECONDARY FILE NAME: $"
mess1 db ' I WIN! $'
buf db ?
.code
main:
mov ax, @data ; set up addressability of data
mov ds, ax
;DISPLAY MESSAGE.
lea dx, prompt1 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;OPEN FILE TO READ FROM IT.
mov ah, 3DH
mov al, 0 ;READ MODE.
mov dx, offset filename + 2
int 21h
mov handle, ax ; save file handle
f1:
;DISPLAY MESSAGE FOR SECOND FILE.
lea dx, prompt2 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename2 ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename2 + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;CREATE FILE.
mov ah, 3ch ; dos service to create file
mov cx, 0 ;READ/WRITE MODE.
mov dx, offset filename2 + 2 ;CHARACTERS START AT BYTE 2.
int 21h
mov handle2, ax ; save file handle
;READ ALL BYTES FROM FIRST FILE AND WRITE THEM TO SECOND FILE.
reading:
;READ ONE BYTE.
mov ah, 3FH
mov bx, handle
mov cx, 1 ;HOW MANY BYTES TO READ.
mov dx, offset buf ;THE BYTE WILL BE STORED HERE.
int 21h ;NUMBER OF BYTES READ RETURNS IN AX.
;CHECK EOF (END OF FILE).
cmp ax, 0 ;IF AX == 0 THEN EOF.
je eof
;WRITE BYTE TO THE SECOND FILE.
mov ah, 40h ; write to
mov bx, handle2 ; file
mov dx, offset buf ; where to find data to write
mov cx, 1 ;LENGTH OF STRING IN CX.
int 21h
jmp f1 ;REPEAT PROCESS.
eof:
;CLOSE FILES.
mov ah, 3Eh ; close file
mov bx, handle ; which file
int 21h
mov ah, 3Eh ; close file
mov bx, handle2 ; which file
int 21h
mov ah, 4ch
int 21h
end main
assembly x86 dos
I'm rather new to assembly language my task requires me to take file name and a number as an input(ex: data.txt 100) and separating that file into multiple files that store 100 characters each (so if my data.txt has 520 chars it will create 6 new files names data1 data2... 6th file will have 20 chars.
currently i have an application which takes name of an input file and as it loops creates new files (asking for names of secondary files until loop ends)
the problem i'm facing is that i am unable to make files automatically change name(data1 data2 data3...) also i am unable to make the secondary input to be the ammount of chars to be read(so it is currently set to 1)
any type of wisdom is highly appreciated
my code:
.model small
.stack 100h
.data
handle dw ?
handle2 dw ?
filename db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
filename2 db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
prompt1 db 13,10,"ENTER FILE NAME HERE: $"
prompt2 db 13,10,"ENTER A SECONDARY FILE NAME: $"
mess1 db ' I WIN! $'
buf db ?
.code
main:
mov ax, @data ; set up addressability of data
mov ds, ax
;DISPLAY MESSAGE.
lea dx, prompt1 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;OPEN FILE TO READ FROM IT.
mov ah, 3DH
mov al, 0 ;READ MODE.
mov dx, offset filename + 2
int 21h
mov handle, ax ; save file handle
f1:
;DISPLAY MESSAGE FOR SECOND FILE.
lea dx, prompt2 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename2 ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename2 + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;CREATE FILE.
mov ah, 3ch ; dos service to create file
mov cx, 0 ;READ/WRITE MODE.
mov dx, offset filename2 + 2 ;CHARACTERS START AT BYTE 2.
int 21h
mov handle2, ax ; save file handle
;READ ALL BYTES FROM FIRST FILE AND WRITE THEM TO SECOND FILE.
reading:
;READ ONE BYTE.
mov ah, 3FH
mov bx, handle
mov cx, 1 ;HOW MANY BYTES TO READ.
mov dx, offset buf ;THE BYTE WILL BE STORED HERE.
int 21h ;NUMBER OF BYTES READ RETURNS IN AX.
;CHECK EOF (END OF FILE).
cmp ax, 0 ;IF AX == 0 THEN EOF.
je eof
;WRITE BYTE TO THE SECOND FILE.
mov ah, 40h ; write to
mov bx, handle2 ; file
mov dx, offset buf ; where to find data to write
mov cx, 1 ;LENGTH OF STRING IN CX.
int 21h
jmp f1 ;REPEAT PROCESS.
eof:
;CLOSE FILES.
mov ah, 3Eh ; close file
mov bx, handle ; which file
int 21h
mov ah, 3Eh ; close file
mov bx, handle2 ; which file
int 21h
mov ah, 4ch
int 21h
end main
assembly x86 dos
assembly x86 dos
asked Nov 26 '18 at 18:49
woohooswoohoos
187
187
1
For this you will probably need string-to-number conversion and back.
– Jester
Nov 26 '18 at 19:06
i was reading quite a bit about that, however i cant seem to implement that as my registers are taken, gona have to think harder to rewrite this...
– woohoos
Nov 26 '18 at 19:18
thank you your comment gave me ideas
– woohoos
Nov 26 '18 at 19:36
2
You registers are not taken (you can reuse them). Even if they were you could just use (local) variables in memory.
– Jester
Nov 26 '18 at 19:36
add a comment |
1
For this you will probably need string-to-number conversion and back.
– Jester
Nov 26 '18 at 19:06
i was reading quite a bit about that, however i cant seem to implement that as my registers are taken, gona have to think harder to rewrite this...
– woohoos
Nov 26 '18 at 19:18
thank you your comment gave me ideas
– woohoos
Nov 26 '18 at 19:36
2
You registers are not taken (you can reuse them). Even if they were you could just use (local) variables in memory.
– Jester
Nov 26 '18 at 19:36
1
1
For this you will probably need string-to-number conversion and back.
– Jester
Nov 26 '18 at 19:06
For this you will probably need string-to-number conversion and back.
– Jester
Nov 26 '18 at 19:06
i was reading quite a bit about that, however i cant seem to implement that as my registers are taken, gona have to think harder to rewrite this...
– woohoos
Nov 26 '18 at 19:18
i was reading quite a bit about that, however i cant seem to implement that as my registers are taken, gona have to think harder to rewrite this...
– woohoos
Nov 26 '18 at 19:18
thank you your comment gave me ideas
– woohoos
Nov 26 '18 at 19:36
thank you your comment gave me ideas
– woohoos
Nov 26 '18 at 19:36
2
2
You registers are not taken (you can reuse them). Even if they were you could just use (local) variables in memory.
– Jester
Nov 26 '18 at 19:36
You registers are not taken (you can reuse them). Even if they were you could just use (local) variables in memory.
– Jester
Nov 26 '18 at 19:36
add a comment |
1 Answer
1
active
oldest
votes
.model small
.stack 100h
.data
handle dw ?
handle2 dw ?
filename db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
filename2 db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
prompt1 db 13,10,"ENTER FILE NAME HERE: $"
prompt2 db 13,10,"ENTER A SECONDARY FILE NAME: $"
prompt3 db 13,10,"ENTER LETTER COUNT: $"
mess1 db ' I WIN! $'
buf db ?
input db 30 dup ('$')
n dw ?
count dw ?
count2 dw ?
output db 30 dup ('$')
msj db 13,10,'The number is = $'
.code
main:
mov ax, @data ; set up addressability of data
mov ds, ax
;DISPLAY MESSAGE.
lea dx, prompt1 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;OPEN FILE TO READ FROM IT.
mov ah, 3DH
mov al, 0 ;READ MODE.
mov dx, offset filename + 2
int 21h
mov handle, ax ; save file handle
jmp w1
f1:
;READ ALL BYTES FROM FIRST FILE AND WRITE THEM TO SECOND FILE.
mov count2, 0
reading:
mov ah, 3FH
mov bx, handle
xor dx, dx
mov dx, offset buf
mov cx, 1 ;HOW MANY BYTES TO READ.
mov dx, offset buf ;THE BYTE WILL BE STORED HERE.
int 21h ;NUMBER OF BYTES READ RETURNS IN AX.
;CHECK EOF (END OF FILE).
;push cx
cmp ax, 0 ;IF AX == 0 THEN EOF.
je eof
cmp count2, 0
jne writ
;DISPLAY MESSAGE FOR SECOND FILE.
lea dx, prompt2 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename2 ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename2 + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;CREATE FILE.
mov ah, 3ch ; dos service to create file
mov cx, 0 ;READ/WRITE MODE.
mov dx, offset filename2 + 2 ;CHARACTERS START AT BYTE 2.
int 21h
mov handle2, ax ; save file handle
writ:
inc count2
;WRITE BYTE TO THE SECOND FILE.
mov ah, 40h ; write to
mov bx, handle2 ; file
mov dx, offset buf ; where to find data to write
mov cx, 1 ;LENGTH OF STRING IN CX.
int 21h
mov cx, count
cmp cx, count2
je f1
;cmp ax, count
jmp reading ;REPEAT PROCESS.
eof:
;CLOSE FILES.
mov ah, 3Eh ; close file
mov bx, handle ; which file
int 21h
mov ah, 3Eh ; close file
mov bx, handle2 ; which file
int 21h
mov ah, 4ch
int 21h
;WORK WITH NUMBER
w1:
;DISPLAY MESSAGE.
mov dx , offset prompt3
mov ah, 9
int 21h
;CAPTURE NUMBER CHAR BY CHAR. NOTICE CHR(13) WILL BE
;STORED IN STRING AND COUNTED.
mov bx , offset input
mov count , 0
l1:
mov ah , 1
int 21h ;CAPTURE ONE CHAR FROM KEYBOARD.
mov [bx] , al ;STORE CHAR IN STRING.
inc bx
inc count
cmp al , 13
jne l1 ;IF CHAR IS NOT "ENTER", REPEAT.
dec count ;NECESSARY BECAUSE CHR(13) WAS COUNTED.
;CONVERT STRING TO NUMBER.
mov bx , offset input ;BX POINTS TO THE FIRST CHAR.
add bx, count ;NOW BX POINTS ONE CHAR AFTER THE LAST ONE.
mov bp, 0 ;BP WILL BE THE NUMBER CONVERTED FROM STRING.
mov cx, 0 ;PROCESS STARTS WITH 10^0.
l2:
;GET CURRENT POWER OF 10.
cmp cx, 0
je first_time ;FIRST TIME IS 1, BECAUSE 10^0 = 1.
mov ax, 10
mul cx ;CX * 10. NEXT TIME=100, NEXT TIME=1000...
mov cx, ax ;CX == 10^CX.
jmp l22 ;SKIP THE "FIRST TIME" BLOCK.
first_time:
mov cx, 1 ;FIRST TIME 10^0 = 1.
l22:
;CONVERT CURRENT CHAR TO NUMBER.
dec bx ;BX POINTS TO CURRENT CHAR.
mov al , [bx] ;AL = CURRENT CHAR.
sub al , 48 ;CONVERT CHAR TO NUMBER.
;MULTIPLY CURRENT NUMBER BY CURRENT POWER OF 10.
mov ah, 0 ;CLEAR AH TO USE AX.
mul cx ;AX * CX = DX:AX. LET'S IGNORE DX.
add bp , ax ;STORE RESULT IN BP.
;CHECK IF THERE ARE MORE CHARS.
dec count
cmp count , 0
jne l2
push bp
pop count
jmp f1
end main
This is the answer for people having similar problem and this is the solution i have found. Application will split files into multiple separate files depending on the selected size. It is by far not the most efficient as every letter is read separately, however it does the job and it managed to split a picture file and using windows commands i managed to restore it back to original picture.
first input requires the name of the file you want to split(for example data.txt or picture.png)
second is the ammount of chars you want to be in a single file(up to around 35000, be careful as splitting a file with 10000 chars into small pieces might create hundreds of files)
third to x input is the newly created file names(ex. data1 , data2.txt , data3.png... etc it will ask for file names until the first file reaches the end, therefore splitting 100 letter file by 1 char will ask you for 100 names as an input).
good luck and happy programming :)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487286%2freading-input-file-and-creating-multiple-output-files-to-store-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
.model small
.stack 100h
.data
handle dw ?
handle2 dw ?
filename db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
filename2 db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
prompt1 db 13,10,"ENTER FILE NAME HERE: $"
prompt2 db 13,10,"ENTER A SECONDARY FILE NAME: $"
prompt3 db 13,10,"ENTER LETTER COUNT: $"
mess1 db ' I WIN! $'
buf db ?
input db 30 dup ('$')
n dw ?
count dw ?
count2 dw ?
output db 30 dup ('$')
msj db 13,10,'The number is = $'
.code
main:
mov ax, @data ; set up addressability of data
mov ds, ax
;DISPLAY MESSAGE.
lea dx, prompt1 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;OPEN FILE TO READ FROM IT.
mov ah, 3DH
mov al, 0 ;READ MODE.
mov dx, offset filename + 2
int 21h
mov handle, ax ; save file handle
jmp w1
f1:
;READ ALL BYTES FROM FIRST FILE AND WRITE THEM TO SECOND FILE.
mov count2, 0
reading:
mov ah, 3FH
mov bx, handle
xor dx, dx
mov dx, offset buf
mov cx, 1 ;HOW MANY BYTES TO READ.
mov dx, offset buf ;THE BYTE WILL BE STORED HERE.
int 21h ;NUMBER OF BYTES READ RETURNS IN AX.
;CHECK EOF (END OF FILE).
;push cx
cmp ax, 0 ;IF AX == 0 THEN EOF.
je eof
cmp count2, 0
jne writ
;DISPLAY MESSAGE FOR SECOND FILE.
lea dx, prompt2 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename2 ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename2 + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;CREATE FILE.
mov ah, 3ch ; dos service to create file
mov cx, 0 ;READ/WRITE MODE.
mov dx, offset filename2 + 2 ;CHARACTERS START AT BYTE 2.
int 21h
mov handle2, ax ; save file handle
writ:
inc count2
;WRITE BYTE TO THE SECOND FILE.
mov ah, 40h ; write to
mov bx, handle2 ; file
mov dx, offset buf ; where to find data to write
mov cx, 1 ;LENGTH OF STRING IN CX.
int 21h
mov cx, count
cmp cx, count2
je f1
;cmp ax, count
jmp reading ;REPEAT PROCESS.
eof:
;CLOSE FILES.
mov ah, 3Eh ; close file
mov bx, handle ; which file
int 21h
mov ah, 3Eh ; close file
mov bx, handle2 ; which file
int 21h
mov ah, 4ch
int 21h
;WORK WITH NUMBER
w1:
;DISPLAY MESSAGE.
mov dx , offset prompt3
mov ah, 9
int 21h
;CAPTURE NUMBER CHAR BY CHAR. NOTICE CHR(13) WILL BE
;STORED IN STRING AND COUNTED.
mov bx , offset input
mov count , 0
l1:
mov ah , 1
int 21h ;CAPTURE ONE CHAR FROM KEYBOARD.
mov [bx] , al ;STORE CHAR IN STRING.
inc bx
inc count
cmp al , 13
jne l1 ;IF CHAR IS NOT "ENTER", REPEAT.
dec count ;NECESSARY BECAUSE CHR(13) WAS COUNTED.
;CONVERT STRING TO NUMBER.
mov bx , offset input ;BX POINTS TO THE FIRST CHAR.
add bx, count ;NOW BX POINTS ONE CHAR AFTER THE LAST ONE.
mov bp, 0 ;BP WILL BE THE NUMBER CONVERTED FROM STRING.
mov cx, 0 ;PROCESS STARTS WITH 10^0.
l2:
;GET CURRENT POWER OF 10.
cmp cx, 0
je first_time ;FIRST TIME IS 1, BECAUSE 10^0 = 1.
mov ax, 10
mul cx ;CX * 10. NEXT TIME=100, NEXT TIME=1000...
mov cx, ax ;CX == 10^CX.
jmp l22 ;SKIP THE "FIRST TIME" BLOCK.
first_time:
mov cx, 1 ;FIRST TIME 10^0 = 1.
l22:
;CONVERT CURRENT CHAR TO NUMBER.
dec bx ;BX POINTS TO CURRENT CHAR.
mov al , [bx] ;AL = CURRENT CHAR.
sub al , 48 ;CONVERT CHAR TO NUMBER.
;MULTIPLY CURRENT NUMBER BY CURRENT POWER OF 10.
mov ah, 0 ;CLEAR AH TO USE AX.
mul cx ;AX * CX = DX:AX. LET'S IGNORE DX.
add bp , ax ;STORE RESULT IN BP.
;CHECK IF THERE ARE MORE CHARS.
dec count
cmp count , 0
jne l2
push bp
pop count
jmp f1
end main
This is the answer for people having similar problem and this is the solution i have found. Application will split files into multiple separate files depending on the selected size. It is by far not the most efficient as every letter is read separately, however it does the job and it managed to split a picture file and using windows commands i managed to restore it back to original picture.
first input requires the name of the file you want to split(for example data.txt or picture.png)
second is the ammount of chars you want to be in a single file(up to around 35000, be careful as splitting a file with 10000 chars into small pieces might create hundreds of files)
third to x input is the newly created file names(ex. data1 , data2.txt , data3.png... etc it will ask for file names until the first file reaches the end, therefore splitting 100 letter file by 1 char will ask you for 100 names as an input).
good luck and happy programming :)
add a comment |
.model small
.stack 100h
.data
handle dw ?
handle2 dw ?
filename db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
filename2 db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
prompt1 db 13,10,"ENTER FILE NAME HERE: $"
prompt2 db 13,10,"ENTER A SECONDARY FILE NAME: $"
prompt3 db 13,10,"ENTER LETTER COUNT: $"
mess1 db ' I WIN! $'
buf db ?
input db 30 dup ('$')
n dw ?
count dw ?
count2 dw ?
output db 30 dup ('$')
msj db 13,10,'The number is = $'
.code
main:
mov ax, @data ; set up addressability of data
mov ds, ax
;DISPLAY MESSAGE.
lea dx, prompt1 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;OPEN FILE TO READ FROM IT.
mov ah, 3DH
mov al, 0 ;READ MODE.
mov dx, offset filename + 2
int 21h
mov handle, ax ; save file handle
jmp w1
f1:
;READ ALL BYTES FROM FIRST FILE AND WRITE THEM TO SECOND FILE.
mov count2, 0
reading:
mov ah, 3FH
mov bx, handle
xor dx, dx
mov dx, offset buf
mov cx, 1 ;HOW MANY BYTES TO READ.
mov dx, offset buf ;THE BYTE WILL BE STORED HERE.
int 21h ;NUMBER OF BYTES READ RETURNS IN AX.
;CHECK EOF (END OF FILE).
;push cx
cmp ax, 0 ;IF AX == 0 THEN EOF.
je eof
cmp count2, 0
jne writ
;DISPLAY MESSAGE FOR SECOND FILE.
lea dx, prompt2 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename2 ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename2 + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;CREATE FILE.
mov ah, 3ch ; dos service to create file
mov cx, 0 ;READ/WRITE MODE.
mov dx, offset filename2 + 2 ;CHARACTERS START AT BYTE 2.
int 21h
mov handle2, ax ; save file handle
writ:
inc count2
;WRITE BYTE TO THE SECOND FILE.
mov ah, 40h ; write to
mov bx, handle2 ; file
mov dx, offset buf ; where to find data to write
mov cx, 1 ;LENGTH OF STRING IN CX.
int 21h
mov cx, count
cmp cx, count2
je f1
;cmp ax, count
jmp reading ;REPEAT PROCESS.
eof:
;CLOSE FILES.
mov ah, 3Eh ; close file
mov bx, handle ; which file
int 21h
mov ah, 3Eh ; close file
mov bx, handle2 ; which file
int 21h
mov ah, 4ch
int 21h
;WORK WITH NUMBER
w1:
;DISPLAY MESSAGE.
mov dx , offset prompt3
mov ah, 9
int 21h
;CAPTURE NUMBER CHAR BY CHAR. NOTICE CHR(13) WILL BE
;STORED IN STRING AND COUNTED.
mov bx , offset input
mov count , 0
l1:
mov ah , 1
int 21h ;CAPTURE ONE CHAR FROM KEYBOARD.
mov [bx] , al ;STORE CHAR IN STRING.
inc bx
inc count
cmp al , 13
jne l1 ;IF CHAR IS NOT "ENTER", REPEAT.
dec count ;NECESSARY BECAUSE CHR(13) WAS COUNTED.
;CONVERT STRING TO NUMBER.
mov bx , offset input ;BX POINTS TO THE FIRST CHAR.
add bx, count ;NOW BX POINTS ONE CHAR AFTER THE LAST ONE.
mov bp, 0 ;BP WILL BE THE NUMBER CONVERTED FROM STRING.
mov cx, 0 ;PROCESS STARTS WITH 10^0.
l2:
;GET CURRENT POWER OF 10.
cmp cx, 0
je first_time ;FIRST TIME IS 1, BECAUSE 10^0 = 1.
mov ax, 10
mul cx ;CX * 10. NEXT TIME=100, NEXT TIME=1000...
mov cx, ax ;CX == 10^CX.
jmp l22 ;SKIP THE "FIRST TIME" BLOCK.
first_time:
mov cx, 1 ;FIRST TIME 10^0 = 1.
l22:
;CONVERT CURRENT CHAR TO NUMBER.
dec bx ;BX POINTS TO CURRENT CHAR.
mov al , [bx] ;AL = CURRENT CHAR.
sub al , 48 ;CONVERT CHAR TO NUMBER.
;MULTIPLY CURRENT NUMBER BY CURRENT POWER OF 10.
mov ah, 0 ;CLEAR AH TO USE AX.
mul cx ;AX * CX = DX:AX. LET'S IGNORE DX.
add bp , ax ;STORE RESULT IN BP.
;CHECK IF THERE ARE MORE CHARS.
dec count
cmp count , 0
jne l2
push bp
pop count
jmp f1
end main
This is the answer for people having similar problem and this is the solution i have found. Application will split files into multiple separate files depending on the selected size. It is by far not the most efficient as every letter is read separately, however it does the job and it managed to split a picture file and using windows commands i managed to restore it back to original picture.
first input requires the name of the file you want to split(for example data.txt or picture.png)
second is the ammount of chars you want to be in a single file(up to around 35000, be careful as splitting a file with 10000 chars into small pieces might create hundreds of files)
third to x input is the newly created file names(ex. data1 , data2.txt , data3.png... etc it will ask for file names until the first file reaches the end, therefore splitting 100 letter file by 1 char will ask you for 100 names as an input).
good luck and happy programming :)
add a comment |
.model small
.stack 100h
.data
handle dw ?
handle2 dw ?
filename db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
filename2 db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
prompt1 db 13,10,"ENTER FILE NAME HERE: $"
prompt2 db 13,10,"ENTER A SECONDARY FILE NAME: $"
prompt3 db 13,10,"ENTER LETTER COUNT: $"
mess1 db ' I WIN! $'
buf db ?
input db 30 dup ('$')
n dw ?
count dw ?
count2 dw ?
output db 30 dup ('$')
msj db 13,10,'The number is = $'
.code
main:
mov ax, @data ; set up addressability of data
mov ds, ax
;DISPLAY MESSAGE.
lea dx, prompt1 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;OPEN FILE TO READ FROM IT.
mov ah, 3DH
mov al, 0 ;READ MODE.
mov dx, offset filename + 2
int 21h
mov handle, ax ; save file handle
jmp w1
f1:
;READ ALL BYTES FROM FIRST FILE AND WRITE THEM TO SECOND FILE.
mov count2, 0
reading:
mov ah, 3FH
mov bx, handle
xor dx, dx
mov dx, offset buf
mov cx, 1 ;HOW MANY BYTES TO READ.
mov dx, offset buf ;THE BYTE WILL BE STORED HERE.
int 21h ;NUMBER OF BYTES READ RETURNS IN AX.
;CHECK EOF (END OF FILE).
;push cx
cmp ax, 0 ;IF AX == 0 THEN EOF.
je eof
cmp count2, 0
jne writ
;DISPLAY MESSAGE FOR SECOND FILE.
lea dx, prompt2 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename2 ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename2 + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;CREATE FILE.
mov ah, 3ch ; dos service to create file
mov cx, 0 ;READ/WRITE MODE.
mov dx, offset filename2 + 2 ;CHARACTERS START AT BYTE 2.
int 21h
mov handle2, ax ; save file handle
writ:
inc count2
;WRITE BYTE TO THE SECOND FILE.
mov ah, 40h ; write to
mov bx, handle2 ; file
mov dx, offset buf ; where to find data to write
mov cx, 1 ;LENGTH OF STRING IN CX.
int 21h
mov cx, count
cmp cx, count2
je f1
;cmp ax, count
jmp reading ;REPEAT PROCESS.
eof:
;CLOSE FILES.
mov ah, 3Eh ; close file
mov bx, handle ; which file
int 21h
mov ah, 3Eh ; close file
mov bx, handle2 ; which file
int 21h
mov ah, 4ch
int 21h
;WORK WITH NUMBER
w1:
;DISPLAY MESSAGE.
mov dx , offset prompt3
mov ah, 9
int 21h
;CAPTURE NUMBER CHAR BY CHAR. NOTICE CHR(13) WILL BE
;STORED IN STRING AND COUNTED.
mov bx , offset input
mov count , 0
l1:
mov ah , 1
int 21h ;CAPTURE ONE CHAR FROM KEYBOARD.
mov [bx] , al ;STORE CHAR IN STRING.
inc bx
inc count
cmp al , 13
jne l1 ;IF CHAR IS NOT "ENTER", REPEAT.
dec count ;NECESSARY BECAUSE CHR(13) WAS COUNTED.
;CONVERT STRING TO NUMBER.
mov bx , offset input ;BX POINTS TO THE FIRST CHAR.
add bx, count ;NOW BX POINTS ONE CHAR AFTER THE LAST ONE.
mov bp, 0 ;BP WILL BE THE NUMBER CONVERTED FROM STRING.
mov cx, 0 ;PROCESS STARTS WITH 10^0.
l2:
;GET CURRENT POWER OF 10.
cmp cx, 0
je first_time ;FIRST TIME IS 1, BECAUSE 10^0 = 1.
mov ax, 10
mul cx ;CX * 10. NEXT TIME=100, NEXT TIME=1000...
mov cx, ax ;CX == 10^CX.
jmp l22 ;SKIP THE "FIRST TIME" BLOCK.
first_time:
mov cx, 1 ;FIRST TIME 10^0 = 1.
l22:
;CONVERT CURRENT CHAR TO NUMBER.
dec bx ;BX POINTS TO CURRENT CHAR.
mov al , [bx] ;AL = CURRENT CHAR.
sub al , 48 ;CONVERT CHAR TO NUMBER.
;MULTIPLY CURRENT NUMBER BY CURRENT POWER OF 10.
mov ah, 0 ;CLEAR AH TO USE AX.
mul cx ;AX * CX = DX:AX. LET'S IGNORE DX.
add bp , ax ;STORE RESULT IN BP.
;CHECK IF THERE ARE MORE CHARS.
dec count
cmp count , 0
jne l2
push bp
pop count
jmp f1
end main
This is the answer for people having similar problem and this is the solution i have found. Application will split files into multiple separate files depending on the selected size. It is by far not the most efficient as every letter is read separately, however it does the job and it managed to split a picture file and using windows commands i managed to restore it back to original picture.
first input requires the name of the file you want to split(for example data.txt or picture.png)
second is the ammount of chars you want to be in a single file(up to around 35000, be careful as splitting a file with 10000 chars into small pieces might create hundreds of files)
third to x input is the newly created file names(ex. data1 , data2.txt , data3.png... etc it will ask for file names until the first file reaches the end, therefore splitting 100 letter file by 1 char will ask you for 100 names as an input).
good luck and happy programming :)
.model small
.stack 100h
.data
handle dw ?
handle2 dw ?
filename db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
filename2 db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
prompt1 db 13,10,"ENTER FILE NAME HERE: $"
prompt2 db 13,10,"ENTER A SECONDARY FILE NAME: $"
prompt3 db 13,10,"ENTER LETTER COUNT: $"
mess1 db ' I WIN! $'
buf db ?
input db 30 dup ('$')
n dw ?
count dw ?
count2 dw ?
output db 30 dup ('$')
msj db 13,10,'The number is = $'
.code
main:
mov ax, @data ; set up addressability of data
mov ds, ax
;DISPLAY MESSAGE.
lea dx, prompt1 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;OPEN FILE TO READ FROM IT.
mov ah, 3DH
mov al, 0 ;READ MODE.
mov dx, offset filename + 2
int 21h
mov handle, ax ; save file handle
jmp w1
f1:
;READ ALL BYTES FROM FIRST FILE AND WRITE THEM TO SECOND FILE.
mov count2, 0
reading:
mov ah, 3FH
mov bx, handle
xor dx, dx
mov dx, offset buf
mov cx, 1 ;HOW MANY BYTES TO READ.
mov dx, offset buf ;THE BYTE WILL BE STORED HERE.
int 21h ;NUMBER OF BYTES READ RETURNS IN AX.
;CHECK EOF (END OF FILE).
;push cx
cmp ax, 0 ;IF AX == 0 THEN EOF.
je eof
cmp count2, 0
jne writ
;DISPLAY MESSAGE FOR SECOND FILE.
lea dx, prompt2 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename2 ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename2 + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;CREATE FILE.
mov ah, 3ch ; dos service to create file
mov cx, 0 ;READ/WRITE MODE.
mov dx, offset filename2 + 2 ;CHARACTERS START AT BYTE 2.
int 21h
mov handle2, ax ; save file handle
writ:
inc count2
;WRITE BYTE TO THE SECOND FILE.
mov ah, 40h ; write to
mov bx, handle2 ; file
mov dx, offset buf ; where to find data to write
mov cx, 1 ;LENGTH OF STRING IN CX.
int 21h
mov cx, count
cmp cx, count2
je f1
;cmp ax, count
jmp reading ;REPEAT PROCESS.
eof:
;CLOSE FILES.
mov ah, 3Eh ; close file
mov bx, handle ; which file
int 21h
mov ah, 3Eh ; close file
mov bx, handle2 ; which file
int 21h
mov ah, 4ch
int 21h
;WORK WITH NUMBER
w1:
;DISPLAY MESSAGE.
mov dx , offset prompt3
mov ah, 9
int 21h
;CAPTURE NUMBER CHAR BY CHAR. NOTICE CHR(13) WILL BE
;STORED IN STRING AND COUNTED.
mov bx , offset input
mov count , 0
l1:
mov ah , 1
int 21h ;CAPTURE ONE CHAR FROM KEYBOARD.
mov [bx] , al ;STORE CHAR IN STRING.
inc bx
inc count
cmp al , 13
jne l1 ;IF CHAR IS NOT "ENTER", REPEAT.
dec count ;NECESSARY BECAUSE CHR(13) WAS COUNTED.
;CONVERT STRING TO NUMBER.
mov bx , offset input ;BX POINTS TO THE FIRST CHAR.
add bx, count ;NOW BX POINTS ONE CHAR AFTER THE LAST ONE.
mov bp, 0 ;BP WILL BE THE NUMBER CONVERTED FROM STRING.
mov cx, 0 ;PROCESS STARTS WITH 10^0.
l2:
;GET CURRENT POWER OF 10.
cmp cx, 0
je first_time ;FIRST TIME IS 1, BECAUSE 10^0 = 1.
mov ax, 10
mul cx ;CX * 10. NEXT TIME=100, NEXT TIME=1000...
mov cx, ax ;CX == 10^CX.
jmp l22 ;SKIP THE "FIRST TIME" BLOCK.
first_time:
mov cx, 1 ;FIRST TIME 10^0 = 1.
l22:
;CONVERT CURRENT CHAR TO NUMBER.
dec bx ;BX POINTS TO CURRENT CHAR.
mov al , [bx] ;AL = CURRENT CHAR.
sub al , 48 ;CONVERT CHAR TO NUMBER.
;MULTIPLY CURRENT NUMBER BY CURRENT POWER OF 10.
mov ah, 0 ;CLEAR AH TO USE AX.
mul cx ;AX * CX = DX:AX. LET'S IGNORE DX.
add bp , ax ;STORE RESULT IN BP.
;CHECK IF THERE ARE MORE CHARS.
dec count
cmp count , 0
jne l2
push bp
pop count
jmp f1
end main
This is the answer for people having similar problem and this is the solution i have found. Application will split files into multiple separate files depending on the selected size. It is by far not the most efficient as every letter is read separately, however it does the job and it managed to split a picture file and using windows commands i managed to restore it back to original picture.
first input requires the name of the file you want to split(for example data.txt or picture.png)
second is the ammount of chars you want to be in a single file(up to around 35000, be careful as splitting a file with 10000 chars into small pieces might create hundreds of files)
third to x input is the newly created file names(ex. data1 , data2.txt , data3.png... etc it will ask for file names until the first file reaches the end, therefore splitting 100 letter file by 1 char will ask you for 100 names as an input).
good luck and happy programming :)
answered Dec 5 '18 at 15:08
woohooswoohoos
187
187
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487286%2freading-input-file-and-creating-multiple-output-files-to-store-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
For this you will probably need string-to-number conversion and back.
– Jester
Nov 26 '18 at 19:06
i was reading quite a bit about that, however i cant seem to implement that as my registers are taken, gona have to think harder to rewrite this...
– woohoos
Nov 26 '18 at 19:18
thank you your comment gave me ideas
– woohoos
Nov 26 '18 at 19:36
2
You registers are not taken (you can reuse them). Even if they were you could just use (local) variables in memory.
– Jester
Nov 26 '18 at 19:36