2

I have this calculator code below, it works fine but it won't jump to the "dividing"..i'm still on the process of learning assembly programming..need help on how to figure this out...

.model small
.stack 100h

.data
msg1 db 13,10,13,10, "Enter 1st Number : $"
msg2 db 13,10, "Enter 2nd Number : $"
msgEr db 13,10, "Error $"
msgCh db 13,10, "Press A to ADD , S to SUBTRACT ,D to MULTIPLY, F to DIVIDE, X to EXIT : $ "
msgSum db 13,10,13,10, "Sum is : $"
msgDif db 13,10,13,10, "Difference is : $"
msgDiv db 13,10,13,10, "Quotient is : $"
msgMul db 13,10,13,10, "Product is : $"
tmp     db ?


.code

start:
mov ax, @data
mov ds, ax

lea dx, msg1
mov ah, 09h
int 21h
mov bx, 0

start1:
mov ah, 01h
int 21h
cmp al,0dh      
je next1
mov ah,0        
sub al,30h      
push ax         
mov ax,10d      
mul bx          
pop bx          
add bx,ax       
jmp start1      




next1:
push bx
lea dx,msg2
mov ah,09h
int 21h

mov bx,0


start2:
mov ah,01h
int 21h
cmp al,0dh
je choice
mov ah,0
sub al,30h
push ax
mov ax,10d
mul bx
pop bx
add bx,ax 
jmp start2


choice:
lea dx, msgCh
mov ah, 09h
int 21h

mov ah, 01h
mov answer, al
int 21h

cmp al,'f'
je dividing

cmp al,'a'  
je adding

cmp al,'s'
je subtracting

cmp al,'d'
je multiplying

cmp al,'x'
mov ah, 4ch
int 21h

error:
lea dx,msgEr
mov ah,09h
int 21h 
jmp start


dividing: 
pop ax
div bx
push ax
lea dx,msgDiv
mov ah,09h
int 21h 
pop ax
mov cx,0
mov dx,0
mov bx,10d
jmp break

adding:     
pop ax
add ax,bx   
push ax
lea dx,msgSum   
mov ah,09h
int 21h 
pop ax
mov cx,0
mov dx,0
mov bx,10d
jmp break   

multiplying: 
pop ax
mul bx      
push ax     
lea dx,msgMul   
mov ah,09h
int 21h 
pop ax
mov cx,0
mov dx,0
mov bx,10d
jmp break


subtracting: 
pop ax
sub ax,bx 
push ax
lea dx,msgDif
mov ah,09h
int 21h 
pop ax
mov cx,0
mov dx,0
mov bx,10d

break:
div bx
push dx
mov dx,0
inc cx
or ax,ax 
jne break 

ans:        
pop dx
add dl,30h
mov ah,02h
int 21h
loop ans

jmp start

end start

4
  • Please elaborate. When you reach the cmp al, 'f' statement do you really have a lower-case 'f' in al and then the jump does not happen, or how will it not jump? Apr 1, 2013 at 18:46
  • Also... mov dx, 0 before the div! Apr 1, 2013 at 20:26
  • Oh..i just add 'mov dx,0' as you said and it works..can you explain this part if it's okay? thanks Apr 2, 2013 at 14:21
  • @SachiTekina: div bx divides the 32-bit value in dx:ax by bx and stores the quotient in ax. If the quotient is too large to fit in ax - which could happen if dx contains some "random" value - you end up with a division overflow. That's why you should clear dx prior to the division.
    – Michael
    Sep 12, 2013 at 18:13

2 Answers 2

1

You must clear DX before performing the operation. Division in x86 will operate on DX:AX. If you fail to clear it first and it has some content, you may end up with an overflow condition. Obviously, this is bad. :)

So:

dividing: 
pop ax
div bx

should become:

dividing: 
pop ax
mov dx,0
div bx
-1
.model small
.stack 100h

.data
msg1 db 13,10,13,10, "Enter 1st Number : $"
msg2 db 13,10, "Enter 2nd Number : $" 
msg01 db 13,10,13,10, "Enter base Number : $"
msg02 db 13,10, "Enter power Number : $"
msgEr db 13,10, "Error $"
msgCh db 13,10, "Press A to ADD , S to SUBTRACT ,M to MULTIPLY, D to DIVIDE,F to FIND FACTORIAL,P to FIND X POWER Y, X to EXIT : $ "
msgSum db 13,10,13,10, "Sum is : $"
msgDif db 13,10,13,10, "Difference is : $"
msgDiv db 13,10,13,10, "Quotient is : $"
msgMul db 13,10,13,10, "Product is : $" 
msgPOW db 13,10,13,10, "ANS is : $"
tmp     db ?


.code

start:
mov ax, @data
mov ds, ax





lea dx, msgCh
mov ah, 09h
int 21h

mov ah, 1
mov dl, al
int 21h

cmp al,'d'
je dividing

cmp al,'a'  
je adding

cmp al,'s'
je subtracting

cmp al,'m'
je multiplying   

cmp al,'f'
je fact

cmp al,'p'
je x_pow_y

cmp al,'x'
mov ah, 4ch
int 21h

error:
lea dx,msgEr
mov ah,09h
int 21h 
jmp start

;//////////////////////////////////////

dividing:
lea dx, msg1
mov ah, 9
int 21h  

