彙編-函數定義

函數定義:

TITLE Add and Subtract


; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc
TAB = 9

.code
Rand1 PROC
    mov ecx, 2
L1:
    call Random32
    call WriteDec
    mov al, TAB
    call WriteChar
    loop L1
    ret  ;必須有ret
Rand1 ENDP

Rand2 PROC
    mov ecx, 2
L1:
    mov eax, 100
    call RandomRange
    sub eax, 50
    call WriteInt
    mov al, TAB
    call WriteChar
    loop L1
    
Rand2 ENDP
main PROC
    call Randomize
    call Rand1
    call Rand2
    INVOKE ExitProcess, 0
main ENDP


END main


USES操作符:

.code
main PROC
    mov eax, 10h
    call fun1

    INVOKE ExitProcess, 0
main ENDP

fun1 PROC USES eax ebx
    call DumpRegs
    ret
fun1 ENDP

在生成的PE文件中,fun1的開頭有push eax push ebx 結尾有pop ebx  pop eax


3個輸入整數的相加

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc
TAB = 9
.data
aName BYTE "Lincoln",0
nameSize = ($-aName) - 1
INTEGER_COUNT = 3
str1 BYTE "enter a :", 0
str2 BYTE "The sum is:", 0
array DWORD INTEGER_COUNT DUP(?)

.code
main PROC
    call Clrscr
    mov esi, OFFSET array
    mov ecx, INTEGER_COUNT
    call PromptForIntegers
    call ArraySum
    call DisplaySum
    INVOKE ExitProcess, 0
main ENDP

PromptForIntegers PROC USES ecx edx esi
    mov edx, OFFSET str1
    L1:
    call WriteString
    call ReadInt
    call Crlf
    mov [esi], eax
    add esi, TYPE DWORD
    loop L1
    ret
PromptForIntegers ENDP

ArraySum PROC USES esi ecx
    mov eax, 0
    L1:
    add eax, [esi]
    add esi, TYPE DWORD
    loop L1
    ret
ArraySum ENDP

DisplaySum PROC USES edx
    mov edx, OFFSET str2
    call WriteString
    call WriteInt
    call Crlf
    ret
DisplaySum ENDP
END main



TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc


.data
data1 dword 10h
data2 dword 20h

.code
addTwo PROC USES ebx ecx,
    pval1:DWORD,
    pval2:DWORD
        LOCAL pval3:DWORD
    mov pval3, 12h
    mov eax, pval1
    add eax, pval2
    add eax, pval3
    ret
addTwo ENDP

main PROC
    push data1
    push data2
    call addTwo
    call WriteDec
    push 0
    call ExitProcess
main ENDP
END main


先設置本地參數,後push 需要保存的寄存器,訪問局部變量的時候,同樣是用ebp作爲基址寄存器


local 定義數組

LOCAL pt[10]:BYTE


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章