⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 procedure1.asm

📁 用汇编语言实现四则运算,其中数值的范围是用32位长字节表示的范围
💻 ASM
📖 第 1 页 / 共 3 页
字号:
.386
.MODEL FLAT
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Include 文件定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
INCLUDE io.h 
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Equ 等值定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
cr      EQU     0dh     ; carriage return character
Lf      EQU     0ah     ; line feed
.STACK  4096             ; reserve 4096-byte stack
PUBLIC ReadExpress ,Adder,Subtraction,Muliplic,Divis,ASCTOBCD,BCDTOASC,ASCTONOBCD,NOBCDTOASC,SingleMulipic,addUnp,subUnp,ifNegative,DivMorediv,mulUnp1,EqualNumbers,NoEqualNumbers,OutResult
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 数据段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.DATA                    ; reserve storage for data
Label1      byte  "Input Expression Error ,you should input Expression like this '4+19='.",cr,Lf ,"please input again:" ,0
Label3      byte  "Input digit don't satisfied requirements, you must input digit more than zero . ",cr,"please input again:" ,0
string		BYTE    40 DUP (?)
number1		BYTE    40 DUP (?)
number2		BYTE    40 DUP (?)
Median1		BYTE    21 DUP (?)
Median2		BYTE    21 DUP (?)
count		DWORD     ?                   ;number of Divisor
count1		DWORD    ?                    ;number of Dividend
count2		DWORD    ?                    ;number of Quotient
count3		DWORD    ?                    ;number of lastRemainder 
count4		DWORD    ?
bcd1		BYTE    9 DUP(?)
bcd2		BYTE    9 DUP(?) 
nobcd1		BYTE    21 DUP(?)
nobcd2		BYTE    10 DUP(?)
Quotient1	BYTE   ?
label4		BYTE    cr ,Lf ,"The Quotient is  " , 0
Quotient	BYTE   20 DUP(?)              ;
		BYTE   cr , Lf ,0
		BYTE   cr , Lf , 0
label5		BYTE    cr ,Lf ,"The Remainder is  " , 0
lastRemainder   BYTE  10 DUP(?)
		BYTE   cr , Lf ,0
		BYTE   cr , Lf , 0
label2		BYTE    cr, Lf, "The result is "
result		BYTE    40 DUP (?)
		BYTE    cr, Lf, 0
		BYTE    cr, Lf, 0
BoxMessage  BYTE cr , Lf ,"Illegal operation,Divisor must be not zero.If you want to continue, you hava to start system again. ", 0
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 代码段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
				.CODE
;*********************************************************************************
; 把字符串String‘+’、‘-’、‘*’、‘/’为分界符号分成两个子字符串number1和number2
;**********************************************************************************
ReadExpress PROC   NEAR32
				push ebp
				lea edi , number1
				cmp byte ptr [esi] , '-'   ;   digit>0
				je  MoreZero
				mov ecx,0
				mov ecx,10
				mov dl,30h
whileDigit1:                           ; Judge string whether digit, but except + -  * / ,and head of + -  * / lay off string to number1
				cmp byte ptr [esi] , dl
				je   IFDigit1
				inc dl
				loop whileDigit1
				jmp  Plus
IFDigit1:	   
				movs  byte ptr [edi] , [esi]  ; 
				mov ecx,10
				mov dl,30h
				cmp byte ptr [esi] , 0h
				jne  whileDigit1
  				jmp  error
Plus:                                 ; if string of char is not digit ,to continu judge whether + - * /
				cmp byte ptr [esi] , '+'
				jne Dashes 
				inc esi
				mov dh ,'+'
				jmp  SeconNumber
Dashes:    
				cmp byte ptr [esi] , '-'
				jne Multiplication
				inc esi
				mov  dh,'-'
				jmp  SeconNumber
Multiplication: 
				cmp byte ptr [esi],'*'
				jne  Inaddtion
				inc esi
				mov  dh, '*'
				jmp  SeconNumber
Inaddtion:  
				cmp byte ptr [esi],'/'
				jne  error
				inc esi
				mov dh ,'/'
				jmp  SeconNumber
MoreZero:  
				lea edi , number1
				output Label3
				jmp  InputAgani
error:	                              ;if string of char is not digit or + - * / ,input string error
				lea edi , number1
				output Label1
InputAgani:                            ; if input string error , input again ,and continue to judge
				cmp byte ptr [edi] , 0h
				jne CleraZero
				input   string, 40 
				lea edi , number1
				lea esi , string
				cmp byte ptr [esi] , '-'
				je  MoreZero
				mov ecx,0
				mov ecx,10
				mov dl,30h  
				jmp whileDigit1
