Created
November 15, 2015 09:11
-
-
Save anonymous/61cd2f27b594ac1c29ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;----------------------------------------------------- | |
| ; | |
| ; | |
| ; <My MASM Happy File> | |
| ; Edit by whuang022 | |
| ; 2015.11.08 | |
| ; PROC for Exam | |
| ; | |
| ;----------------------------------------------------- | |
| ;1.call BetterSetTextColor ;設定字體顏色(eax前景色 ebx 背景色) | |
| ;2.call BetterRandomRange ;設定亂數(ebx 下界 eax上界) | |
| ;3.call RevArryDWORD ;反轉陣列(edx OFFSET 到arry ebx元素數 DWORD可改) | |
| ;4.call SortList ;氣泡排序陣列 (push OFFSET 到arry, push userNum 元素數) | |
| ;5.call displayArryDWORD ;依序顯示陣列元素(esi OFFSET 到arry ecx 元素數 DWORD可改) | |
| ;6.call ArraySum ;計算陣列元素總和(esi OFFSET 到arry ecx 元素數 DWORD可改) | |
| ;7.call BetterRandomRange8bit ;設定亂數(bl 下界 al上界) | |
| ;8.call generateKey ;產生亂數字串eax是選擇器1=數字 2=大寫 3=小寫 ecx元素數 esi OFFSET到str1) | |
| ;9.call Fib ;產生費氏數列(112358...) 數量=ebx元素數 esi OFFSET到要放的地方 | |
| ;10.call FileIO ;把資料寫出到檔案(ecx 檔案大小 edx OFFSET到檔名的str array ebx OFFSET到資料) | |
| ;11.call strcpy ;複製字串(.data 固定變數為 source 和 target) | |
| ;12.call AllStar ;在螢幕上噴星星 | |
| ;----------------------------------------------------- | |
| ;----------------------------------------------------- | |
| BetterSetTextColor PROC USES eax ebx edx | |
| ; | |
| ; Set Text Color more easier | |
| ; Receives: EAX for text front colors(0-15) | |
| ; EBX for text background colors (0-15) | |
| ; Returns: void | |
| ;----------------------------------------------------- | |
| ;顏色公式: | |
| ;顏色設定 = 前景色 + 背景色*16 | |
| mov edx,16 ;乘數 | |
| mul edx ;eax*16 EAX放底色 | |
| add eax,ebx ;EAX+EBX(放字色) | |
| call SetTextColor ;設定顏色 | |
| ret | |
| BetterSetTextColor ENDP | |
| ;----------------------------------------------------- | |
| ;----------------------------------------------------- | |
| BetterRandomRange PROC | |
| ; Generates a random integer between M and N-1. | |
| ; Receives: | |
| ; EBX = lower range (M) | |
| ; EAX = upper range (N) | |
| ; Returns: | |
| ; EAX = pseudorandom integer | |
| sub eax,ebx ; calc difference between ranges | |
| call RandomRange | |
| add eax,ebx ; scale to desired range | |
| ret | |
| BetterRandomRange ENDP | |
| ;------------------------------------------------------- | |
| ;------------------------------------------------------- | |
| RevArryDWORD PROC USES eax esi ecx ebx edx | |
| ; | |
| ; Receives edx to the OFFSET of arry | |
| ; ebx to the element number | |
| ; Change DWORD if you want other types to display | |
| ;------------------------------------------------------- | |
| mov ecx,ebx ; 把元素數量設到ecx上 | |
| mov esi,edx ; 把偏移位址設到esi上 | |
| L1: | |
| mov eax,[esi] ; 把esi指的元素移動到eax上 | |
| push eax ; 把eax Push 起來 | |
| add esi,SIZEOF DWORD ; 向前移動一個元素的偏移量 | |
| loop L1 | |
| mov ecx,ebx ; 對迴圈重新賦值 | |
| mov esi,edx ; 把esi指的元素對到arry上 | |
| L2: | |
| pop eax ; 把eax值pop出來 | |
| mov [esi],eax ; 把esi指的元素改成pop出來的數字 | |
| add esi,SIZEOF DWORD ; 向前移動一個元素的偏移量 | |
| loop L2 | |
| ret | |
| RevArryDWORD ENDP | |
| ;------------------------------------------------------- | |
| ;------------------------------------------------------- | |
| ;Sorts the contents of an integer array | |
| ; Receives parameters on the system stack (in order pushed) | |
| ; Array | |
| ; Array Size | |
| ;registers: none | |
| ;------------------------------------------------------- | |
| sortList PROC | |
| pushad | |
| mov ebp, esp | |
| mov ecx, [ebp+36] | |
| mov edi, [ebp+40] | |
| dec ecx ;ecx < request-1 | |
| mov ebx, 0 ;ebx=k | |
| ;for(k=0; k<request-1; k++) | |
| outerLoop: | |
| mov eax, ebx ;eax=i=k | |
| mov edx, eax | |
| inc edx ;edx=j=k+1 | |
| push ecx | |
| mov ecx, [ebp+36] ;ecx < request | |
| ;for(j=k+1; j<request; j++) | |
| innerLoop: | |
| mov esi, [edi+edx*4] | |
| cmp esi, [edi+eax*4] | |
| jle skip | |
| mov eax, edx | |
| skip: | |
| inc edx | |
| loop innerLoop | |
| ;swap elements | |
| lea esi, [edi+ebx*4] | |
| push esi | |
| lea esi, [edi+eax*4] | |
| push esi | |
| call exchange | |
| pop ecx | |
| inc ebx | |
| loop outerLoop | |
| popad | |
| ret 8 | |
| sortList ENDP | |
| ;------------------------------------------------------- | |
| ;------------------------------------------------------- | |
| ; Exchange k and i | |
| ; Receives parameters on the system stack (in order pushed) | |
| ; array[k] | |
| ; array[i] | |
| ;registers: none | |
| ;------------------------------------------------------- | |
| Exchange PROC | |
| pushad | |
| mov ebp,esp | |
| mov eax, [ebp+40] ;array[k] low number | |
| mov ecx, [eax] | |
| mov ebx, [ebp+36] ;array[i] high number | |
| mov edx, [ebx] | |
| mov [eax], edx | |
| mov [ebx], ecx | |
| popad | |
| ret 8 | |
| Exchange ENDP | |
| ;------------------------------------------------------- | |
| ;------------------------------------------------------- | |
| displayArryDWORD PROC USES eax esi ecx | |
| ; | |
| ; Receives esi to the OFFSET of arry | |
| ; ecx to the element number | |
| ; Change DWORD if you want other types to display | |
| ;------------------------------------------------------- | |
| L1: | |
| mov eax,[esi] ; 把esi指的元素移動到eax上 | |
| call WriteDec ; 寫到螢幕上 | |
| add esi,SIZEOF DWORD ; 向前移動一個元素的偏移量 | |
| call Crlf ; 換行 | |
| loop L1 | |
| ret | |
| displayArryDWORD ENDP | |
| ;------------------------------------------------------- | |
| ;----------------------------------------------------- | |
| ArraySum PROC USES esi ecx | |
| ; | |
| ; Calculates the sum of an array of 32-bit integers. | |
| ; Receives: ESI points to the array, ECX = number | |
| ; of array elements | |
| ; Returns: EAX = sum of the array elements | |
| ;----------------------------------------------------- | |
| mov eax,0 ; set the sum to zero | |
| L1: add eax,[esi] ; add each integer to sum | |
| add esi,TYPE DWORD ; point to next integer | |
| loop L1 ; repeat for array size | |
| ret ; sum is in EAX | |
| ArraySum ENDP | |
| ;----------------------------------------------------- | |
| ;----------------------------------------------------- | |
| BetterRandomRange8bit PROC | |
| ; Generates a random integer between M and N-1. | |
| ; Receives: | |
| ; BL = lower range (M) | |
| ; AL = upper range (N) | |
| ; Returns: | |
| ; AL = pseudorandom integer | |
| sub al,bl ; calc difference between ranges | |
| call RandomRange | |
| add al,bl ; scale to desired range | |
| ret | |
| BetterRandomRange8bit ENDP | |
| ;----------------------------------------------------- | |
| ;------------------------------------------------------- | |
| generateKey PROC USES eax ecx ebx | |
| ; Receives parameters on | |
| ; eax (choose Type) | |
| ; 1=radom number only | |
| ; 2=radom A-Z | |
| ; 3=radom a-z | |
| ; ecx (elements num) | |
| ;------------------------------------------------------- | |
| pushf | |
| cmp eax , 1 ; 兩運元相減且只改變旗標 | |
| je L1 ; 相等跳至L1 | |
| jmp L2 ; 否則跳至L2 | |
| L1: | |
| i1: ; 第二層迴圈起點 | |
| mov ebx,0 ; ebx放亂數的最小值(0) | |
| mov eax,9 ; eax放亂數的最大值(9) | |
| call BetterRandomRange ; 呼叫BetterRandomRange產生亂數 | |
| add eax,48 ; 偏移48到ASCII字元的數字(48-57) | |
| mov [esi],eax ; 把esi的值放進亂數字元 | |
| inc esi ; 陣列向前+1 | |
| loop i1 ; 第二層迴圈折返點 | |
| L2: | |
| ; | |
| cmp eax , 2 ; 兩運元相減且只改變旗標 | |
| je L3 ; 相等跳至L3 | |
| jmp L4 ; 否則跳至L4 | |
| L3: | |
| i2: ; 第二層迴圈起點 | |
| mov ebx,0 ; ebx放亂數的最小值(0) | |
| mov eax,25 ; eax放亂數的最大值(25) | |
| call BetterRandomRange ; 呼叫BetterRandomRange產生亂數 | |
| add eax,65 ; 偏移65到ASCII字元的數字(A-Z) | |
| mov [esi],eax ; 把esi的值放進亂數字元 | |
| inc esi ; 陣列向前+1 | |
| loop i2 ; 第二層迴圈折返點 | |
| L4: | |
| ; | |
| cmp eax , 3 ; 兩運元相減且只改變旗標 | |
| je L5 ; 相等跳至L3 | |
| jmp L6 ; 否則跳至L4 | |
| L5: | |
| i3: ; 第二層迴圈起點 | |
| mov ebx,0 ; ebx放亂數的最小值(0) | |
| mov eax,25 ; eax放亂數的最大值(25) | |
| call BetterRandomRange ; 呼叫BetterRandomRange產生亂數 | |
| add eax,97 ; 偏移97到ASCII字元的數字(a-z) | |
| mov [esi],eax ; 把esi的值放進亂數字元 | |
| inc esi ; 陣列向前+1 | |
| loop i3 ; 第二層迴圈折返點 | |
| L6: | |
| popf | |
| ret | |
| generateKey ENDP | |
| ;------------------------------------------------------- | |
| ;------------------------------------------------------- | |
| TITLE OUT PROC | |
| INCLUDE Irvine32.inc | |
| ;這篇示範一些標準輸出的函式 | |
| .data | |
| str1 BYTE "Hello world",0 | |
| source BYTE "hello",0 ;要被複製的字串來源 | |
| target BYTE SIZEOF source DUP (?);要複製到的位置 | |
| .code | |
| main PROC | |
| mov edx,OFFSET str1 ;把要顯示的字串的OFFSET丟掉edx | |
| call WriteString ;call WriteString | |
| call Crlf ;換行 | |
| mov eax,12345 ;把要顯示的數字放到eax | |
| call WriteInt ;把整數用帶正負號的方式印出 | |
| call Crlf ;換行 | |
| call WriteDec ;把整數用不帶正負號的方式印出 | |
| call Crlf ;換行 | |
| call WriteHex ;把整數用16進位表示印出 | |
| call Crlf ;換行 | |
| call WriteBin ;把整數用2進位表示印出 | |
| call Crlf ;換行 | |
| mov al,'A' ;把要顯示的字元放進al | |
| call WriteChar ;顯示字元印出 | |
| call Crlf ;換行 | |
| call WaitMsg ;顯示"Press [Enter] to continue..." | |
| call Crlf ;換行 | |
| call StrLength ;計算offset在edx的字串長度(此時為Hello world) 並放在eax | |
| call WriteInt ;顯示字串長度 | |
| INVOKE Str_ucase, ADDR str1;把字串小寫轉大寫 | |
| call Crlf ;換行 | |
| call WriteString ;顯示轉換好大小寫的str | |
| ;顯示數字放在eax 顯示字元放在al 顯示字串放在.data 呼叫Offset放到edx | |
| INVOKE Str_copy, ADDR source,ADDR target;複製字串 | |
| call Crlf ;換行 | |
| mov edx,OFFSET target ;複製的結果OFFEST移動到edx | |
| call WriteString ;顯示複製的字串 | |
| exit | |
| main ENDP | |
| END main | |
| ;------------------------------------------------------- | |
| ;----------------------------------------------------- | |
| Fib PROC USES eax ebx ecx | |
| ; Receives: ECX for element number | |
| ; Returns: ESI point to array | |
| ;----------------------------------------------------- | |
| mov eax, 0 ; f[0] = 0 | |
| mov ebx, 1 ; f[1] = 1 | |
| L1: ; eax = f[i-2], ebx = f[i-1] | |
| mov [esi],ebx ; write f[i] | |
| add esi,SIZEOF DWORD ; next esi | |
| add eax, ebx ; f[i] = f[i-1]+f[i-2] | |
| xchg eax, ebx ; swap(eax, ebx) | |
| loop L1 | |
| ret | |
| Fib ENDP | |
| ;------------------------------------------------------- | |
| ;----------------------------------------------------- | |
| FileIO PROC USES edx eax ecx ebx | |
| ; | |
| ; Receives edx to the OFFSET of arry filename | |
| ; ecx to the element number | |
| ; ebx to the OFFSET of arry data | |
| ;------------------------------------------------------- | |
| push ecx | |
| call CreateOutputFile | |
| cmp eax, INVALID_HANDLE_VALUE | |
| je file_error ; display error message | |
| mov edx,ebx | |
| pop ecx | |
| call WriteToFile | |
| call CloseFile | |
| file_error: | |
| invoke ExitProcess, 0 ; end program | |
| ret | |
| FileIO ENDP | |
| ;----------------------------------------------------- | |
| ;------------------------------------------------------- | |
| strcpy PROC USES ecx esi eax | |
| ; Receives | |
| ; .data must have | |
| ; source BYTE "Yout text here" , 0 | |
| ; target BYTE SIZEOF source DUP(0) , 0 | |
| ;------------------------------------------------------- | |
| mov ecx ,SIZEOF source ; 迴圈次數 | |
| mov esi,0 | |
| L1: | |
| mov al,source[esi] ; 從來源取得字元 | |
| mov target[esi],al ; 儲存在目標 | |
| inc esi | |
| loop L1 | |
| ret | |
| strcpy ENDP | |
| ;------------------------------------------------------- | |
| ;-------------------ALLSTAR--PROC-------------------------------- | |
| ; | |
| ; made by 410206121 | |
| ; | |
| ;---------------------------------------------------------------- | |
| ;----------------------------------------------------- | |
| AllStar PROC USES eax ebx ecx edx | |
| ; make Stars | |
| ; Receives: must have this data, choose color by colorStart and colorEnd | |
| ; ,star typeby startext ,time by msecond | |
| ; .data | |
| ; startext BYTE "*",0 ;星星符號 | |
| ; colorStart DWORD 1 ;色值下限1(不要黑色) | |
| ; colorEnd DWORD 16 ;色值上限15 | |
| ; msecond DWORD 200 ;200 milliseconds | |
| ; Returns: void | |
| ;----------------------------------------------------- | |
| L1: ; 迴圈進入點 | |
| mov bl,0 ; 亂數最小值0 | |
| mov al,80 ; 亂數最大值80 | |
| call BetterRandomRange8bit ; 呼叫BetterRandomRange 產生亂數於al | |
| mov dl,al ; 設定亂數於列 | |
| mov bl,0 ; 亂數最小值0 | |
| mov al,25 ; 亂數最大值25 | |
| call BetterRandomRange8bit ; 呼叫BetterRandomRange8bit產生亂數於al | |
| mov dh,al ; 設定亂數於行 | |
| call Gotoxy ; 定位游標位置 | |
| push ebx ; 因為要用到ebx eax跑亂數和設色所以先push起來 | |
| push eax ; 因為要用到ebx eax跑亂數和設色所以先push起來 | |
| mov ebx,colorStart ; 設定起始色 | |
| mov eax,colorEnd ; 設定結束色 | |
| call BetterRandomRange ; 產生色區間的亂數 | |
| mov ebx,eax ; 把產生的值從eax放到ebx | |
| mov eax,0 ; 把eax給0(滿天星的字底色是黑的=0) | |
| call BetterSetTextColor ; 設定顏色 | |
| pop ebx ; 把原本的資料pop出來 | |
| pop eax ; 把原本的資料pop出來 | |
| mov edx,OFFSET startext ; 設定OFFSET要顯示的字串 | |
| call WriteString ; 寫到螢幕 | |
| mov eax ,msecond ; 設定間格時間 | |
| call Delay; ; 呼叫Delay 200 milliseconds | |
| loop L1 ; 迴圈折返點 | |
| call Crlf ; 換行 | |
| mov eax,0 | |
| mov ebx,15 ; 色彩歸位 | |
| call BetterSetTextColor | |
| ret | |
| AllStar ENDP | |
| ;----------------------------------------------------- | |
| ;----------------------------------------------------- | |
| BetterSetTextColor PROC USES eax ebx edx | |
| ; | |
| ; Set Text Color more easier | |
| ; Receives: EAX for text front colors(0-15) | |
| ; EBX for text background colors (0-15) | |
| ; Returns: void | |
| ;----------------------------------------------------- | |
| ;顏色公式: | |
| ;顏色設定 = 前景色 + 背景色*16 | |
| mov edx,16 ;乘數 | |
| mul edx ;eax*16 EAX放底色 | |
| add eax,ebx ;EAX+EBX(放字色) | |
| call SetTextColor ;設定顏色 | |
| ret | |
| BetterSetTextColor ENDP | |
| ;----------------------------------------------------- | |
| ;----------------------------------------------------- | |
| BetterRandomRange PROC | |
| ; Generates a random integer between M and N-1. | |
| ; Receives: | |
| ; EBX = lower range (M) | |
| ; EAX = upper range (N) | |
| ; Returns: | |
| ; EAX = pseudorandom integer | |
| sub eax,ebx ; calc difference between ranges | |
| call RandomRange | |
| add eax,ebx ; scale to desired range | |
| ret | |
| BetterRandomRange ENDP | |
| ;------------------------------------------------------- | |
| ;------------------------------------------------------- | |
| BetterRandomRange8bit PROC | |
| ; Generates a random integer between M and N-1. | |
| ; Receives: | |
| ; BL = lower range (M) | |
| ; AL = upper range (N) | |
| ; Returns: | |
| ; AL = pseudorandom integer | |
| sub al,bl ; calc difference between ranges | |
| call RandomRange | |
| add al,bl ; scale to desired range | |
| ret | |
| BetterRandomRange8bit ENDP | |
| ;------------------------------------------------------- | |
| ;-------------------ALLSTAR--ENDP-------------------------------- | |
| ;------------------------ | |
| @暫存器大小: | |
| 8 bit: | |
| al | |
| ah | |
| 16 bit: | |
| ax | |
| 32 bit: | |
| eax | |
| 圖: | |
| ------------------------ | |
| | AH | AL | | |
| ------------------------ | |
| AX | |
| EAX | |
| ;------------------------ | |
| 旗標 | |
| carry flag | |
| CF | |
| 0 CF=1,CY | |
| CF=0,NC | |
| 當運算發生進位或借位時,ZF 被設為 1;反之設為 0。例如兩數相加: | |
| mov ax,8000h | |
| add ax,8000h | |
| 8000h+8000h 應為 10000h,發生進位,雖然 AX 為 0,但是會使 CF 設為一,表示進位。 | |
| 同位旗標 | |
| parity flag | |
| PF | |
| 2 PF=1,PE | |
| PF=0,PO 運算的結果換成二進位後,最低的 8 個位元中,若有偶數個 1,則此位元設為 1,反之為 0。 | |
| 輔助進位旗標 | |
| auxiliary carry flag | |
| AF | |
| 4 AF=1,AC | |
| AF=0,NA 當運算過程中,第 3 位元與第 4 位元之間發生進位或借位時,AF 被設為 1,否則被設為 0,常用於 BCD 的運算。 | |
| 零旗標 | |
| zero flag | |
| ZF | |
| 6 ZF=1,ZR | |
| ZF=0,NZ 運算結果為零時,ZF 會被設定為 1。若比較相同兩數, ZF 也會被設為一,若比較不相同的兩數,ZF 會被清除為零。例如: | |
| mov ax,8000h | |
| add ax,8000h | |
| 相加後,AX 為零,故 ZF 設為一。 | |
| 符號旗標 | |
| sign flag | |
| SF | |
| 7 SF=1,NG | |
| SF=0,PL 運算結果的最高位元為 1 時,SF 會被設為 1 ( 表示負數 ),否則被清除。 | |
| 陷阱旗標 | |
| trap flag | |
| TF | |
| 8 用於單步追蹤除錯時,所以也稱為追蹤旗標 ( trace flag ),例如在 MS-DOS 的 DEBUG 中,就是利用 TF 達到單步追蹤的目的。當 TF 設為一時,每執行一個指令便會發生中斷,此中斷就將執行該指令後暫存器列出。 | |
| 中斷旗標 | |
| interrupt flag | |
| IF | |
| 9 IF=1,EI | |
| IF=0,DI 當 IF 被設定時,可遮罩的硬體中斷才能使 CPU 產生中斷效果;反之所有可遮罩的硬體中斷均會被抑制。但是不可遮罩中斷以及 CPU 產生的例外卻完全不受 IF 影響。 | |
| 方向旗標 | |
| direction flag | |
| DF | |
| 10 DF=1,DN | |
| DF=0,UP 當 DF 被清除時 ( 即 DF=0 ),處理字串的索引暫存器會遞增,往高位址方向處理,反之則遞減。 | |
| 溢位旗標 | |
| overflow flag | |
| OF | |
| 11 OF=1,OV | |
| OF=0,NV | |
| 當運算結果無法容納於目的運算元中,此旗標會被設為一。大致可分三種情形:(1)兩同號數相加或兩異號數相減,(2)乘除運算時,所得的積或商超過運算元存放範圍,在移位或旋轉指令時,最高位元值受到更動 ( 0 變成 1 或 1 變成 0 )。 | |
| ;------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment