Getting started in assembly

Moderators: None (Apply to moderate this forum)
Number of threads: 218
Number of posts: 576

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
tasm problem in reversing text in file Posted by thatsme on 4 Dec 2009 at 4:15 PM
Hi, this program has to reverse the text in file. It uses two buffers of 100 bytes. One for reading piece of text, next for reversing that text.I tested program with file which contains of 120 bytes of numbers. I run debbuger and see, that symbols are reversed corertly in buffer and first to 100bytes are rewritten corectly. I see that when i open text file. Then other 20 bytes are reversed corretly, but when writing them something goes wrong and symbols in text become damaged. Can anybody help me, i am debugging this program for a week and can not make it work in the right way
ASSUME CS:Kodas, DS:Duom, SS:Stekas

;buferioDydis EQU 121


Stekas SEGMENT PARA STACK 'STACK'
DB 256 dup (?)
Stekas ENDS

duom segment
file db 13 dup (0)
handle dw ?
buffer1 db 100 dup (0)
buffer2 db 100 dup (0)
msg1 db 10, 13, "PSP is empty", '$'
msg2 db 10, 13, "Only spaces found in psp", '$'
msg3 db 10, 13, "Name of file is too long", '$'
er1 db 10, 13, "error in opening the file", "$"
er2 db 10, 13, "error in reading the file", "$"
er3 db 10, 13, "error in writting the file", "$"
er4 db 10, 13, "error in moving file pointer", "$"
msg4 db 10, 13, "Done", "$"
var dw ?
duom ends

kodas segment

start:
mov ax, duom
mov ds, ax

mov ax, 02h ;clear screen
int 10h

xor si, si
mov si, 80h

xor cx, cx
mov cl, es:[si]
cmp cl, 0 ;if length of psp=0, finish work
je message1
jmp continue

message1:
mov dx, offset msg1
mov ah, 9
int 21h
jmp endprogram

continue:
xor di, di
mov di, offset file ;initializing buffer for the file name
xor cx, cx

skipspaces:
inc si ; skipping spaces in psp
mov al, es:[si]
cmp al, ' '
je skipspaces

filename:
mov [di], al ;getting file name
inc di
inc cx
inc si
mov al, es:[si]
cmp al, ' '
je continue2
cmp al, 0Dh ;if the end of psp, finish the program
je continue2
jmp filename

continue2:
mov [di], byte ptr 0 ;marking the end of the name with zero
cmp cl, 0
je message2 ;if only spaces found, finish program
cmp cl, 12
jg message3 ;if only spaces found, finish program
jmp continue3

message2:
mov dx, offset msg2
mov ah, 9
int 21h
jmp endprogram

message3:
mov dx, offset msg3
mov ah, 9
int 21h
jmp endprogram


continue3:
xor bx, bx
xor ax, ax
mov ah, 3Dh
mov al, 2
mov dx, [di] ; opening file
int 21h
jc error1
mov handle, ax
jmp reading

error1:
mov dx, offset er1
mov ah, 9
int 21h
jmp endprogram

reading:
xor bx, bx
xor ah, ah
mov ah, 3Fh
mov cx, 100 ;reading 100 bytes to buffer
mov dx, offset buffer1
mov bx, handle
int 21h
jc error2
mov var, ax
mov ah, 42h ;Seek command
mov al, 1 ;Move from current location
xor cx, cx ;Zero out CX and DX so we
xor dx, dx ; stay right here
mov bx, Handle
int 21h
jc error4
jmp continue4

error2:
mov dx, offset er2
mov ah, 9
int 21h
jmp endprogram

continue4:
xor al, al ;now reversing the text using buffer2
xor di, di
xor si, si
mov si, 99
mov al, buffer1[si]
cmp al, 00h
je cycle
cmp al, 20h
je ciklas
mov buffer2[di],
al

cycle:
dec si
mov al, buffer1[si]
cmp al, 00h
je cycle
cmp al, 20h
je cycle
inc di
mov buffer2[di], al
cmp si, 0
je movepointer
jmp cycle

