發表文章

目前顯示的是 10月, 2008的文章

[MASM] IO Patterns

讀取一串由空格分隔的整數 ex. 10 20 30 40 50 .data buffer BYTE 512 DUP(0) array DWORD 30 DUP(0) len DOWRD 0 ;//陣列長度 .code mov edx,OFFSET buffer mov ecx, (SIZEOF buffer) call ReadString ;//Read string to buffer mov esi,OFFSET buffer mov edi,OFFSET intArray ;//set addr, esi=buffer, edi=array mov eax,0 ;//init eax start: movzx edx,BYTE PTR[esi] cmp edx,0 ;//end of string je over cmp edx,32 ;//compare to space je stateB jmp stateA stateA: ;//is digit sub edx,48 ;//ascii edx-'0' mov ebx,edx ;//because mul use edx, back up edx mov ecx,10 mul ecx ;//eax=eax*10 mov edx,ebx add eax,edx inc esi jmp start stateB: ;//is space mov [edi],eax ;//把eax存進array裡 mov eax,0 add edi,4 inc esi inc len jmp start over: mov [edi],eax inc len 觀念很簡單 edx是Input String的位址,結果則是存在eax裡 每次從edx抓一個byte進來,判斷是1.數字 或2.空白 1.如果是數字就跳到 StateA,把eax原來的數字x10,然後加上新的數 2.如果是空白,代表一個整數結束,跳到StateB,將eax存進intArray裡,重新開始另一個循環直到edx讀到0,也就是字串的結尾為止。

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 //sta