2011-09-17 88 views
0

我有一個很簡單的問題:MASM32:簡單的數組操作

我想存儲字節在MASM32一維數組(我剛開始用它昨天,之前使用C#),然後用一些簡單的修改數學,但我沒有發現任何有用的網絡。

tiles BYTE 12 dup (0) ; array of 12 bytes with value 0 

這是怎麼我在.data段聲明數組,基本上是我想在C#語法做的是:

for(int i = 0; i < tiles.Length; i++) 
    tiles[i] += 2; 

回答

0

我不記得MASM32使用確切的指令,但基本結構應該是這樣的:

mov edi, addr tiles ; might be called offset, some assemblers (notably gas) would use something like lea edi, [tiles] instead 
    mov ecx, 12 ; the count, this could be gotten from an equ, read from a variable etc. 
for_loop: 
    add byte ptr [edi], 2 ; tiles[i] += 2 
    inc edi ; move to next tile 
    dec ecx ; count-- 
    jnz for_loop ; if (count != 0) goto for_loop 

或者,如果你希望它是結構更像是C#代碼:

mov edi, addr tiles 
    sub ecx, ecx ; ecx = 0 
for_loop: 
    cmp ecx, 12 ; ecx < tiles.Length ? 
    jnl done ; jump not less 
    add byte ptr [edi+ecx], 2 ; tiles[i] += 2 
    inc ecx ; i++ 
    jmp for_loop 
done: 

請注意,如果更改tiles的類型,則某些代碼將不得不更改(特別是涉及edi的代碼)。