如何使用汇编语言输入浮点数...以及如何添加减法和乘以这些浮点数

时间:2015-06-06 16:24:34

标签: assembly floating-point x86 nasm

我试图在NASM中使用汇编语言计算浮点数,但是我不知道如何输入浮点数。我用整数做了这个。

整数代码如下:

.MODEL small
.stack 100h
.data 
fctr db "facorial of this number is :$"
sport db "this is not supported $"
er db "invalid entery $" 
syn db "Syntax error $"
ovr db "answer out of range $" 
owr db "this digit eliminated bcz input will go out of range $"
math db "MATH ERROR $"
sq db "this number hav'nt comlete square root $"
cu db "this number hav'nt comlete cube root $"
lg db "log($" 
arr dw 3 dup(?)
count1 dw 0
count2 dw 0
a dw 0
b dw 0
c dw 0
d dw 0
.code 
main proc
mov ax,@data
mov ds,ax
 start: 
mov dl,10
mov ah,2
int 21h
mov count1,0 
mov a,0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
lab:
mov ah,1
int 21h
cmp al,'+'
je pls
cmp al,'-'
je sbt  
cmp al,'*'
je mult
cmp al,'/'
je dvn
cmp al,'%'
je mod   
cmp al,'!'
je fct
cmp al,'^'
je exp
cmp al,'s'
je sqr
cmp al,'c'
je cub   
cmp al,'l'
je log
cmp al,0dh
je skip
cmp al,'0'
jb eror
cmp al,'9'
ja eror
sub al,48 
cmp a,6553
ja lbl
cmp a,6553
jb  further
cmp al,5    
jna further
lbl:
mov dl,10
mov ah,2
int 21h
lea dx,owr
mov ah,9
int 21h    
jmp lab
;;;;;;;;;;;;;;;;;;;;;;
further:
mov ah,0
mov bx,a
mov cl,3
shl a,cl
shl bx,1
add a,bx
add a,ax
add count1,1 
jmp lab

eror:
lea dx,er
mov ah,9
int 21h
jmp lab
skip:
mov dl,10
mov ah,2
int 21h 
cmp count1,0
je start 
mov ax,0
add ax,a
cmp a,65535
jnbe ag
call show 
jmp start
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pls:
mov ax,a
call plus
mov dl,10
mov ah,2
int 21h
jmp hhh                  ;lable 
sbt:  
mov ax,a
call subtractoin
mov dl,10
mov ah,2
int 21h
jmp hhh
mult:
mov ax,a
call multiplication
mov dl,10
mov ah,2
int 21h
jmp hhh
dvn:
mov ax,a
call division
mov dl,10
mov ah,2
int 21h
jmp hhh
mod:
mov ax,a
call modulas
mov dl,10
mov ah,2
int 21h
jmp hhh
fct:  
mov ax,a
call factorial
mov dl,10
mov ah,2
int 21h
jmp hhh
exp:
mov ax,a
call exponential
mov dl,10
mov ah,2
int 21h
jmp hhh
sqr:
mov ax,a
call sqrt
mov dl,10
mov ah,2
int 21h
jmp hhh
cub:
mov ax,a
call cuber
mov dl,10
mov ah,2
int 21h
jmp hhh
log:
mov ax,a
call logarithm
mov dl,10
mov ah,2
int 21h
jmp hhh                  ;lable 

ag:
lea dx,ovr
mov ah,9
int 21h
hhh:
mov dl,10
mov ah,2
int 21h
jmp start

mov ah,4ch
int 21h
main endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
plus proc                           ;;;;; addition procedure

mov c,ax
mov count2,0
     mov b,0
labp:
mov ah,1
int 21h
cmp al,0dh
je skipp
cmp al,'0'
jb erorp
cmp al,'9'
ja erorp
cmp b,6553
ja lblp
cmp b,6553
jne  furtherp
cmp al,5    
jna furtherp
lblp:    
mov dl,10
mov ah,2
int 21h
lea dx,owr
mov ah,9
int 21h    
jmp labp
;;;;;;;;;;;;;;;;;;;;;;
furtherp:
sub al,48
mov ah,0
mov bx,b
mov cl,3
shl b,cl
shl bx,1
add b,bx
add b,ax
add count2,1 
jmp labp

erorp:
lea dx,er
mov ah,9
int 21h
jmp labp
skipp:
mov dl,10
mov ah,2
int 21h
cmp count2,0 
jne xxx
lea dx,syn
mov ah,9
int 21h
jmp rt
xxx:
mov ax,c
add ax,b
jc  no 
 call show
 jmp rt 
 no:  
