循環程序設計
一、 實驗目的:
- 掌握彙編語言循環程序編寫的基本方法。
- 理解高級語言中的循環的實現方式。
- 理解循環程序對性能的一些影響因素。
二、 實驗內容
- 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