循环程序设计
一、 实验目的:
- 掌握汇编语言循环程序编写的基本方法。
- 理解高级语言中的循环的实现方式。
- 理解循环程序对性能的一些影响因素。
二、 实验内容
- C语言函数voidmemset(voids,intch,size_tn);是将s中当前位置后面的n个字节用ch替换,通常用于在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法。
编写程序实现类似以上函数的功能(不写成函数形式),将指定的内存中连续N个字节填写成指定的内容,要求:
- 每次填写一个字节
- 每次填写一个字
- 分别用LOOP指令、条件(无条件)转移指令分别实现以上的操作
- 请用冒泡算法对逻辑地址为8000:0000h开始的16个字节排序,要求:
- 冒泡排序的内层循环和外层循环均用LOOP指令实现
- 16个字节数据请分别考虑为符号数和无符号数
- 编写汇编程序完成以下的C语言代码提供的功能(break;continue)
int i=0;
int sum=0;
for(;😉
{
i++;
if(i<30)continue;
sum+=i;
if(sum>1000)break;
}
三、 实验过程
-
include io32.inc
.data
s byte 8 dup(0)
n dword 6
cc byte '8'
.code
start: ;显示原来的s
mov eax,offset s
call dispmsg
;初始化数据
xor ecx,ecx
mov ecx,n
xor esi,esi
mov al,cc
;循环赋值
again:
mov byte ptr s[esi],al
inc esi
next:loop again
;改变后的s
mov eax,offset s
call dispmsg
exit 0
end start
include io32.inc
.data
s byte 8 dup(0)
n dword 6
cc word '13'
.code
start: ;显示原来的s
mov eax,offset s
call dispmsg
;初始化数据
xor ecx,ecx
mov ecx,n
xor esi,esi
mov ax,cc
;循环赋值
again:
mov word ptr s[esi],ax
inc esi
inc esi
next:loop again
;改变后的s
mov eax,offset s
call dispmsg
exit 0
end start
2. include io32.inc
.data
s byte 0,1,2,3,4,5,6,7,8,9,0AH,0BH,0CH,0DH,0EH,0FH
.code
start: ;循环前
xor eax,eax
xor ebx,ebx
mov ecx,16
again:
mov al,byte ptr s[ebx]
call dispuid
mov al,0
call dispc
inc ebx
next:loop again
;嵌套循环
xor ebx,ebx
mov ecx,16
againw:
xor edx,edx
push ecx
sub ecx,ebx
againn:
mov al,byte ptr s[edx]
mov ah,byte ptr s[edx+1]
cmp al,ah
jae lable
mov byte ptr s[edx],ah
mov byte ptr s[edx+1],al
lable:inc edx
nextn:loop againn
pop ecx
inc ebx
nextw:loop againw
;循环后
xor eax,eax
xor ebx,ebx
mov ecx,16
againh:
mov al,byte ptr s[ebx]
call dispuid
mov al,32
call dispc
inc ebx
nexth:loop againh
exit 0
end start
-
include io32.inc
.data
a dword 0
sum dword 0
.code
start:
xor eax,eax
xor ebx,ebx
mov eax,a
mov ebx,sum
again:
inc eax
cmp eax,30
jb again
add ebx,eax
cmp ebx,1000
ja done
jmp again
done:
mov a,eax
mov sum,ebx
mov eax,sum
call dispuid
exit 0
end start