lea dx,ovr
mov ah,9
int 21h                                
 rt:
ret

;;;;;;;;;;;;;;;;;;;
subtractoin proc                           ;;;;; subtraction procedure

mov c,ax
mov count2,0
     mov b,0
labps:
mov ah,1
int 21h
cmp al,0dh
je skipps
cmp al,'0'
jb erorps
cmp al,'9'
ja erorps
cmp b,6553
ja lblps
cmp b,6553
jne  furtherps
cmp al,5    
jna furtherps
lblps:    
mov dl,10
mov ah,2
int 21h
lea dx,owr
mov ah,9
int 21h    
jmp labps
;;;;;;;;;;;;;;;;;;;;;;
furtherps:
sub al,48
mov ah,0
mov bx,b
mov cl,3
shl b,cl
shl bx,1
add b,bx
add b,ax
add count2,1 
jmp labps

erorps:
lea dx,er
mov ah,9
int 21h
jmp labps
skipps:
mov dl,10
mov ah,2
int 21h
cmp count1,0
je sup
cmp count2,0 
jne xxxs
lea dx,syn
mov ah,9
int 21h
jmp rt
xxxs:
mov ax,c
sub ax,b
jc  sup
;;;;;;;;;;;;;;;;;;;
 call show
 jmp rts
 sup:
 lea dx,sport
 mov ah,9
 int 21h                                
 rts:
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   
multiplication proc                  ; multiplication procedure

mov c,ax
mov count2,0
     mov b,0
labpm:
mov ah,1
int 21h
cmp al,0dh
je skippm
cmp al,'0'
jb erorpm
cmp al,'9'
ja erorpm
cmp b,6553
ja lblpm
cmp b,6553
jne  furtherpm
cmp al,5    
jna furtherpm
lblpm:    
mov dl,10
mov ah,2
int 21h
lea dx,owr
mov ah,9
int 21h    
jmp labpm
;;;;;;;;;;;;;;;;;;;;;;
furtherpm:
sub al,48
mov ah,0
mov bx,b
mov cl,3
shl b,cl
shl bx,1
add b,bx
add b,ax
add count2,1 
jmp labpm

erorpm:
lea dx,er
mov ah,9
int 21h
jmp labpm
skippm:
mov dl,10
mov ah,2
int 21h
cmp count1,0
jne xxxm
cmp count2,0 
jne xxxm
lea dx,syn
mov ah,9
int 21h
jmp rtm
xxxm:
mov ax,c
mul b
jc  nom 
 call show
 jmp rtm 
 nom:  
lea dx,ovr
mov ah,9
int 21h                                
 rtm:
ret

;;;;;;;;;;;;;;;;;;;
division proc                           ;;;;; division procedure

mov c,ax
mov count2,0
     mov b,0
labpd:
mov ah,1
int 21h
cmp al,0dh
je skippd
cmp al,'0'
jb erorpd
cmp al,'9'
ja erorpd
cmp b,6553
ja lblpd
cmp b,6553
jne  furtherpd
cmp al,5    
jna furtherpd
lblpd:    
mov dl,10
mov ah,2
int 21h
lea dx,owr
mov ah,9
int 21h    
jmp labpd
;;;;;;;;;;;;;;;;;;;;;;
furtherpd:
sub al,48
mov ah,0
mov bx,b
mov cl,3
shl b,cl
shl bx,1
add b,bx
add b,ax
add count2,1 
jmp labpd

erorpd:
lea dx,er
mov ah,9
int 21h
jmp labpd
skippd:
mov dl,10
mov ah,2
int 21h
cmp count1,0
je synt
cmp count2,0 
jne xxxd
synt:
lea dx,syn
mov ah,9
int 21h
jmp rt
xxxd:
mov ax,c
mov bx,b
mov dx,0
div bx
mov d,ax
mov cx,10         ;;looping on remainder
mov ax,dx
mov dx,0
mul cx
div bx
mov a, ax
mov ax,dx
mov dx,0
mul cx
div bx
mov b, ax
mov ax,dx
mov dx,0
mul cx
div bx
mov c, ax
push c
push b
push a 
mov dx,'.'
push dx
mov ax,d
jc  nod 
 call show
 pop dx 
 mov ah,2
 int 21h
 mov cx,3
