Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

tasm problem in reversing text in file

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

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]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.
    [/color]
  • thatsmethatsme Posts: 8Member
    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
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]Then your algorithm is different:[/color]
    [code]
    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
    [/code][color=Blue]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.
    [/color]
Sign In or Register to comment.