實驗目的
掌握串操作指令的用法,注意DF標識位以及源串、目標串的存儲和改變。
掌握位運算指令的用法,注意目標串的存儲和改變。
實驗要求:
(1)編寫子程序
(2)在主程序中調用子程序
(3)實現主程序與子程序的參數傳遞
(4)實現串指令的調用。
(5)練習使用位運算
實驗一
題目
寫一個過程statA,該過程統計一個串(串中元素爲字長)中有多少正整數,該過程有堆棧三個參數傳遞。
(1)串的地址
(2)串中元素的個數。(傳遞參數的長度爲字長)
(3)保存結果的一個變量的地址。
源碼
;Program:統計串中正整數的數量
;Author:Nonoas
;Date:20191210
.386
.MODEL FLAT
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
INCLUDE io.h ; header file for input/output
cr EQU 0dh ; carriage return character
Lf EQU 0ah ; line feed
.STACK 4096 ; reserve 4096-byte stack
.DATA ; reserve storage for data
prtInput byte cr,lf,"Original string:",0
string word 80 dup (?)
prtResult byte cr,lf,"The quantity of positive integer is:",0
result word ?
.CODE ; start of main program code.
strlen PROC NEAR32 ;Get the length of string
push ebp
mov ebp,esp
pushf
push ebx
sub eax,eax
mov ebx,[ebp+8]
whileChar:
cmp byte ptr [ebx],0
je endWhileChar
inc eax
inc ebx
jmp whileChar
endWhileChar:
pop ebx
popf
pop ebp
ret 4
strlen ENDP
;count the number of string
statA PROC NEAR32
push ebp
mov ebp,esp
pushf
mov ecx,[ebp+14] ;length
mov ebx,[ebp+10] ;addr
mov dx,[ebp+8] ;count
forFind:
mov al,[ebx]
cmp al,'0'
jb endLoop
cmp al,'9'
ja endLoop
inc dx
endLoop:
inc ebx
loop forFind
popf
pop ebp
ret 10
statA ENDP
_start:
output prtInput
input string,80
lea eax,string
push eax
call strlen ;Get the length of string
mov ecx,eax
push ecx
lea ebx,string
push ebx
mov dx,0
push dx
call statA
output prtResult
itoa result,dx
output result
INVOKE ExitProcess, 0 ; exit with return code 0
PUBLIC _start ; make entry point public
END ; end of source code
實驗二
題目
從鍵盤輸入一個數據,顯示其二進制表示方式在在屏幕上。(提示用位運算指令完成)
例如:從鍵盤輸入數據爲12,它的二進制爲0000 0000 0000 0000 0000 0000 0000 1100,
那麼在屏幕上顯示 00000000000000000000000000001100B
源碼
;Program:輸出一個十進制數的二進制
;Author:Nonoas
;Date:20191210
.386
.MODEL FLAT
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
INCLUDE io.h ; header file for input/output
cr EQU 0dh ; carriage return character
Lf EQU 0ah ; line feed
.STACK 4096 ; reserve 4096-byte stack
.DATA ; reserve storage for data
prtIN byte "Enter a number:",0
prtResult byte "Its binary value is:",cr,lf,0
prtUnits byte " B",0
strNum byte 30 dup(?)
intNum word ?
result word ?
.CODE ; start of main program code
_start:
output prtIN
input strNum,20
atoi strNum
mov intNum,ax
lea ebx,strNum
mov ecx,16
output prtResult
forOut:
mov dx,intNum
rol dx,1 ;Cycle left
mov intNum,dx
and dx,1 ;and the number
itoa result,dx
output result
loop forOut
output prtUnits
INVOKE ExitProcess, 0 ; exit with return code 0
PUBLIC _start ; make entry point public
END ; end of source code