ree:
pop dx  
add dx,48
 mov ah,2
 int 21h
loop ree
 jmp rt 
 nod:  
lea dx,ovr
mov ah,9
int 21h                                
 rtd:
ret

;;;;;;;;;;;;;;;;;;;
modulas proc                           ;;;;; modulas procedure

mov c,ax
mov count2,0
     mov b,0
labpdr:
mov ah,1
int 21h
cmp al,0dh
je skippdr
cmp al,'0'
jb erorpdr
cmp al,'9'
ja erorpdr
cmp b,6553
ja lblpdr
cmp b,6553
jne  furtherpdr
cmp al,5    
jna furtherpdr
lblpdr:    
mov dl,10
mov ah,2
int 21h
lea dx,owr
mov ah,9
int 21h    
jmp labpdr
;;;;;;;;;;;;;;;;;;;;;;
furtherpdr:
sub al,48
mov ah,0
mov bx,b
mov cl,3
shl b,cl
shl bx,1
add b,bx
add b,ax
add count2,1 
jmp labpdr

erorpdr:
lea dx,er
mov ah,9
int 21h
jmp labpdr
skippdr:
mov dl,10
mov ah,2
int 21h
cmp count1,0
je syntr
cmp count2,0 
jne xxxdr
syntr:
lea dx,syn
mov ah,9
int 21h
jmp rt
xxxdr:
mov ax,c
mov bx,b
mov dx,0
div bx
mov ax,dx
 call show

ret

;;;;;;;;;;;;;;;;;;;
factorial proc                          ;;factorial proc
    cmp ax,9
    jae oud
     mov cx,ax
     mov ax,1
     lbf:
     mul cx
     dec cx
     cmp cx,1
     ja lbf
     mov c,ax
     lea dx,fctr
     mov ah,9
     int 21h 
     mov ax,c
     call show
     jmp rtf
    oud:
    lea dx,ovr
    mov ah,9
    int 21h
    rtf:
ret                                          ;end fact proc
;;;;;;;;;;;;;;;;;;
exponential proc                  ; expo procedure

mov c,ax
mov count2,0
     mov b,0
labpme:
mov ah,1
int 21h
cmp al,0dh
je skippme
cmp al,'-'
je minp
cmp al,'0'
jb erorpme
cmp al,'9'
ja erorpme
cmp b,6553
ja lblpme
cmp b,6553
jne  furtherpme
cmp al,5    
jna furtherpme
lblpme:    
mov dl,10
mov ah,2
int 21h
lea dx,owr
mov ah,9
int 21h    
jmp labpme
;;;;;;;;;;;;;;;;;;;;;;
furtherpme:
sub al,48
mov ah,0
mov bx,b
mov cl,3
shl b,cl
shl bx,1
add b,bx
add b,ax
add count2,1 
jmp labpme

erorpme:
lea dx,er
mov ah,9
int 21h
jmp labpme
skippme:
mov dl,10
mov ah,2
int 21h
cmp count1,0
je  sntx
cmp count2,0 
jne xxxme
minp:
cmp count2,0
jne sntx
call inverse
jmp rtme
sntx:
lea dx,syn
mov ah,9
int 21h
jmp rtme
xxxme:
mov ax,c
mov cx,b
cmp cx,1
je for
lble:
mul c
jc  nome
dec cx
cmp cx,1
ja lble
for: 
 call show
 jmp rtme 
 nome:  
lea dx,ovr
mov ah,9
int 21h                                
 rtme:
ret

;;;;;;;;;;;;;;;;;;      
inverse proc                              ; inverse proc

mov count2,0
     mov b,0
labpmei:
mov ah,1
int 21h
cmp al,0dh
je skippmei
cmp al,'0'
jb erorpmei
cmp al,'9'
ja erorpmei
cmp b,6553
ja lblpmei 
cmp b,6553
jne  furtherpmei
cmp al,5    
jna furtherpmei
lblpmei:    
mov dl,10
mov ah,2
int 21h
lea dx,owr
mov ah,9
int 21h    
jmp labpmei
;;;;;;;;;;;;;;;;;;;;;;
furtherpmei:
sub al,48
mov ah,0
mov bx,b
mov cl,3
shl b,cl
shl bx,1
add b,bx
add b,ax
add count2,1 
jmp labpmei