start1:
mov ah, 1
int 21h
cmp al,0dh      
je next1
mov ah,0        
sub al,30h      
push ax         
mov ax,10d      
mul bx          
pop bx          
add bx,ax       
jmp start1      




next1:
push bx
lea dx,msg2
mov ah,09h
int 21h


mov bx,0


start2:
mov ah,1
int 21h
cmp al,0dh
je d
mov ah,0
sub al,30h
push ax
mov ax,10d
mul bx
pop bx
add bx,ax 
jmp start2
d:
pop ax
mov dx,0
div bx
push ax
lea dx,msgDiv
mov ah,09h
int 21h 
pop ax
mov cx,0
mov dx,0
mov bx,10d
jmp break



 ;///////////////////////////////////

adding:  
lea dx, msg1
mov ah, 9
int 21h
mov bx, 0


start01:
mov ah, 1
int 21h
cmp al,0dh      
je next01
mov ah,0        
sub al,30h      
push ax         
mov ax,10d      
mul bx          
pop bx          
add bx,ax       
jmp start01      




next01:
push bx
lea dx,msg2
mov ah,09h
int 21h


mov bx,0


start02:
mov ah,1
int 21h
cmp al,0dh
je a
mov ah,0
sub al,30h
push ax
mov ax,10d
mul bx
pop bx
add bx,ax 
jmp start02 
a:   
pop ax
add ax,bx   
push ax
lea dx,msgSum   
mov ah,09h
int 21h 
pop ax
mov cx,0
mov dx,0
mov bx,10d
jmp break 




;///////////////////////////////  

multiplying:

lea dx, msg1
mov ah, 9
int 21h
mov bx, 0

start001:
mov ah, 1
int 21h
cmp al,0dh      
je next001
mov ah,0        
sub al,30h      
push ax         
mov ax,10d      
mul bx          
pop bx          
add bx,ax       
jmp start001      




next001:
push bx
lea dx,msg2
mov ah,09h
int 21h


mov bx,0


start002:
mov ah,1
int 21h
cmp al,0dh
je m
mov ah,0
sub al,30h
push ax
mov ax,10d
mul bx
pop bx
add bx,ax 
jmp start002
m: 
pop ax 
mov dx,0
mul bx      
push ax     
lea dx,msgMul   
mov ah,09h
int 21h 
pop ax
mov cx,0
mov dx,0
mov bx,10d
jmp break

;//////////////////////////////////  

fact: 

lea dx, msg1
mov ah, 9
int 21h
mov bx, 0

start003:
mov ah, 1
int 21h
cmp al,0dh      
je l
mov ah,0        
sub al,30h      
push ax


mov ax,10d      
mul bx          
pop bx          
add bx,ax       
jmp start003      


l:                                       ;ax=6
mov cx,bx
mov ax,1 

jmp l2
l2:
cmp cx,0
je l1
mov dx,0 
mul cx

push ax

lea dx,msgPOW   
mov ah,09h
int 21h  

sub cx,1
pop ax

jmp l2
l1: 
mov dx,0
mov bx,10d
jmp break 

;//////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\

x_pow_y:


lea dx, msg01
mov ah, 9
int 21h
mov bx, 0

start004:
mov ah, 1
int 21h
cmp al,0dh

je next004
mov ah,0        
sub al,30h      
push ax         
mov ax,10d      
mul bx          
pop bx          
add bx,ax       
jmp start004      




next004:
push bx
lea dx,msg02
mov ah,09h
int 21h


mov bx,0


start005:
mov ah,1
int 21h
cmp al,0dh
je p
mov ah,0
sub al,30h
push ax
mov ax,10d
mul bx
pop bx
add bx,ax 
jmp start005 

p:  
mov cx,bx 

pop ax
mov bx,ax 
mov dx,0


c:
cmp cx,1
je c1
mul bx      
push ax     
lea dx,msgPOW   
mov ah,09h
int 21h
sub cx,1 
pop ax
jmp c
c1:
mov cx,0
mov dx,0
mov bx,10d
jmp break







 ;/////////////////////////////// \\\\\\\\\\\\\\\\\\\\\ ' _ '

subtracting: 
lea dx, msg1
mov ah, 9
int 21h
mov bx, 0

start0001:
mov ah, 1
int 21h
cmp al,0dh      
je next0001
mov ah,0        
sub al,30h      
push ax         
mov ax,10d      
mul bx          
pop bx          
add bx,ax       
jmp start0001      




next0001:
push bx
lea dx,msg2
mov ah,09h
int 21h


mov bx,0


start0002:
mov ah,1
int 21h
cmp al,0dh
je s
mov ah,0
sub al,30h
push ax
mov ax,10d
mul bx
pop bx
add bx,ax 
jmp start0002
s:
pop ax
sub ax,bx 
push ax
lea dx,msgDif
mov ah,09h
int 21h 
pop ax
mov cx,0
mov dx,0
mov bx,10d

break:
div bx
push dx
mov dx,0
inc cx
or ax,ax 
jne break 

ans:        
pop dx
add dl,30h
mov ah,02h
int 21h
loop ans

jmp start

end start
2
  • Use the editor functions to mark code as code and text as text. This makes your post more readable. :)
    – nologin
    Apr 19, 2019 at 9:16
  • This answer is basically useless if you don't say (in text outside the code) what was wrong and what you changed. Apr 19, 2019 at 15:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.