Reputation: 19
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
Upvotes: 0
Views: 89
Reputation: 39676
Your code has several problems
MOV Y,AX
should be mov Y, al
. Luckily this would not show in the resultsAAM
instruction needlessly(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.
Upvotes: 2