erorpmei:
lea dx,er
mov ah,9
int 21h
jmp labpmei
skippmei:
mov dl,10
mov ah,2
int 21h
cmp count1,0
je  sntxi
cmp count2,0 
jne xxxmei
sntxi:
lea dx,syn
mov ah,9
int 21h
jmp rtmei
xxxmei:
mov ax,c
mov cx,b
cmp cx,1
je fori
dec cx
lblei:
mul c
jc  nomei
loop lblei
fori: 
 mov bx,ax
 mov ax,1
 mov dx,0
 div bx 
 mov d,ax
mov cx,10         ;;looping on remainder
mov ax,dx
mov dx,0
mul cx
div bx
mov a, ax
mov ax,dx
mov dx,0
mul cx
div bx
mov b, ax
mov ax,dx
mov dx,0
mul cx
div bx
mov c, ax
push c
push b
push a 
mov dx,'.'
push dx
mov ax,d 
 call show
 pop dx 
 mov ah,2
 int 21h
 mov cx,3
reei:
pop dx  
add dx,48
 mov ah,2
 int 21h
loop reei
 jmp rtmei 
 nomei:  
lea dx,ovr
mov ah,9
int 21h                                
 rtmei:
ret
;;;;;;;;;;;;;;;;;;;;;;;;
sqrt proc                           ;;;;; sqrt procedure

mov c,ax
mov count2,0
     mov b,0
labpq:
mov ah,1
int 21h
cmp al,0dh
je skippq
cmp al,'0'
jb erorpq
cmp al,'9'
ja erorpq
cmp b,6553
ja lblpq
cmp b,6553
jne  furtherpq
cmp al,5    
jna furtherpq
lblpq:    
mov dl,10
mov ah,2
int 21h
lea dx,owr
mov ah,9
int 21h    
jmp labpq
;;;;;;;;;;;;;;;;;;;;;;
furtherpq:
sub al,48
mov ah,0
mov bx,b
mov cl,3
shl b,cl
shl bx,1
add b,bx
add b,ax
add count2,1 
jmp labpq

erorpq:
lea dx,er
mov ah,9
int 21h
jmp labpq
skippq:
mov dl,10
mov ah,2
int 21h
cmp count1,0
jne sntq
cmp count2,0 
jne xxxq
sntq:
lea dx,syn
mov ah,9
int 21h
jmp rtq
xxxq:   
mov bx,0
mov ax,0
mov cx,b
sqt:
mul bx
cmp ax,b
je found
inc bx
mov ax,bx
loop sqt
jmp noq
found:
mov ax,bx  
 call show
 jmp rts 
 noq:  
lea dx,sq
mov ah,9
int 21h                                
 rtq:
ret
;;;;;;;;;;;;;;;;;;;;;;;;;
cuber proc                           ;;;;; cube root procedure

mov c,ax
mov count2,0
     mov b,0
labpqc:
mov ah,1
int 21h
cmp al,0dh
je skippqc
cmp al,'0'
jb erorpqc
cmp al,'9'
ja erorpqc
cmp b,6553
ja lblpqc
cmp b,6553
jne  furtherpqc
cmp al,5    
jna furtherpqc
lblpqc:    
mov dl,10
mov ah,2
int 21h
lea dx,owr
mov ah,9
int 21h    
jmp labpqc
;;;;;;;;;;;;;;;;;;;;;;
furtherpqc:
sub al,48
mov ah,0
mov bx,b
mov cl,3
shl b,cl
shl bx,1
add b,bx
add b,ax
add count2,1 
jmp labpqc

erorpqc:
lea dx,er
mov ah,9
int 21h
jmp labpqc
skippqc:
mov dl,10
mov ah,2
int 21h
cmp count1,0
jne sntqc
cmp count2,0 
jne xxxqc
sntqc:
lea dx,syn
mov ah,9
int 21h
jmp rtqc
xxxqc:   
mov bx,0
mov ax,0
mov cx,b
crt:
mul bx
mul bx
cmp ax,b
je foundc
inc bx
mov ax,bx
loop crt
jmp noqc
foundc:
mov ax,bx  
 call show
 jmp rtqc 
 noqc:  
lea dx,sq
mov ah,9
int 21h                                
 rtqc:
ret

;;;;;;;;;;;;;;;;;;;;;;;;
logarithm proc                      ;logarithm procedure

cmp count1,0
ja  sntxe
lea dx,lg
mov ah,9
int 21h
mov count2,0
     mov b,0
