2016-03-20 21 views
0

我寫了一個例程(作爲代碼的一部分),即使它必須ret也不會回退。爲什麼程序不停止(ret)而在循環中繼續? [assembly]

這是例行:

rout3:  lea SI,strtxt ;put the array in indirect addressing register 
      add SI,counter ;add a counter (starts from 0) 
      mov CL,[SI] ;move the character in counter location in the 
          array to CL register 
      mov char,CL ;move the character from the register to an operand 
      inc counter  ;increase counter for the next character (next location) 


      lea BX,arr ;new counter array (size: 256 ascii characters). 
         put the array in the register 
      mov CL,char ;instead of the counter, the ascii value of the 
          character is the location 
      add BX,CX ; add the location (the ascii value- up to 256) 
      inc [BX] ;increase the value of the ascii location (in the array) by one 

      mov CL,strlen ;move the length of the first array (strtxt) to CL register 
      cmp counter,CX ;compare the currect location of the character 
           to the full length of it 
      jb rout3   ;if the location is smaller than the strtxt array length, 
           go to the next character. if not- ret. 
      ret  

我與"jb rout 3"的一部分,並與ret一個問題。即使計數器等於或大於strlen(數組的長度),程序也會重新啓動。 請幫助我,當你幫我時,我花了很長時間才寫出瞭解釋你的理解的代碼。

謝謝你,美好的一天!

+0

從我所看到的JB中可以看出,JB使用2個操作數。如果第一個操作數中指定的位的值爲1,則JB指令分支到第二個操作數中指定的地址。被測試的位未被修改。沒有標誌受此指令的影響。[來自[8051指令集手冊](http://www.keil.com/support/man/docs/is51/is51_jb.htm) – MikeT

+0

)如果不是8051,但是80x86可以顯示更多有可能。如果在下面,JB是jmp。計數器(從0開始並遞增)可能不會低於CX(從未使用80x86,所以不確定,但我懷疑這是問題所在)。 – MikeT

+0

@MikeT:根據所有的說明,這看起來像8086/8088 –

回答

1

這段代碼效率太低,很難弄清楚它應該完成什麼。這些評論描述了每條指令在真正的本地層面上所做的,但不是總體目標是什麼。

AFAICT,循環不應該是無限的。 inc [counter]/cmp [counter],CX最終將導致未採取jb。假設counter沒有被循環中的其他東西覆蓋,並且inc counter正在使用16位操作數大小(感謝彙編程序在標籤後面看到dw指令?),它最終將達到0xFFFF,這不可能是低於CX可能擁有的任何價值。

正如評論中指出的,你永遠不會寫CH。您編寫CL並閱讀CX。如果在CH中使用非零值調用此例程,那麼它可能會循環比您想要的次數更多的次數。您可能應該xor cx,cx在輸入該函數時將其歸零。


我不明白爲什麼你不只是保持在寄存器中的計數器,並使用不同的寄存器(如人/啊,DL/DH)爲其他溫度值。

這個序列是特殊的無意義:

mov char,CL 
... a couple insns that don't touch CX 
mov CL,char 

即使你確實需要灑CL,你可能剛剛從[SI]重新加載它,因爲你不修改[SI]。或者直到需要的時候纔將它加載到第一個地方。


與環路大部分功能不環回功能的切入點,因爲它是更有效的做環以外的一些設置,因此該循環本身可以是緊(以一些指令和一些負載/商店)。

+0

謝謝!我寫了「xor CX,CX」,它幫了很多!唯一的問題是,即使它到達「ret」,它也會再次進入「調用rout3」,然後再次通過例程「rout3」,而不是再次通過例程去下一行。你知道這怎麼會發生? –

+0

@ H.Bi:不知道,因爲你還沒有發佈你的其他代碼。 –

+0

嘿,我解決了我的問題。現在我又遇到了另一個問題,所以我用其餘的代碼打開了一個新的問題頁面。如果可以的話,請幫助我:http://stackoverflow.com/questions/36183130/the-output-is-not-displayed-in-its-entirety-8086-assembly謝謝! –