实验目的
掌握串操作指令的用法,注意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