2016-06-20 48 views
-2

如何拆分字符串,程序集中的分隔符AT & T?拆分字符串程序集AT&T

這是我有字符串:6,3,1,2431,7,9,1221

欲1添加到所有的,之間的數字。

的結果應該是:7, 4, 2432, 8, 10, 1222

我與ubuntu14工作 - 64位,Intel的CPU和GAS編譯

+2

要做到這一點,最好的方法是編寫一些代碼,掃描字符串中的逗號,將ASCII碼轉換爲數字值並添加任何你想要的。如果您要求我們爲您編寫此代碼,那麼您就錯了。 –

+1

源文件中是否用逗號分隔「字符串」?通過指針或堆棧?字符串元素的大小和類型?你有什麼嘗試? – jolati

回答

1

僞代碼:

; si = input string 
di = output string 
if 0 == [si] jmp NoNumberFound 
NextNumberLoop: 
rax = 0 
ReadLoop: 
bl = [si] & inc si 
if 0 == bl jmp LastNumberFound 
if ',' == bl jmp CommaFound 
rax = rax*10 + (bl-'0') ; extend bl to 64b of course 
jmp ReadLoop 

CommaFound: 
call StoreNumberPlusOne 
[di]=',' & inc di 
jmp NextNumberLoop 

LastNumberFound: 
call StoreNumberPlusOne 
NoNumberFound: 
[di]='0' 
output resulting string and exit. 

StoreNumberPlusOne: 
inc rax (number+1) 
print decimal formatted rax to [di] + make di point after it 
ret 

(DI/SI 64b上平臺指針是當然rsi/rdi等等......它只是顯示算法的僞代碼,不能從字面上理解)


其他選項是在字符串本身進行分析而不用解析數字。

分配足夠的緩存生成的字符串(如果你把輸入作爲n9,輸出緩衝器將幾乎兩倍只要輸入緩衝器,包含n10)。

將輸入字符串複製到outputBuffer,並在其最後一個字符處具有指針ptr

doIncrement = true 
while (outputBuffer <= ptr) { 
    if ',' == [ptr] { 
    if doIncrement { 
     move by 1 byte further everything from ptr+1 onward: 
     in ASM you can do that by simple REP MOVSB, but as the 
     source overlap destination, make sure you use STD (DF=1) 
     together with end pointers. 
     [ptr+1] = '1' 
    } 
    doIncrement = true 
    } else { 
    ; [ptr] should be between '0' to '9' (for valid input) 
    if doIncrement { 
     ++[ptr] 
     if '9'+1 == [ptr] { 
     [ptr] = '0' 
     } else { 
     doIncrement = false 
     } 
    } 
    } 
    --ptr; 
} 
+1

因爲x86-64保證有SSE2可用,所以我可能會傾向於使用'pcmpeqb' /'pmovmskb' /'bsf'來查找下一個','的位置。你甚至可以使用SSE作爲string-> integer('pmaddwd',其矢量爲1,10,100,... place-values)。如果SSSE3 pshufb可用,您可以做更多更酷的事情,例如[將IPv4虛線四字符串轉換爲整數](http://stackoverflow.com/a/31683632/224132)。 –