【问题标题】:Buffers do no behave properly缓冲区不能正常运行
【发布时间】:2014-12-02 14:08:21
【问题描述】:

我在汇编编程方面很痛苦,并寻求您的帮助。我不明白的是,即使缓冲区有足够的空间,为什么我会从缓冲区中丢失字符串的字符?

我的 readBuffer 大小是 32,writeBuffer 大小是 64,我的程序所做的是在句号后添加一个空格并将第一个字母大写。

示例:

Good.morning.people hehehe in the jungle the mighty jungle  //this is the data
Good. Morning. People hehehe in the jungle the mighty jungle //this is the output

我的程序做什么:

Good.morning.people hehehe in the jungle the mighty jungle  //this is the data
Good. Morning. People hehehe in e jungle the mighty jungle //this is the output

如你所见,我以 'e' 而不是 'the' 结尾

这是我进行编辑的代码:

Read:

    MOV bx, DataHandle          
    CALL ReadBuffer         
    CMP ax, 0               ;ax = how many symbols did I read
    JE  closeWrite          ;if 0 then close write file

;Editing the string

    MOV  cx, ax               ;CX FOR LOOPING
    MOV  si, offset rBuff     ;read buffer = si
    MOV di, offset wBuff      ;write buffer = di

  Do_It:

    MOV dl, [si]
    CMP dl, '.'                 ;comparing to period
    JNE Keep_Going              ;if not period put it in di (write buffer)
    MOV dh, [si+1]              ;put the char after the period in dh
    CMP dh, ' '                 ;check if space is after it
    JNE If_capital              ;if not I check if it is a capital letter
    MOV dh, [si+2]              ;if there was a space I add the char after the space
    CMP dh, 'a'                 ;checking if it is a capital letter
    JB  Keep_Going  
    CMP dh, 'z'     
    JA  Keep_Going  
    SUB byte ptr [si+2], 32     ;turn it into a  capital letter

  Keep_Going:

    MOV [di], dl                ;put the  char into writebuff
    INC si
    INC di
    LOOP Do_It
    JMP Finishin

  If_capital:

    CMP dh, 'a'     
    JB  Add_Space       
    CMP dh, 'z'     
    JA  Add_Space   
    SUB byte ptr [si+1], 32

  Add_Space:

    MOV [di], '.'       
    INC di
    MOV [di], ' '
    INC di
    INC si
    JMP Do_It

;Write the result

  Finishin:
    MOV cx, ax          
    CALL checkOnScreen       ;print it to command line
    MOV bx, WriteHandle         
    CALL    writeBuffer      ;print it
    CMP ax, rBuffSize        ;compare the size to 32
    JE  Read                 ;if it was 32 then read again

【问题讨论】:

  • 会不会只是你的输出函数搞砸了?据我所知,您正在将原始长度传递给它。
  • 调试器逐行通过您的代码

标签: string assembly char output x86-16


【解决方案1】:

您只写了 32 个字节的 wBuff。这是在第一次运行之后:“Good. Morning. People hehehe in”。由于该行较长,其余部分位于 32 字节边界的后面。然后您将指针重置为rBuffwBuff 并阅读下一个块:“e 丛林强大的丛林”。该块附加到写入的行。

考虑要写入的块具有可变长度。将JMP Do_It改为LOOP Do_It后,可以通过DI(sub di, OFFSET wBuff)减去wBuff的偏移量来计算长度。

由于我无法解释您代码的相关部分(CMP ax, rBuffSizeJE Read ???),因此我无法展示一个工作示例。

【讨论】:

    【解决方案2】:

    感谢大家的意见,这次我自己做到了。问题是

    Finishin:
        MOV cx, ax          
        CALL checkOnScreen       
        MOV bx, WriteHandle         
        CALL    writeBuffer      
        CMP ax, rBuffSize        
        JE  Read     
    

    新代码:

        Finishin:
            MOV cx, strLength2         ;length of writebuffer
            CALL checkOnScreen       
            MOV bx, WriteHandle         
            CALL    writeBuffer      
            CMP ax, rBuffSize        ;ax was messed up so I changed it to strLength1 or readBuffer
            JE  Read                
    

    【讨论】:

      猜你喜欢
      • 2020-09-05
      • 1970-01-01
      • 2011-08-10
      • 2021-01-25
      • 2012-03-06
      • 2012-08-29
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      相关资源
      最近更新 更多