汇编编程 2位十进制数的四则运算
发布网友
发布时间:2022-04-20 09:51
我来回答
共3个回答
热心网友
时间:2023-07-11 03:01
; 本程序在MASMPlus 1.2集成环境下通过编译,经过调试,运行正确。
Code Segment
Assume CS:Code,DS:Code
; -------------------------------------
; 功能:显示指定地址(Str_Addr)的字符串
; 入口:
; Str_Addr=字符串地址(要求在数据段)
; 用法: Output Str_Addr
; 用法举例:Output PromptStr
Output MACRO Str_Addr
lea dx,Str_Addr
mov ah,9
int 21h
EndM
; -------------------------------------
ASCII_Binary MACRO ;将两位数字的ASCII码转换成二进制数
mov dl,10
lodsb
and al,0fh
mul dl
mov dx,ax
lodsb
and al,0fh
add al,dl
stosb
EndM
; -------------------------------------
; 功能:输入两位十进制数,将其保存在指定的内存单元
; 入口:es:di=保存输入的数字首地址
; 出口:转换后的二进制数保存在di所指的单元
Input_Dec Proc Near
push cx
push bx
cld
mov cx,2
@@Input_Dec: mov ah,7 ;从键盘接受一个字符
int 21h
cmp al,'0'
jb @@Input_Dec ;若低于数字'0',重新输入
cmp al,'9'
ja @@Input_Dec ;若高于数字'9',重新输入
stosb ;保存
mov ah,0eh ;显示这个数字
xor bh,bh
int 10h
loop @@Input_Dec ;接受下一个数字
pop bx
pop cx
ret
Input_Dec EndP
; -------------------------------------
; 功能:把AX中的二进制无符号数转换成显式的十进制ASCII码,并送显示屏显示
; 入口:AX=二进制数
; 出口:在当前光标位置显示转换后的ASCII码数字
Unsi_Dec_ASCII Proc Near
push dx
push bx
push di
mov bx,10
lea di,@@Temp_Save[5]
mov BYTE PTR [di],'$'
dec di
cld
@@Divide: xor dx,dx
div bx
or dl,30h
mov [di],dl
dec di
test ax,ax
jnz @@Divide
inc di
push di
pop dx
mov ah,9
int 21h
pop di
pop bx
pop dx
ret
@@Temp_Save db 6 p(?)
Unsi_Dec_ASCII EndP
; -------------------------------------
; 功能:键盘输入一个字符,若输入的是'y'或'n'(不分大小写),则显示并返回
; 入口:无
; 出口:若输入的是'y',进位标志置位;若输入的是'n',进位标志复位。
Yes_or_No proc Near
push dx
push ax
@@Input_Chr: mov ah,7 ;不带回显的键盘输入
int 21h
mov dl,al
and al,0dfh
cmp al,'Y'
jnz $+5
stc
jmp $+7
cmp al,'N'
jnz @@Input_Chr
clc
mov ah,2
int 21h
pop ax
pop dx
ret
Yes_or_No Endp
; -------------------------------------
; 加法运算
Addition Proc Near
mov ax,WORD ptr Number
add al,ah
xor ah,ah
call Unsi_Dec_ASCII ;显示和
ret
Addition EndP
; -------------------------------------
; 减法运算
Subtration Proc Near
mov ax,WORD ptr Number
sub al,ah
xor ah,ah
call Unsi_Dec_ASCII ;显示差
ret
Subtration EndP
; -------------------------------------
; 乘法运算
Multiple Proc Near
mov ax,WORD ptr Number
mul ah
call Unsi_Dec_ASCII ;显示乘积
ret
Multiple EndP
; -------------------------------------
; 除法运算
Division Proc Near
mov ax,WORD ptr Number
mov bl,ah
xor ah,ah
div bl
push ax
xor ah,ah
call Unsi_Dec_ASCII ;显示商数的整数部分
mov cx,6 ;写字符次数
mov al,'.'
mov ah,0eh
@@Write_Dot: int 10h
loop @@Write_Dot
pop ax
xchg ah,al
or al,30h
mov ah,0eh
int 10h
ret
Division EndP
; -------------------------------------
prompt_Str db 13,10,10,'Please input an arithmetical expression:',13,10,'$'
Contin_Str db 13,10,10,'Continue(Y/N)?$'
Number db ?,? ;操作数存储单元
Operators db '+-*/' ;运算符
Procere dw Addition,Subtration,Multiple,Division
Press_Key db 7,13,10,10,'The complated. Press any key to exit...$'
Start: push cs
pop ds
push cs
pop es
Input_Expre: Output prompt_Str ;提示输入算术表达式
lea di,Arith_Expre ;算术表达式缓冲区地址
call Input_Dec ;输入两位十进制数,将其保存在指定的内存单元
Input_Oper: mov ah,7
int 21h
cmp al,'+'
jz Store_Oper
cmp al,'-'
jz Store_Oper
cmp al,'*'
jz Store_Oper
cmp al,'/'
jnz Input_Oper
Store_Oper: stosb ;保存
mov ah,0eh ;显示这个数字
xor bh,bh
int 10h
call Input_Dec
mov al,'='
mov ah,0eh
xor bh,bh
int 10h
lea si,Arith_Expre
lea di,Number ;操作数存储单元地址
ASCII_Binary ;将两位数字的ASCII码转换成二进制数
inc si
ASCII_Binary
mov al,BYTE ptr Arith_Expre[2] ;取运算符
mov cx,4
lea di,Operators
mov bx,di
repnz scasb
xchg di,bx
sub bx,di
dec bx
shl bx,1
call Procere[bx] ;调用相应的运算子程序
Output Contin_Str
call Yes_or_No
jc Input_Expre
; -------------------------------------
Exit_Proc: Output Press_Key ;提示操作完成,按任意键结束程序
mov ah,1
int 21h
mov ah,4ch ;结束程序
int 21h
Arith_Expre: ;算术表达式缓冲区
Code ENDS
END Start ;编译到此结束
热心网友
时间:2023-07-11 03:02
,,你看看行不行,
data segment
buf db 0
outs db '.......$'
data ends
write macro c
mov dl,c
mov ah,2
int 21h
endm
shuru macro
mov ah,1
int 21h
sub al,30h
mov bh,al
mov cl,4
shl bh,cl
mov ah,1
int 21h
sub al,30h
or bh,al
endm
writeBCD macro x ;假设X=36H,则在屏幕上输出为36
push ax ;注 X中的数必须为某数的BCD码
push cx
push dx
mov al,x
add al,0
daa
mov dL,al
mov dH,al
mov cl,4
shr dh,cl
and dl,0fh
add dx,3030h
xchg dl,dh
mov ah,2
int 21h
mov dl,dh
mov ah,2
int 21h
pop dx
pop cx
pop ax
endm
code segment
assume cs:code,ds:data
start: mov ax,data
mov ds,ax
shuru
mov [buf],bh
mov ah,1
int 21h
cmp al,'+'
jz add0
cmp al,'-'
jz sub0
cmp al,'*'
jz mul0
jmp div0
add0: call add1
jmp done
sub0: call sub1
jmp done
mul0: call mul1
jmp done
div0: call div1
done: mov ah,4ch
int 21h
add1 proc
shuru
add bh,[buf]
mov al,bh
daa
mov bl,al
mov dl,'='
mov ah,2
int 21h
writeBCD bl
ret
add1 endp
sub1 proc
shuru
xchg [buf],bh
sub bh,[buf]
mov al,bh
das
mov bl,al
mov dl,'='
mov ah,2
int 21h
writeBCD bl
ret
sub1 endp
mul1 proc
mov bh,0
mov bl,[buf]
mov al,[buf]
mov cl,4
shr bl,cl
and al,0fh
mov ah,bl
aad
mov [buf],al
shuru
mov al,bh
mov bl,bh
mov bh,0
shr bl,cl
and al,0fh
mov ah,bl
aad
mov bl,[buf]
mul bl
aam
add ax,3030h
mov bx,ax
mov dl,'='
mov ah,2
int 21h
mov dl,bh
mov ah,2
int 21h
mov dl,bl
mov ah,2
int 21h
ret
mul1 endp
div1 proc
mov bh,0
mov bl,[buf]
mov al,[buf]
mov cl,4
shr bl,cl
and al,0fh
mov ah,bl
aad
mov [buf],al
shuru
mov al,bh
mov bl,bh
mov bh,0
shr bl,cl
and al,0fh
mov ah,bl
aad
mov ah,0
mov bl,[buf]
xchg al,bl
div bl
mov bl,ah
add al,0
aam
add ax,3030h
mov bh,ah
mov cl,al
mov dl,'='
mov ah,2
int 21h
write bh
write cl
mov dx,offset outs
mov ah,9
int 21h
add bl,30h
write bl
ret
div1 endp
code ends
end start
热心网友
时间:2023-07-11 03:02
ERJINGZHI