labpl:
mov ah,1
int 21h
cmp al,0dh
je skippl
cmp al,'0'
jb erorpl
cmp al,'9'
ja erorpl
cmp b,6553
ja lblpl
cmp b,6553
jne  furtherpl
cmp al,5    
jna furtherpl
lblpl:    
mov dl,10
mov ah,2
int 21h
lea dx,owr
mov ah,9
int 21h    
jmp labpl
;;;;;;;;;;;;;;;;;;;;;;
furtherpl:
sub al,48
mov ah,0
mov bx,b
mov cl,3
shl b,cl
shl bx,1
add b,bx
add b,ax
add count2,1 
jmp labpl

erorpl:
lea dx,er
mov ah,9
int 21h
jmp labpl
skippl:
mov dl,10
mov ah,2
int 21h
cmp count2,0 
jne xxxl
sntxe:
lea dx,syn
mov ah,9
int 21h
jmp rtl
xxxl:
cmp b,0
je nol
mov ax,count2
mov a,ax
dec a
mov ax,b
;;;;;;; 
mov cx,10
mov count1,0
divsnl:
mov dx,0
div cx
push dx
add count1,1
cmp ax,0
je okk
jmp divsnl
okk: 
cmp count1,1
je bb
cmp count1,2
je cc
pop dx
dec count1
mov b,dx
pop dx
dec count1
mov c,dx
cmp count1,0
je f
mov cx,count1
l:
pop dx
loop l
jmp f
bb:
pop dx
mov b,dx
mov c,0
jmp f
cc:
pop dx
mov b,dx
pop dx
mov c,dx
jmp f
f:
cmp c,0
je zz
cmp c,1
je yy
cmp c,2
je xx
cmp c,3
je ww
cmp c,4
je vv
cmp c,5
je uu
cmp c,6
je tt
cmp c,7
je  sss
cmp c,8
je  rr
cmp c,9
je  qq
zz:
mov dx,0
push dx   
jmp forwrd
yy:
mov dx,0
push dx   
jmp forwrd
xx:
mov dx,3
push dx   
jmp forwrd
ww:
mov dx,4
push dx   
jmp forwrd
vv:
mov dx,6
push dx   
jmp forwrd
uu:
mov dx,6
push dx   
jmp forwrd
tt:
mov dx,7
push dx   
jmp forwrd
sss:
mov dx,8
push dx
jmp forwrd
rr:
mov dx,9
push dx
jmp forwrd 
qq:
mov dx,9
push dx
forwrd:                                   ; for b 
cmp b,0
je zzz
cmp b,1
je yyy
cmp b,2
je xxxx
cmp b,3
je www
cmp b,4
je vvv
cmp b,5
je uuu
cmp b,6
je ttt
cmp b,7
je  ssss
cmp b,8
je  rrr
cmp b,9
je  qqq 
zzz:
mov dx,0
push dx 
jmp move
yyy:
mov dx,0
push dx 
jmp move
xxxx:
mov dx,3
push dx 
jmp move
www:
mov dx,4
push dx 
jmp move
vvv:
mov dx,6
push dx 
jmp move
uuu:
mov dx,6
push dx 
jmp move
ttt:
mov dx,7
push dx
jmp move
ssss:
mov dx,8
push dx
jmp move
rrr:
mov dx,9
push dx
jmp move 
qqq:
mov dx,9
push dx
move:
mov dx,a
add dx,48
mov ah,2
int 21h
mov dx,'.'
mov ah,2
int 21h
pop dx
add dx,48
mov ah,2
int 21h
pop dx
add dx,48
mov ah,2
int 21h
 jmp rtl 
 nol:  
lea dx,math
mov ah,9
int 21h                                
 rtl:
ret

;;;;;;;;;;;;;;;;;;;;;;;;
show proc                                     ;;; show multidigit
mov cx,10
mov count1,0

;;;;;;;;;;;;;;;;;;;;;;
divsn:
mov dx,0
div cx
push dx
add count1,1
cmp ax,0
je ok
jmp divsn

ok:
mov cx,count1 
lp:
pop dx
add dx,48
mov ah,2
int 21h
loop lp
    ret
end main

1 个答案:

答案 0 :(得分:2)

我建议你先给Floating Point Assembly一个好的阅读。如果你打算做任何严肃的事情,毫无疑问你会想要使用浮点指令而不是标准的x86指令集。

阅读完文档后,您最有可能的问题将围绕如何表示浮点值以及如何将其显示给用户。我建议你重做这个问题(如果它没有被投票)或者打开一个更具体的新问题。 :)