【汇编语言实验三】有符号整数排序

1. 实验名称

有符号整数排序

2. 实验目的

熟悉逻辑指令,移位指令,比较指令与条件判断指令,并灵活运用这些指令。

3. 实验要求

从键盘输入多个有符号整数并进行排序,然后查找用户再次输入的任何一个整数,并以二进制编码输出其在排序结果中的下标。

4. 实验内容

  1. 从键盘接收多个有符号整数
  2. 对输入的多个整数进行排序
  3. 再次接收用户输入的一个整数,并在排序结果中查找;
  4. 以二进制编码输出下标。若未找到,则输出提示。

(可选)更高要求:(涉及到乘法指令)
在第3)步输入一个整数时,不使用ReadInt等别人写的过程,而是自己写一个读入有符号整数的过程(此时可以使用readkey读取输入的字符。注意从键盘读入的均是ASCII代码字符)。

提示:
可以先做1)、3)、4)步,最后再加入第2)步。

5. 实验步骤或源代码、结果

1. 实验步骤

查找数据成功

1

查找数据不存在时

2

2. 实验源代码、结果

INCLUDE Irvine32.inc

.data
str1	BYTE	"How many integers you want input? ", 0				
str2	BYTE	"Please input a integer: ", 0
str3	BYTE	"The index of the integer is: ", 0
str4	BYTE	"The integer you want to seek is: ", 0
str5	BYTE	"Cannot find the integer.", 0
IntegerSize DWORD 0
Array DWORD 0FFFFH DUP(?)

.code
main PROC
	call InputSize
	call InputArray
	call SortIntegers
	mov edx, offset str4
	call WriteString
	call ReadInt
	call BinSeek
	cmp eax, -1
	je NotFound
	mov edx,offset str3
	call WriteString
	add eax, 1
	call WriteBinB
	call Crlf
	jmp ProcessExit
NotFound:
	mov edx, offset str5
	call WriteString
	call Crlf
ProcessExit:
	exit
main ENDP
InputSize PROC USES edx eax
	mov edx, offset str1
	call WriteString
	call ReadDec
	mov IntegerSize, eax
	ret
InputSize ENDP

InputArray PROC USES eax ecx esi
	mov esi, 0
	mov eax, 1
	mov ecx, IntegerSize
L1:
	mov edx, offset str2
	call WriteString
	push eax
	call ReadInt
	mov Array[esi], eax
	add esi, TYPE Array
	pop eax
	loop L1
InputArrayRet:
	ret
InputArray ENDP 

SortIntegers PROC USES ecx eax esi edi
	mov ecx, IntegerSize
	dec ecx
L1:
	mov esi, ecx
	mov eax, esi
	call MulTypeArray
	mov esi, eax
	mov edi, esi
L2:
	mov eax, Array[esi]
	sub edi, TYPE Array
	cmp eax, Array[edi]
	jge L2Ret
	xchg eax, Array[edi]
	mov Array[esi], eax
L2Ret:
	cmp edi, 0
	jg L2
	loop L1
	
	ret
SortIntegers ENDP

MulTypeArray PROC USES edx
	sal eax, 2
	ret
MulTypeArray ENDP

DivTypeArray PROC USES edx
	sar eax, 2
	ret
DivTypeArray ENDP

BinSeek PROC USES edx esi edi ecx
	mov ecx, eax
	mov esi, 0
	mov edi, IntegerSize
	dec edi
L1:
	cmp edi, esi
	jb NotFound
	mov edx, esi
	add edx, edi
	sar edx, 1
	mov eax, edx
	call MulTypeArray
	mov edx, eax
	cmp ecx, Array[edx]
	jl LessDeal
	je Found
	mov eax, edx
	call DivTypeArray
	inc eax
	mov esi, eax
	jmp L1
LessDeal:
	mov eax, edx
	dec eax
	call DivTypeArray
	mov edi, eax
	jmp L1
Found:
	mov eax, edx
	call DivTypeArray
	jmp BinSeekRet
NotFound:
	mov eax, -1
BinSeekRet:
	ret
BinSeek ENDP

end main

6. 实验结论和心得体会

  • 通过输入想要输入的数字个数来确定数组的大小,不能像高级语言那种一次性输完;
  • 熟悉了逻辑指令,移位指令,比较指令与条件判断指令,并灵活运用这些指令;
  • 数组的索引以4字节增加;
  • 冒泡排序中要注意保存和恢复外循环计数器;
  • 确定了索引值后以二进制值方式输出,但是作者提供的WriteBinB是输出eax的值,不能从第一个1开始输出,这里下来还需要考虑进行重写。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值