0

I have to make a code that solves an equation ( in code) The cube of y and the 10*x^2 values are wrong. Also can anyone help me how to print the variable ans? I am currently using emu8086 and im viewing the values of every variable from emu options. This is the code i wrote. Im a beginner so i dont want to complicate this code. Please suggest

.MODEL SMALL
.STACK 100H
.DATA

equation DB "Equation: (10x^2 - 6y^3)*3z + y^3 + z^4",0DH,0AH ,'$'

MSGX DB 0DH,0AH,"ENTER VALUE OF X",0DH,0AH,'$'
MSGY DB 0DH,0AH,"ENTER VALUE OF Y",0DH,0AH,'$'
MSGZ DB 0DH,0AH,"ENTER VALUE OF Z",0DH,0AH,'$' 
MSGNO DB "",0DH,0AH,'$' 

X DB ?
Y DB ?
Z DB ? 
SQUAREX DW ?   
CUBEY DW ? 
SQUARE10X DW ?
CUBE6Y DW ?
3Z DW ?
ZP4 DW ? ; z powered 4
BRAC1 DW ?
BRAC2 DW ?  
ANS DW ?

.CODE
.STARTUP  


; INPUT X                 (10x^2 - 6y^3)*3z + y^3 + z^4
MOV AH,9
MOV DX,OFFSET MSGX
INT 21H
MOV AH,1
INT 21H
MOV X,AL
SUB X,48 
MOV AH,9
MOV DX,OFFSET MSGNO
INT 21H

; INPUT Y                      (10x^2 - 6y^3)*3z + y^3 + z^4
MOV AH,9
MOV DX,OFFSET MSGY
INT 21H
MOV AH,1
INT 21H
MOV Y,AX
SUB Y,48  
MOV AH,9
MOV DX,OFFSET MSGNO
INT 21H

; INPUT Z         
MOV AH,9
MOV DX,OFFSET MSGZ
INT 21H
MOV AH,1
INT 21H
MOV Z,AL
SUB Z,48 
MOV AH,9
MOV DX,OFFSET MSGNO
INT 21H       


;ZP4                                     (10x^2 - 6y^3)*3z + y^3 + z^4
 
MOV AL, Z
MOV BL,Z
MUL BL 
MOV CL,Z
MUL CL   
MOV DL,Z
MUL DL
AAM   
MOV ZP4,AX

                     
;Y^3
MOV AL,Y
MOV BL,Y
MUL BL 
MOV CL,Y
MUL CL
AAM   
MOV CUBEY,AX   

;6Y^3                        (10x^2 - 6y^3)*3z + y^3 + z^4
MOV AX,CUBEY 
MOV BL,6
MUL BL
AAM
MOV CUBE6Y,AX

;X^2                
MOV AL,X
MOV BL,X
MUL BL
AAM   
MOV SQUAREX,AX 

;10X^2                
MOV AX,SQUAREX
MOV BL,10
MUL BL
AAM   
MOV SQUARE10X,AX 

;3Z
MOV AL,Z
MOV BL,3
MUL BL
AAM
MOV 3Z,AX 

  
                            
;bracket 1                  (10x^2 - 6y^3)
MOV AX,SQUARE10X
SUB AX,CUBE6Y   
AAA 
MOV BRAC1 ,AX
                           
;bracket 2                   (3z + y^3 + z^4)       
MOV AX,3Z
ADD AX,CUBEY 
ADD AX, ZP4 
AAA 
MOV BRAC2 ,AX

MOV AX, BRAC1
MUL BRAC2
MOV ANS, AX


.EXIT
END 
ali Syed
  • 19
  • 2
  • 3
    Avoid bcd if possible. If not, make sure you use the adjustments properly, e.g. you seem to have a couple of multiplications where you don't use `AAM`. Also `3z` isn't `z*z*z` that you calculate. You don't need to reload (or even load) variables from memory. It's good that you use emu8086 to check register values, so single step the code and spot the first mistake and fix that and repeat. – Jester Jul 16 '20 at 13:38
  • The this i dont get is when i input Y (eg Y = 2) , the code calculates Y^3 correctly as ( Y^3 = 8) but the value of 6*Y^3 the code calculates ( 408) instead of 48. Happens with every Y value. Whydoes this 0 comes in between 48? – ali Syed Jul 16 '20 at 18:59
  • `aam` works for unpacked bcd arithmetic which means 1 decimal digit per byte. Note you don't get 408 decimal, you get 0x408 hex which means `ah=4` and `al=8` so `48` in unpacked bcd. – Jester Jul 16 '20 at 21:21
  • so can i convert that 0x408h to to actually look like 48 to user thats using my program? – ali Syed Jul 17 '20 at 11:22
  • Sure, you can just print `ah` and `al` separately as two digits. Your code will only work up to 99 obviously. – Jester Jul 17 '20 at 11:39
  • Please tell me how to print a variable too? i wanna print the ans variable – ali Syed Jul 17 '20 at 11:52
  • Just add the ascii code of `0` (48) back and print each digit as a character. – Jester Jul 17 '20 at 11:59
  • I am still unable to get the problem with my code. can you tell me what should i change in code to make it work fine? i need to send this by 18 july please help – ali Syed Jul 17 '20 at 17:14
  • @aliSyed There's nothing that I can do about your deadline of 18 july, but I've written a complete answer for your benefit nonetheless. – Sep Roland Jul 19 '20 at 16:59

1 Answers1

2

Your code has several problems

  • a typo on inputting Y. MOV Y,AX should be mov Y, al. Luckily this would not show in the results
  • you're using the AAM instruction needlessly
  • on several occasions you're using the byte-sized multiplication when in fact you should be using the word-sized multiplication
  • you're not following the normal algebraic rules on BRAC2. Adding a second pair of brackets on (10x^2 - 6y^3)*3z + y^3 + z^4 gives ((10x^2 - 6y^3)*3z) + y^3 + z^4 and not as you did (10x^2 - 6y^3)*(3z + y^3 + z^4). Remember that multiplication takes precedence over addition!
; ZP4                 (10x^2 - 6y^3)*3z + y^3 + z^4
mov al, Z
mul al
mul al
mov ZP4, ax           ; Could be as big as 9 x 9 x 9 x 9 == 6561
                     
; Y^3
mov al,Y
mul al
mov bl,Y
mul bl 
mov CUBEY, ax         ; Could be as big as 9 x 9 x 9 == 729

; 6Y^3
mov ax, CUBEY         ; 729 would not fit in AL register!
mov bx, 6
mul bx                ; Needs to be word-sized multiplication!
mov CUBE6Y, ax        ; Could be as big as 6 x 9 x 9 x 9 == 4374

; X^2
mov al, X
mul al
mov SQUAREX, ax 

; 10X^2
mov bl, 10
mul bl
mov SQUARE10X, ax     ; Could be as big as 10 x 9 x 9 == 810 

; 3Z
mov al, Z
mov bl, 3
mul bl
mov 3Z, ax 

; bracket 1           (10x^2 - 6y^3)
mov ax, SQUARE10X
sub ax, CUBE6Y        ; This could be a NEGATIVE value!
             
; bracket 2           ((10x^2 - 6y^3)*3z)
mov bx, 3Z
imul bx

; additions           ((10x^2 - 6y^3)*3z) + y^3 + z^4
add ax, CUBEY
add ax, ZP4
mov ANS, ax

Also can anyone help me how to print the variable ans?

I've written this Q/A Displaying numbers with DOS that explains in detail how you can print your answer.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76