汇编 子程序与操作系统功能调用

1.编写一个求n!的子程序,利用它求1!+2! +3! +4! +5! +6!  的和并输出。

    要求参数的传递分别采用:寄存器传递、全局变量传递,堆栈传递三种不同的方法实现。

    1,2使用寄存器传递,3,4使用全局变量,5,6使用堆栈

include vcIO.inc

.data
	frmStr	  byte	' %5d',10,0
	result dword ?
	param dword ?
.code
 main proc
	;寄存器
	mov eax,1
	call factorialbyregister

	xor ebx,ebx
	add ebx,eax

	mov eax,2
	call factorialbyregister

	add ebx,eax
	
	;变量

	mov param,3
	call factorialbyparam
	add ebx,result

	mov param,4
	call factorialbyparam
	add ebx,result


	;堆栈
	push 5
	call factorialbystack
	pop ecx
	add ebx,eax

	push 6
	call factorialbystack
	pop ecx
	add ebx,eax

	pushad
	    invoke printf,offset frmStr,ebx
	popad

	pushad
	    invoke _getch
    popad
    ret

factorialbyparam proc
	mov eax,param
	mov ecx,eax
	mov eax,1
	again:
		mul ecx
		loop again
	mov result,eax
factorialbyparam endp
ret

factorialbyregister proc
	mov ecx,eax
	mov eax,1
	again:
		mul ecx
		loop again
factorialbyregister endp
ret

factorialbystack proc
	push ebp
	mov ebp,esp
	mov eax,[ebp+8]
	mov ecx,eax
	mov eax,1
	again:
		mul ecx
		loop again
	pop ebp
	ret
factorialbystack endp

main endp
end main

2.编程写一个名为Gcd的求两个数最大公约数子程序,主子程序间的参数传递通过堆栈完成。调用Gcd子程序求出三个双自变量:dvar1dvar2dvar3的最大公约数并输出。

include vcIO.inc

.data
	frmStr byte "gcd: %d",10,0
	dvar1 dword 28
	dvar2 dword 56
	dvar3 dword 98

.code
 main proc
   push dvar1
   push dvar2
   call gcd
   pop ebx
   pop ebx
   push eax
   push dvar3
   call gcd
   pop ebx
   pop ebx

   pushad
		invoke printf,offset frmStr,eax
	popad

   pushad
	invoke _getch
   popad

gcd proc
	push ebp
	mov ebp,esp
	mov eax,[ebp+8]
	mov ebx,[ebp+12]
	.if eax

3.编程写一个名为Bubble的冒泡排序子程序,主子程序间的参数传递通过堆栈完成

include vcIO.inc

.data
	 array  dword  12,4, 168,122,-33,56,78,99,345, 66,-5
	 strfrm byte " %d",10,0
.code
 main proc

 push offset array
  mov eax,(lengthof array-1)
 push eax
 call mybubblesort
 pop eax
 pop eax
 mov eax,(lengthof array)
 mov esi,offset array
 .while eax>0
	pushad
	mov ebx,[esi]
	invoke printf, offset strfrm,ebx
   popad
   add esi,4
   dec eax
 .endw
   pushad
	invoke _getch
   popad
	ret

mybubblesort proc
	push ebp
	mov ebp,esp
	mov ecx,[ebp+8]
	
	.while ecx>0
		mov esi,[ebp+12]
		mov ebx,ecx
		.while ebx>0
			mov eax,[esi]
			mov edx,[esi+4]
			.if eax>edx
				mov [esi],edx
				mov [esi+4],eax
			.endif
			add esi,4
			dec ebx
			
		.endw
		dec ecx
	.endw
	pop ebp
	ret
mybubblesort endp
 main endp
 end main

5.编写一个调用Windows API 的程序,演示MessageBoxA16种不同的组态。

include vcIO.inc

MessageBoxA PROTO,
hwnd:DWORD,
lpText:DWORD,
lpCaption:DWORD,
uType:DWORD
 
ExitProcess PROTO,
dwExitCode:DWORD
.data
     msg byte "hello, gensokyo",10, 0
.code
 main proc
 ;push 0h
	;push 1h
	;push 2h
	;push 3h
	;push 4h
	;push 5h
	;push 6h
	;push 10h
	push 20h
	;push 30h
	;push 40h
	;push 1000h
	;push 2000h
	mov eax,offset msg
	push 0h
	push eax
	push 0h
	call MessageBoxA
	

   pushad
	invoke _getch
   popad
	ret
	
 main endp
 end main

 

你可能感兴趣的:(汇编)