反转线顺序程序集 32 位



我需要翻转文件的行顺序并将它们写入另一个文件中,但我遇到了一些问题。由于某种原因,我无法在 file2 中写入...任何建议和提示都会有用,这是我这种类型的第一个问题。我的老师的提示是使用fseek,我使用了它,但我卡住了。

例:

输入文件1:

line1
line 2
line 3

所需的输出文件2:

line 3
line2
line 1

.386
.model flat, stdcall
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;includem biblioteci, si declaram ce functii vrem sa importam
includelib msvcrt.lib
extern exit: proc
extern fopen:proc
extern getc:proc
extern fclose:proc
extern printf:proc
extern ftell:proc
extern fseek:proc
extern fscanf:proc
extern fprintf: proc
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;declaram simbolul start ca public - de acolo incepe executia
public start
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;sectiunile programului, date, respectiv cod
.data
;aici declaram date
s db 99 dup(0)
read db "r",0
write db "w",0
nume db "fisier1.txt",0
nume2 db "fisier2.txt",0
seek_end dd 2
format db "%s",0
.code
start:
;open first file to read
push offset read
push offset nume
call fopen
add esp,8
mov esi,eax;save pointer of file
;open second file to write
push offset write
push offset nume2
call fopen
add esp,8
mov edi,eax;save pointer of file
;find the end of file
push seek_end
push -1
push esi
call fseek
add esp,12
;save in ecx current position
push esi
call ftell
add esp,4
mov ecx,eax
et:
push esi
call getc
add esp,4
cmp eax,0ah;verify if isn't new line
jne previous
previous:
;move to the previous line
push 1
push -1
push esi
call fseek
add esp,12
jmp cont
read_write:
;read the line in string s 
push offset s
push offset format
push esi
call fscanf
add esp,12
;print string s in second file
push offset s
push offset format
push edi
call fprintf
add esp,12
jmp previous
cont:
dec ecx
;verify if isn't the beginning of file  
cmp ecx,0
jne et
push 0
call exit
end start

这绝对是我不会用汇编语言写的东西......但是,要完成这项工作,我将首先用高级语言编写算法。如果可以使逻辑在更高级别的语言中正常工作,则可以使程序集正常工作。

in = fopen("source.txt", "r");
fseek(in, 0, SEEK_END);
size = ftell(in);
fseek(in, 0, SEEK_SET);
out = fopen("destination.txt", "w");
ftruncate(out, size);
fseek(out, 0, SEEK_END);
while(fgets(buf, sizeof(buf), in))
{
len = strlen(buf);
fseek(out, -len, SEEK_CUR);
fwrite(out, 1, len, buf);
fseek(out, -len, SEEK_CUR);
}

此函数对由sizeof(buf)确定的行的大小有限制,并且该行中存在错误:

如果fgets()可以读取sizeof(buf)个字节,则不会返回以 null 结尾的字符串。

不幸的是,这就是 C 库的糟糕程度。该错误的简单修复:

buf[sizeof(buf) - 1] = '';
...
fgets(buf, sizeof(buf) - 1, in)

即,您将''放在缓冲区的末尾并且永远不会覆盖它。所以至少你永远不会超过你的缓冲区。

现在让您在汇编中转换该 C 代码。

在进行实际实际编码之前,还要先了解您的算法。

注意:我也没有测试任何错误代码。错误处理是个好主意。(即,如果fopen("destination.txt"...)失败怎么办?

最新更新