Assembly Language Patterns

計算陣列長度

list Byte 10,20,30,40 //list 是array的起始位址
ListSize = ($ - list) //ListSize是一個Constant
//$代表Current Location counter

list DWORD 1000,2000,3000
ListSize = ($-list)/4 //DWORD大小是4Byte,所以除以四

雙層迴圈

mov ecx,5  //外圈次數
L1:
    mov count,ecx
    mov ecx, 7   //內圈次數
    L2:
      //do something here
      loop L2
      mov ecx,count
loop L1


if statement

if (op1==op2){ statement1; }
else {statement2; }

cmp op1,op2 // op1==op2
je _true     // jump if equal 相等的話就跳TRUE label
jmp _false   // 否則就無條件跳到FALSE label
_true:
    //statement1
    jmp next //跳過False部份
_false:
    //statement2
next:

While loop

while( var1>var2 ){
//statement
}
mov eax,var1
_while:
    cmp eax,var2
    jle endwhile //jmp if less and equl
    //原本是var1>var2,條件成立就進while
    //這裡改成var1<=var2,條件成立就離開while     
    //statement     
    jmp _while 
_endwhile:  

for loop

for(int i=0;i<9;i++){ statement; }
mov ecx,0 //把ecx當作計數器i
_for:
    cmp ecx,9
    jge _endfor //jump if greater & equal
    //statement;
    inc ecx     //ecx++
    jmp _for
_endfor:

留言

這個網誌中的熱門文章

UVa 10125 Sumsets

讀書心得: 你以為你以為的就是你以為的嗎?

讀書心得: 撒哈拉的故事