Assembly ASM x86推送和弹出

Assembly ASM x86推送和弹出,assembly,x86,tasm,Assembly,X86,Tasm,这应该将用户给定的字符串与文本文件的每一行进行比较 如果等于,则必须打印该行 但即使提供了正确的字符串,程序也不会打印任何内容 我可能在push和pop指令上做错了什么 .model small .stack 100h .data filename db 255 dup(0) text db 255 dup(0) char db ?

这应该将用户给定的字符串与文本文件的每一行进行比较

如果等于,则必须打印该行


但即使提供了正确的字符串,程序也不会打印任何内容

我可能在
push
pop
指令上做错了什么

.model small
.stack  100h

.data
            filename          db 255 dup(0)
            text              db 255 dup(0)
            char              db ?

            text1             db 0dh, 0ah, 'Enter a string to search for: $'
            text2             db 0dh, 0ah, 'Enter a filename             : $' 

            string            db 255 dup(0)

            filehandle        dw ?

.code
            newline macro    ;NEW LINE
                             ;
            mov dl, 10       ;
            mov ah, 02h      ;
            int 21h          ;
                             ;
            mov dl, 13       ;
            mov ah, 02h      ;                     ;
            int 21h          ;
            endm             ;NEW LINE
main:

            mov ax, @data
            mov ds, ax

            mov ah, 09h
            lea dx, text1    ;'Enter a string to search for: $'
            int 21h 

            lea si, string
            mov ah, 01h      ;read character

string_input:

            int 21h

            cmp al, 0dh      ;end of string
            je zero_terminator2

            mov [si], al
            inc si

            jmp string_input

zero_terminator2:

            mov byte ptr [si], 0

            mov ah, 09h
            lea dx, text2
            int 21h 

            lea si, filename
            mov ah, 01h      ;read character

char_input:

            int 21h

            cmp al, 0dh      ;end of string
            je zero_terminator

            mov [si], al
            inc si

            jmp char_input

zero_terminator:

            mov byte ptr [si], 0

open_file:

            lea dx, filename
            mov al, 0
            mov ah, 3Dh      ;open file
            int 21h

            mov filehandle, ax

            lea si, text

read_line:

            mov ah, 3Fh      ;read file
            mov bx, filehandle
            lea dx, char
            mov cx, 1
            int 21h

            cmp ax, 0       ;EOF
            je EO_file

            mov al, char

            cmp al, 0ah     ; line feed
            je compare      ; compare line with string given by user

            mov [si], al
            inc si

            jmp read_line

EO_file:

            push dx         ;save dx for later use
            push cx         ;
            push si         ;

            xor dx, dx
            xor si, si
            xor di, di

            lea si, text
            lea di, string 

        loop_cmp_EOF:

            mov al, [si]    ;Get the next byte from text
            mov bl, [di]    ;Get the next byte from string

            cmp al, bl      ;Compare bytes
            jne end_it      ;end of program if not equal

            cmp al, 0       ;End of string. text=string
            je last_print   ;Print that line from text  

            inc si
            inc di 

            jmp loop_cmp_EOF  

        last_print:

            pop dx           
            pop cx
            pop si   

            newline

            lea dx, text    ;DX=offset(address) of text
            mov ah, 40h     ;print
            mov cx, si      ;CX = # characters. Move pointer to last char to it
            sub cx, dx      ;Subtract the offset of text (in DX) from CX
                            ;To get the actual number of chars in the buffer
            mov bx, 1
            int 21h  

       end_it:

            mov ah, 4ch
            int 21h

LF_print:    

            pop dx           
            pop cx
            pop si

            newline         ;Macro

            lea dx, text    ;DX=offset(address) of text
            mov ah, 40h     ;print
            mov cx, si      ;CX = # characters. Move pointer to last char to it
            sub cx, dx      ;Subtract the offset of text (in DX) from CX
                            ;To get the actual number of chars in the buffer                
            mov bx, 1
            int 21h 

            mov si, dx      ;Start from beginning of buffer 
                            ;(DX=beginning of text buffer)
            jmp read_line


       compare:

            push dx         ;save dx for later use
            push cx         ;
            push si         ;

            xor dx, dx
            xor si, si
            xor di, di

            lea si, text
            lea di, string 

       loop_cmp:

            mov al, [si]    ;Get the next byte from text
            mov bl, [di]    ;Get the next byte from string

            cmp al, bl      ;Compare bytes
            jne read_line   ;Do not print. Take another line from text

            cmp al, 0       ;End of string. text=string
            je LF_print     ;Print that line from text 

            inc si
            inc di 

            jmp loop_cmp

end main
有几个问题

  • 您必须按相反顺序弹出寄存器

    push dx         ;save dx for later use
    push cx         ;
    push si         ;
    ...
    pop  si           
    pop  cx
    pop  dx
    
  • 在循环中,你不能跳回读行,因为你有3个字压在堆栈上,永远不会弹出

    loop_cmp:
     mov al, [si]    ;Get the next byte from text
     mov bl, [di]    ;Get the next byte from string
     cmp al, bl      ;Compare bytes
     jne read_line   ;Do not print. Take another line from text
    
  • 在loop_cmp中,您将与零进行比较,但将行尾定义为换行(=10)。这是不一致的

     cmp al, 0       ;End of string. text=string
     je LF_print     ;Print that line from text
    

嗯,看起来你按错误的顺序弹出了它们。如果你最后推的东西是SI,那么这应该是你第一个弹出的东西。你可以在阅读时逐字符比较。如果匹配,则打印包含您正在搜索的模式的缓冲区,因为您只想打印与您刚刚读取的行相同的内容。因此,您不需要在阅读时存储这些行。一旦你有了用户的输入,你就有了你将要打印的唯一字符串。这只是一个何时打印的问题。因此,如果在比较后无法使用
read\u line
循环,我如何继续读取文本文件?请确保执行3
pop
s。将
jne read\u line
更改为
jne extra
,并在下面添加
jmp loop\u cmp
extra:
pop sipop cxpop dx
jne read\u line