movepointer:
mov ah, 42h ;Seek command
mov al, 1 ;Move from current location
xor cx, cx ;Zero out CX and DX so we
xor dx, dx ; stay right here
mov bx, Handle
int 21h
jc error4
sub ax, var ;DX:AX now contains the
sbb dx, 0 ; current file position, so
mov cx, dx ; compute a location 256
mov dx, ax ; bytes back.
mov ah, 42h
mov al, 0 ;Absolute file position
int 21h ;BX still contains handle.
jc error4
jmp writting

error3:
mov dx, offset er3
mov ah, 9
int 21h
jmp end program

writting:
xor ax, ax
mov ah, 40h
mov bx, handle
mov cx, var ;writing content of buffer2 to file
mov dx, offset buffer2
int 21h
jc error3
mov ah, 42h ;Seek command
mov al, 1 ;Move from current location
xor cx, cx ;Zero out CX and DX so we
xor dx, dx ; stay right here
mov bx, Handle
int 21h
jc error4
jmp continue5

continue5:
cmp var, 100
jl endprogram
mov var, 0
xor si, si ;before reading next 100 bytes clear buffers
mov buffer1[si], 0
mov buffer2[si], 0

cycle3:
inc si
mov buffer1[si], 0
mov buffer2[si], 0
cmp si, 99
jl cycle3

xor cx, cx
xor bx, bx
xor ax, ax
jmp reading


error4:
mov dx, offset er4
mov ah, 9
int 21h
jmp endprogram

endprogram:
xor ax, ax
xor bx, bx
mov ah, 3Eh
mov bx, handle
int 21h

mov dx, offset msg4
mov ah, 9
int 21h

mov ah, 07h
int 21h

mov ax, 4c00h
int 21h
kodas ends
end start
Report
Re: tasm problem in reversing text in file Posted by AsmGuru62 on 5 Dec 2009 at 5:56 AM
Can you explain your algorithm? Are you reading from beginning of the file? If so, then using 100 byte buffer you can't reverse the 120 byte file.

Here is a proper algorithm:

Open source file
Create destination file
Make variable OFFSET = size of source file

LOAD_AGAIN:

NUM BYTES TO LOAD = 100
OFFSET -= NUM BYTES TO LOAD

if (OFFSET < 0)
{
NUM BYTES TO LOAD += OFFSET
OFFSET = 0
}

Set (seek) source file pointer to OFFSET from beginning
Read NUM BYTES TO LOAD from source file
Reverse those bytes
Write back into destination file

if (OFFSET > 0) GOTO LOAD_AGAIN

Close both files
All done.
Report
Re: tasm problem in reversing text in file Posted by thatsme on 5 Dec 2009 at 7:25 AM
no, i have to reverse text in the source file and if text is longer than 100 bytes, i have to reverse by 100bytes each time until the end of file. The biggest buffer i can use, is of 100bytes. I can use no more than two such buffers
Report
Re: tasm problem in reversing text in file Posted by AsmGuru62 on 6 Dec 2009 at 6:08 AM
Then your algorithm is different:
Open source file

Allocate BUF1 of size = 100 bytes
Allocate BUF2 of size = 100 bytes

OFS_BEGIN = 0
OFS_END = file size - 100

loop
  load 100 bytes into BUF1 from OFS_BEGIN
  load 100 bytes into BUF2 from OFS_END

  reverse bytes in BUF1
  reverse bytes in BUF2

  write data from BUF2 at OFS_BEGIN
  write data from BUF1 at OFS_END

  OFS_BEGIN += 100
  OFS_END -= 100

  IF (OFS_BEGIN > OFS_END) BREAK OUT OF LOOP
end loop
This is just a scheme. The code becomes complicated when file size is not a multiple of 100s - you need to make calculations of exact number of bytes for a last piece of the file. That happens when you just written buffers. At this point the size of file leftover is:

LEFT_OVER_SIZE = OFS_END - (OFS_BEGIN+100);

If that size of over 100 bytes long - you repeat the loop, but if less, then you need to adjust the block size in a scheme to that value instead of a 100 bytes.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.