CleraZero:                               
				mov byte ptr [edi] , 0h
				inc edi
				jmp InputAgani
SeconNumber:                          ; lay off behind string of + - * / into number2 
				lea edi , number2
				mov ecx,0
				mov ecx,10
				mov dl,30h
whileDigit2: 
				cmp byte ptr [esi] , dl
				je   Digit2
				inc dl
				loop whileDigit2
				jmp  meanwhile
Digit2:	  
				movs  byte ptr [edi] , [esi]
				mov ecx,10
				mov dl,30h
				jmp  whileDigit2
meanwhile:                              ;if number2 of char have char that is not digit ,output error and input again
				cmp byte ptr [esi],'='
				jne  error
				inc esi
				cmp byte ptr [esi],0h
				jne error
exit:	                         
				pop ebp
				ret                     ; return
ReadExpress  ENDP
;********************************************************************
; 两个压缩BCD码bcd1和bcd2相加,并把结果输出
;********************************************************************
Adder PROC   NEAR32
; add two non-negative 10 byte packed BCD numbers
; parameter1:  address of operand1 (and destination)
; parameter2:  address of operand2
; author:  R. Detmer     revised: 5/98
				push ebp
				lea edi ,bcd1
				lea esi , number1
				call  ASCTOBCD
				lea edi ,bcd2
				lea esi , number2
				call  ASCTOBCD
				lea  edi , bcd1
				lea  esi , bcd2
				clc                 ; clear carry flag for first add
				mov  ecx, 9         ; count of bytes to process
forAdd:  
				mov  al, [edi]      ; get one operand byte
				adc  al, [esi]      ; add other operand byte
				daa                 ; adjust to BCD
				mov  [edi], al      ; save sum
				inc  edi            ; point at next operand bytes
				inc  esi
				loop forAdd         ; repeat for all 9 bytes
				lea  esi , bcd1
				call BCDTOASC
				call OutResult
				output label2
				pop ebp
            ret                                      ; return to caller
Adder ENDP
;********************************************************************
; 两个压缩BCD码bcd1和bcd2相减,并把结果输出
;********************************************************************
Subtraction PROC  NEAR32
; subtract 2 non-negative 10 byte packed BCD numbers
; parameter1:  address of operand1 (and destination)
; parameter2:  address of operand2
; operand1 - operand2  stored at destination
; author:  R. Detmer     revised: 5/98
				push ebp
				lea edi , bcd1
				lea  esi , number1
				call  ASCTOBCD
				lea edi ,bcd2
				lea esi , number2
				call  ASCTOBCD
				lea  edi , bcd1
				lea  esi , bcd2
            			clc                 ; clear carry flag
				mov  ecx, 9         ; count of bytes to process
forSub:   
				mov  al, [edi]      ; get one operand byte
				sbb  al, [esi]      ; subtract other operand byte
				das                 ; adjust to BCD
				mov  [edi], al      ; save difference
				inc  edi            ; point at next operand bytes
				inc  esi
				loop forSub         ; repeat for all 9 bytes
				jnc  endIfBigger    ; done if destination >= source
				sub  edi, 9         ; point at beginning of destination
				mov  ecx, 9         ; count of bytes to process
forSub1: 
				mov  al, 0          ; subtract destination from zero
				sbb  al, [edi]
				das
				mov  [edi], al
				inc  edi            ; next byte
				loop forSub1
				mov  BYTE PTR [edi], 80h   ; negative result
endIfBigger:
				lea  esi , bcd1
				call BCDTOASC
				call OutResult
				output label2
			  	pop ebp
				ret                ; return to caller
Subtraction ENDP
;********************************************************************
; 两个非压缩BCD码nobcd1和nobcd2相乘,并把结果输出
;********************************************************************
Muliplic PROC NEAR32
				push ebp
				lea esi , number1
				lea edi , nobcd1
			   call  ASCTONOBCD             ;number1 into no BCD
			   mov edx , DWORD PTR count
			   lea esi , number2
			   lea edi , nobcd2
			   call ASCTONOBCD              ;number2 into no BCD
			   mov ecx , count
			   mov ebx , 0
			   lea esi , nobcd2
			   add esi , count
			   dec esi 
			   mov count , 0h
loopFor4:		  
	  		   mov bl , [esi]
			   call SingleMulipic          ; nobcd1 and nobcd2 muliplic
			   inc count
			   dec esi
			   loop loopFor4 
			   lea esi , Median2
				lea edi , result
			   mov ecx , 21
			   call  NOBCDTOASC            ;result into ASCII
			   call OutMulResult           ;output result
			   output label2
			   pop ebp
			   ret   
