《汇编语言 基于x86处理器》第八章高级过程部分的代码 - 两种规范计算数组元素的和

时间:2023-03-08 17:39:49

▶ 输入 Count 个 32 位有符号整数,计算他们的和

● 代码,使用堆栈传参

 ;// Sum_main.asm,主过程
INCLUDE Irvine32.inc EXTERN PromptForIntegers@0:PROC ; 外部链接声明需要的函数
EXTERN ArraySum@0:PROC, DisplaySum@0:PROC ArraySum EQU ArraySum@0 ; 外部链接声明需要的变量
PromptForIntegers EQU PromptForIntegers@0
DisplaySum EQU DisplaySum@0 Count = ; 输入的变量数 .data
prompt1 BYTE "Enter a signed 32-bit integer: ",
prompt2 BYTE "Sum: ",
array DWORD Count DUP(?)
sum DWORD ? .code
main PROC
call Clrscr ; 清屏 ; PromptForIntegers( addr prompt1, addr array, Count ),提示信息和输入整数
push Count
push OFFSET array
push OFFSET prompt1
call PromptForIntegers ; eax = ArraySum( addr array, Count ),数组求和
push Count
push OFFSET array
call ArraySum
mov sum,eax ; DisplaySum( addr prompt2, sum ),输出结果
push sum
push OFFSET prompt2
call DisplaySum call Crlf
call WaitMsg
exit
main ENDP
END main
 ;// _prompt.asm,提示信息和输入整数
INCLUDE Irvine32.inc .code
PromptForIntegers PROC
arraySize EQU [ebp+] ; 获取参数
ptrArray EQU [ebp+]
ptrPrompt EQU [ebp+]
enter ,
pushad mov ecx, arraySize
cmp ecx,
jle L2
mov edx, ptrPrompt
mov esi, ptrArray L1:
call WriteString
call ReadInt
call Crlf
mov [esi], eax
add esi,
loop L1 L2:
popad
leave
ret ; 3 个参数
PromptForIntegers ENDP
END
 ;// _arrysum.asm,数组求和
INCLUDE Irvine32.inc .code
ArraySum PROC
ptrArray EQU [ebp+]
arraySize EQU [ebp+]
enter ,
push ecx
push esi mov eax, ; 使用循环对数组求和,放入 eax
mov esi, ptrArray
mov ecx, arraySize
cmp ecx,
jle L2 L1:
add eax, [esi]
add esi,
loop L1 L2:
pop esi
pop ecx
leave
ret
ArraySum ENDP
END
 ;// _display.asm,输出结果
INCLUDE Irvine32.inc .code
DisplaySum PROC
theSum EQU [ebp+]
ptrPrompt EQU [ebp+]
enter ,
push eax
push edx mov edx, ptrPrompt
call WriteString
mov eax, theSum
call WriteInt
call Crlf pop edx
pop eax
leave
ret
DisplaySum ENDP
END

● 代码,使用伪指令 PROTO 和 INVOKE 传参

 ;// Sum_main.asm,主过程
INCLUDE sum.inc ; 需要头文件 Count = .data
prompt1 BYTE "Enter a signed 32-bit integer: ",
prompt2 BYTE "Sum: ",
array DWORD Count DUP(?)
sum DWORD ? .code
main PROC
call Clrscr INVOKE PromptForIntegers, ADDR prompt1, ADDR array, Count ; 3 个过程直接传参 INVOKE ArraySum, ADDR array, Count
mov sum, eax INVOKE DisplaySum, ADDR prompt2, sum call Crlf
call WaitMsg
exit
main ENDP
END main
 ;// _prompt.asm,提示信息和输入整数
INCLUDE sum.inc .code
PromptForIntegers PROC, ptrPrompt:PTR BYTE, ptrArray:PTR DWORD, arraySize:DWORD
pushad ; 不用 enter 和 leave mov ecx, arraySize
cmp ecx,
jle L2
mov edx, ptrPrompt
mov esi, ptrArray L1:
call WriteString
call ReadInt
call Crlf
mov [esi], eax
add esi,
loop L1 L2:
popad
ret
PromptForIntegers ENDP
END
 ;// _arrysum.asm,数组求和
INCLUDE sum.inc
.code ArraySum PROC, ptrArray:PTR DWORD, arraySize:DWORD
push ecx
push esi mov eax,
mov esi, ptrArray
mov ecx, arraySize
cmp ecx,
jle L2 L1:
add eax, [esi]
add esi,
loop L1 L2:
pop esi
pop ecx
ret
ArraySum ENDP
END
 ;// _display.asm,输出结果
INCLUDE Sum.inc .code
DisplaySum PROC, ptrPrompt:PTR BYTE, theSum:DWORD
push eax
push edx mov edx, ptrPrompt
call WriteString
mov eax, theSum
call WriteInt
call Crlf pop edx
pop eax
ret
DisplaySum ENDP
END
 ;// sum.inc,头文件
INCLUDE Irvine32.inc PromptForIntegers PROTO, ptrPrompt:PTR BYTE, ptrArray:PTR DWORD, arraySize:DWORD ArraySum PROTO, ptrArray:PTR DWORD, count:DWORD DisplaySum PROTO, ptrPrompt:PTR BYTE, theSum:DWORD