2014-12-02 81 views
0

我在汇编程序设计时感到很痛苦,并寻求你的帮助。我不明白的是为什么即使有足够的空间,我也从缓冲区中丢失了字符串的字符?缓冲区没有正确的行为

我的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' 而不是 '的'

这里是我的代码做编辑:

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 
+1

难道只是你的输出功能搞乱?据我所见,你将原始长度传递给它。 – 2014-12-02 15:23:43

+2

**通过您的代码逐行执行**步骤**,在**调试器**中 – xmojmr 2014-12-02 16:12:03

回答

1

您只写入wBuff的32个字节。这是第一次运行后:“早上好,人们嘿嘿”。由于该行较长,其余的位于32字节的寄存器之后。然后你重置指针rBuffwBuff并阅读下一个块:“丛林强大的丛林”。该块被附加到书面线。

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

因为我无法解释代码的相关部分(CMP ax, rBuffSizeJE Read ???),所以无法显示工作示例。

0

感谢输入的人,我这次设法自己做。问题是

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