Muliplic ENDP
;********************************************************************
; 两个压缩BCD码nobcd1和nobcd2相除,并把结果输出
;********************************************************************
Divis PROC NEAR32
				push ebp
			   pushad
			   lea esi , number2
			   cmp BYTE PTR[esi] , 30h          ; Divisor must be not equal zero
			   jne DivisorNotZero
				output BoxMessage
			   jmp exit2
DivisorNotZero:                              
			   lea esi , number1
			   lea edi , nobcd1
			   call  ASCTONOBCD                 ;Dividend  into no BCD
			   mov edx , 0
			   mov edx , DWORD PTR count
			   lea esi , number2
			   lea edi , nobcd2
			   call ASCTONOBCD                  ;Divisor    into no BCD
			   mov ecx , edx
			   mov count1 , edx
			   sub edx , count                  ;Judge number of Dividend and number of Divisor
			   cmp edx , 0
			   jge toJudge1
			   call  DivMorediv                 ;number of Dividend < number of Divisor
			   mov  Quotient , 0h
			   jmp exit2
toJudge1:     
			   cmp edx , 0
				jg  toJudge2                     ;number of Dividend >number of Divisor
			   lea edi , nobcd1                 ;number of Dividend = number of Divisor
			   add edi , count1
			   lea esi , nobcd2
			   add esi , count
			   mov ecx , count1
			   call  ifNegative                ;Judge Dividend and  Divisor
			   cmp al , 0h
			   je  toJudge3                    ;Dividend >=  Divisor
			   mov  Quotient1 , 0h             ;Dividend <  Divisor
			   call DivMorediv
			   jmp exit2
toJudge3:                             
			   mov ecx , count
			   lea edi , Median1
			   add edi , 21
			   lea esi , nobcd2
			   add esi , count
loopFor6:                                   ;If (number of Dividend = number of Divisor) and (Dividend >=  Divisor) do Quotient and Remainder
  			   dec edi
			   dec esi
			   mov al , [esi]
			   mov [edi] , al
			   loop loopFor6
			   lea esi , Median1
			   add esi , 21
			   lea edi , Median2
			   add edi , 21
			   mov bl , 1h
			   call mulUnp1                   
			   lea edi , nobcd1
			   add edi , count1
			   lea esi , Median2
			   add esi , 21
			   mov ecx , count1
			   call  ifNegative
			   cmp al , 0
			   je toJudge4
			   mov  Quotient1 , 0h
			   call DivMorediv
			   jmp exit2
toJudge4:
			   lea esi , Median1
			   add esi , 21
			   lea edi , Median2
			   add edi , 21
			   inc bl
			   call mulUnp1
			   lea edi , nobcd1
			   add edi , count1
			   lea esi , Median2
			   add esi , 21
			   cmp bl ,9
			   je toJudge13
			   mov ecx ,count1
			   add ecx , count4
			   call  ifNegative
			   cmp al , 0 
			   je toJudge4
			   dec bl
toJudge13:
			   mov Quotient, bl
			   lea esi , Median1
			   add esi , 21
			   lea edi , Median2
			   add edi , 21
			   call mulUnp1
			   lea edi , nobcd1
			   add edi , count1
			   lea esi , Median2
			   add esi , 21
			   mov ecx , count1
			   call subUnp 
			   mov  Quotient1 , bl
			   lea  esi , Quotient1 
			   lea  edi , Quotient
			   mov ecx , 1
			   call NOBCDTOASC
			   output label4
			   output Quotient
			   lea esi , nobcd1
			   lea edi , lastRemainder
			   mov ecx , count
			   call  NOBCDTOASC
			   output label5
			   output lastRemainder
			   jmp exit2
toJudge2:                              ;if( number of Dividend > number of Divisor) do   Quotient and lastRemainder 
			   lea esi , nobcd1
			   lea edi , Median1
			   mov ecx , count
loopFor7:                              ;take head of Dividend that number of Divisor 
			   mov al , [esi]
			   mov [edi] , al
			   inc esi 
		           inc edi
			   loop loopFor7
			   lea esi , nobcd2
			   add esi , count
			   mov ecx , count
			   call  ifNegative           ;Judge whether more Divisor
			   cmp al , 0
			   je  toJudge6               ;if more or equal Divisor 
			   lea esi , nobcd1           ;if less Divisor,take head of Dividen more than number of Divisor
			   add esi , count
			   mov al , [esi]
			   mov [edi] , al
			   inc esi
			   call NoEqualNumbers        ;number of Dividend  not equal number of Diviso ,do Quotient and Remainder  
			   mov bl , Quotient1
			   mov Quotient , bl 
			   mov count2 ,0
			   inc count2
			   mov  ecx , count1
			   sub  ecx , count
		       	   dec ecx
			   cmp ecx , 0

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -