加载中...

2023春汇编实验(拓展题)


  1. 删除一批数据中的重复数据,删除后的一批数据是互不相同的。
include irvine32.inc
.data
	dat word 1,2,4,3,2,1
	p word 6 dup(?)
.code
main proc
	mov ecx,6
	mov edx,0
	mov edi,0
again1:
	mov esi,0
	mov ax,word ptr dat[edi*2]
again2:
	cmp ax,word ptr p[esi*2]
	jz next
	inc esi
	cmp esi,edx
	jl again2
	mov word ptr p[edx*2],ax
	inc edx
next:
	inc edi
	cmp edi,ecx
	jnz again1
	mov esi,0
pt:
	movzx eax,p[esi*2]
	call writeint
	call crlf
	inc esi
	cmp esi,edx
	jnz pt
	
	exit
	main endp
	end main
  1. 有一批互不相同的有符号数,求出其中两数和的绝对值的最大值,要求输出最大值,以及这两个数。
    第一种(全正或全负):5, 3, 2, 6, 1,和绝对值最大值是11,两个数是5, 6
    第二种(有负有正):-3,2,-2,-4, 1, 5,和绝对值最大值是7,两个数:-3,-4及2,5
此代码有所缺陷,只能输出一组。如果有两组等大的,可以排序后比较头两位和尾两位的和的绝对值,如果等大则两组都输出。(我懒得改了
include irvine32.inc
.data
	dat dword 5,3,2,6,1
	max dword 0
	x dword ?
	y dword ?
	count dword ?
.code
main proc
	mov ecx,lengthof dat
	mov count,ecx
	mov ecx,0
again1:
	mov eax,dat[ecx*4]
	inc ecx
	cmp ecx,count
	jz pt
	mov esi,ecx
again2:
	mov edx,dat[esi*4]
	add edx,eax
	cmp edx,0
	jg cpare
	neg edx
cpare:
	cmp edx,max
	jle next
	mov max,edx
	mov edx,dat[esi*4]
	mov x,edx
	mov y,eax
next:
	inc esi
	cmp esi,count
	jnz again2
	cmp esi,count
	jz again1
pt:
	mov eax,max
	call writeint
	call crlf
	mov eax,x
	call writeint
	call crlf
	mov eax,y
	call writeint
	
	exit
	main endp
	end main

3、从键盘输入两个非0的无符号数,要求用子程序实现求两个数的最大公约数和最小公倍数。

include irvine32.inc
.data
	x dd ?
	y dd ?
	max dd ?
	min dd ?
.code
main proc
	call readint
	mov x,eax
	call readint
	mov y,eax
	
	push x
	push y
	push offset max
	push offset min
	call mpublic
	
	mov eax,max
	call writeint
	call crlf
	mov eax,min
	call writeint
	exit
	main endp
 
 
mpublic proc
	push ebp
	mov ebp,esp
	push eax
	push ecx
	push edx
	push ebx
 
	mov eax,dword ptr [ebp+20]	;x的值
	mov ecx,dword ptr [ebp+16]	;y的值
	mov edx,dword ptr [ebp+12]	;max的地址
	mov ebx,dword ptr [ebp+8]	;min的地址
again:
	cmp eax,ecx
	ja next
	xchg eax,ecx
next:
	sub eax,ecx
	cmp eax,0
	jnz again
	mov [edx],ecx
	
	mov eax,[ebp+20]
	mov ecx,[ebp+16]
	mul cl		;x*y
	mov ecx,[edx]
	div cl		;x*y/max
	mov [ebx],eax
 
	pop ebx
	pop edx
	pop ecx
	pop eax
	pop ebp
	ret 16
	mpublic endp
end main

4、从键盘输入字符串,查找是否包含指定子字符串’abc’

include irvine32.inc
.data
	dat byte 10 dup(?)
	count dword ?
.code
main proc
	mov edi,0
	lea edx,dat
	mov ecx,10
	call readstring
	mov count,eax
	mov esi,0
 
again:
	mov ecx,esi
	cmp dat[ecx],61h
	jnz next
	inc ecx
	cmp dat[ecx],62h
	jnz next
	inc ecx
	cmp dat[ecx],63h
	jnz next
	inc edi
	jmp pt
next:
	inc esi
	cmp esi,count
	jnz again
pt:
	mov eax,edi
	call writeint
	
	exit
	main endp
	end main

文章作者: Lurume
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Lurume !
  目录