使用汇编器交换文本文件中的 2 行

问题描述 投票:0回答:1

这是我在 DosBox 程序集中交换 2 行的代码。我不明白为什么它从 input.txt 打印相同的内容到 out.txt。我认为问题在于行号(和用户输入)。尝试直接向 line1 和 line2 提供固定值,但仍然不起作用。如果有人可以修复它或至少引导我走向正确的方向,我将不胜感激

.model small
.stack 100h

.data
    handle      dw ? 
    handle2     dw ? 

    filename    db 26               
                db ?                
                db 26 dup(0)

    outFile     db "out.txt", 0      

    prompt1     db 13,10,"Provide filename and two line numbers, which will be swapped, E.g.: input.txt 2 3: $"

    line1       dw ?                
    line2       dw ?               

  
    buf         db 128 dup(0)        
    tempBuf     db 128 dup(0)       
    bufLen      dw ?                
    currentLine dw 1                

.code
main:
    mov ax, @data
    mov ds, ax

    lea dx, prompt1
    mov ah, 9
    int 21h

    mov ah, 0Ah
    lea dx, filename
    int 21h

    mov si, offset filename + 1
    mov cl, [si]
    mov ch, 0
    inc cx
    add si, cx
    mov byte ptr [si], 0

    lea si, filename + 2
    call ParseLineNumbers
    jc failed

    mov ah, 3Dh
    mov al, 0
    lea dx, filename + 2
    int 21h
    jc failed
    mov handle, ax

    mov ah, 3Ch
    xor cx, cx
    lea dx, outFile
    int 21h
    jc failed
    mov handle2, ax

copy_and_swap:
    lea si, buf
    call ReadLine
    jc eof

    mov ax, currentLine
    cmp ax, line1
    je swap_with_line2

    lea si, buf
    call WriteLine
    inc currentLine
    jmp copy_and_swap

swap_with_line2:
    lea si, tempBuf
    call ReadLine

    lea si, tempBuf
    call WriteLine
    inc currentLine

    lea si, buf
    call WriteLine
    inc currentLine
    jmp copy_and_swap

eof:
    mov ah, 3Eh
    mov bx, handle
    int 21h
    mov ah, 3Eh
    mov bx, handle2
    int 21h

failed:
    mov ah, 4Ch
    int 21h


ReadLine proc
    mov ah, 3Fh
    mov bx, handle
    lea dx, buf
    mov cx, 128
    int 21h
    jc @exit
    cmp ax, 0     
    je @exit
    mov bufLen, ax
    ret
@exit:
    stc
    ret
ReadLine endp

WriteLine proc
    mov ah, 40h
    mov bx, handle2
    lea dx, buf
    mov cx, bufLen
    int 21h
    ret
WriteLine endp

ParseLineNumbers proc
    xor ax, ax
    xor cx, cx
parse_digit:
    lodsb
    cmp al, ' '
    je store_line1
    cmp al, 0
    je store_line2
    sub al, '0'
    mov bx, 10
    mov ax, cx    
    imul bx      
    mov cx, ax   
    add cx, ax
    jmp parse_digit

store_line1:
    mov line1, cx
    xor cx, cx
    jmp parse_digit

store_line2:
    mov line2, cx
    ret
ParseLineNumbers endp

end main
assembly line swap dosbox low-level-code
1个回答
0
投票

来自我之前的回答:

如果您对 ReadLine 进行编程,使其接受一个地址来加载该行,那么简单的解决方案将根本不需要复制:

现在您已经包含了 ReadLineWriteLine 过程,我可以看到您尚未更改它们以实际使用该 SI 参数!这当然是您在文本文件中看不到更改的原因之一。

; IN (si)
ReadLine proc
    mov ah, 3Fh
    mov bx, handle
    lea dx, buf           <==== change to   MOV DX, SI
    mov cx, 128
    int 21h
    jc @exit
    cmp ax, 0     
    je @exit
    mov bufLen, ax
    ret
@exit:
    stc
    ret
ReadLine endp

; IN (si)
WriteLine proc
    mov ah, 40h
    mov bx, handle2
    lea dx, buf           <==== change to   MOV DX, SI
    mov cx, bufLen
    int 21h
    ret
WriteLine endp
© www.soinside.com 2019 - 2024. All rights reserved.