TASM在缓冲区上重复字节

时间:2014-12-08 15:24:28

标签: assembly x86 tasm procedures

我的算法无法找到缺陷。基本上我想要get_byte处理一个事情,当分析整个缓冲区时,将加载新的缓冲区。比如,例如,如果我将缓冲区长度设置为3并且我将get_byte调用了5次,则会得到输出" 12245"我的文件内容为" 123456789 ...."。如果我将缓冲区大小增加到5以上(例如9),则输出为12345.请帮我找出发生这种情况的原因。谢谢。代码如下

proc get_byte
  mov ah, read_buffer_length   ; ah = read_buffer_length
  cmp read_position, ah        ; comparing current position and ah
  jl @@less                    ; if read_position < read_buffer_length, then we jump to @@less part
  @@read_input_buffer:         
    call read_input_buffer

    ; if we have read 0 bytes, we are done
    cmp read_buffer_length, 0
    je @@done

    ; if not, we renew buffer information
    mov read_position, 1       ; setting position to 1
    mov si, offset read_buffer ; showing to read position
    inc current_position       ; for other purposes
    ret

  @@less:                  ; we dont have to reread buffer, it wokrs okay
    mov al, byte ptr[si]   ; putting al byte from buffer
    inc si                 ; going to next byte
    inc read_position      ; to know when to renew buffer
    inc current_position   ; for other purposes
  ret

  ; no more to do, stopping loop
  @@done:
    mov stop_prog_loop, 1
    ret
endp

proc read_input_buffer
  ; saving registry values
  push ax
  push bx
  push cx
  push dx

  mov bx, input_handler      ; descriptor number
  mov cx, read_buffer_size   ; how many bytes to read
  mov dx, offset read_buffer ; address of buffer

  mov ah, 3Fh                ; calling dos
  int 21h                    ; calling dos

  mov read_buffer_length, al ; how many symbols current buffer has

  ; giving registers values back
  pop dx
  pop cx
  pop bx
  pop ax

  ret
endp

编辑:我的调试代码。我不会发表评论,因为如果你理解英语和基础汇编程序,应该清楚会发生什么。打开/关闭文件非常有效。

call open_input_file
call open_output_file
call read_input_buffer

mov read_position, 1       
mov si, offset read_buffer 

call get_byte
int 29h
call get_byte
int 29h
call get_byte
int 29h
call get_byte
int 29h
call get_byte
int 29h

call close_output_file   
call close_input_file   

2 个答案:

答案 0 :(得分:0)

我看不到你拨打get_byte的位置。

无论如何 - 因为你将read_position初始化为一个(不是零),你的

jl @@less

应该是

jle @@less

read_positionread_buffer_length进行比较后

答案 1 :(得分:0)

当你调用GET_BYTE并且你在缓冲区的末尾没有条件跳转到@@ LESS时,流程继续到@@ READ_INPUT_BUFFER和被调用的子程序,你在PUSH中注册堆栈,然后POP它们。但是,请注意,当您增加SI时,从前一个CALL到GET_BYTE的AL值保持不变,因此您再次显示相同的字符。您可以通过在GET_BYTE中的CALL READ_INPUT_BUFFER之前添加:MOV AL,BYTE PTR [SI]来修复它。