0

I should do two projects. The first a, b, c are bytes and the second they are double type.
The equation: a-(b-c:2).

What I've managed to do (double type I think):

.model small
.stack 100h
.data
a db 0
b db 0
c db 0
result db ?

message1 db, "Equation: a-(b-c:2) a=$"
message2 db, "b=$"    ;<=========
message3 db, "c=$"    ;<========= LINEBREAK
message4 db, "Result=$"
.code

start: mov ax, data
mov ds,ax

mov ax, seg message1  ;get a and save to a variable
mov ds,ax
mov dx,offset message 1
mov ah, 9h
int 21h
mov ah, 1h
int 21h
sub al,30h  ;converting to real number
mov a,al

mov ax, seg message2  ;get b and save to a variable
mov ds,ax
mov dx,offset message2
mov ah, 9h
int 21h
mov ah, 1h
int 21h
sub al,30h  ;converting to real number
mov b,al

mov ax, seg message3  ;get c and save to a variable
mov ds,ax
mov dx,offset message3
mov ah, 9h
int 21h
mov ah, 1h
int 21h
sub al,30h ; converting  to real numebr
mov c,al
GSerg
  • 76,472
  • 17
  • 159
  • 346
  • 1
    It's unclear what you mean by **double**! I could imagine it meaning word-size (`dw`) being double as big as byte-size (`db`). Also lines like `message1 db, "Equation: a-(b-c:2) a=$"` have a comma inserted after the `db` directive that is probably wrong. – Sep Roland Dec 24 '20 at 15:51

1 Answers1

0

To solve an equation in a computer program, just like elsewhere, you need to follow the algebraic rules:

  • parenthesised items are to be treated as a whole
  • inside a parenthesised item the normal precedences apply, * and / come before + and -

For the expression a-(b-c:2) it comes down to these 3 steps:

  1. Divide c by 2, a mere shift to the right accomplishes this
  2. Subtract the result from step 1 from b
  3. Subtract the result from step 2 from a

This is a one-register solution. It's based on: a - b <=> a + (-b) <=> (-b) + a

; Step 1
mov al, c
shr al, 1
; Step 2
neg al
add al, b
; Step 3
neg al
add al, a
Sep Roland
  • 33,889
  • 7
  • 43
  • 76