I have to do an operation like this in 8086
((x*y)-c) 1
I wrote this code:
.stack
.data
A dw 180
B db 4
C dw 300
.code
start:
MOV AX, 00h
MOV AX, A
MUL B
SUB AX, C
DEC AX
int 21h
end start
This code works but I don't think does the expression I wanted...
Where am I wrong?
CodePudding user response:
This code works but I don't think does the expression I wanted...
((x * y) - c) 1
The calculation is almost correct. Just the DEC AX
should become INC AX
so it matches the expression where you add 1 in the end:
Looking at my teacher's example he always uses
int 21h
at the end of his code
I'm sure your teacher's code will have something like mov ah, 00h
or mov ax, 4C00h
directly above that line. It's meant to terminate the program and return to DOS.
This is the code that additionally shows the result of the calculation on the screen. The program waits for a keyboard key before it terminates giving you the opportunity to actually see the outcome.
start:
mov ax, A
mul B
sub ax, C
inc ax ; -> AX == 421
mov bx, 10
xor cx, cx
Divide:
xor dx, dx ; Setup for division DX:AX / BX
div bx ; -> Quotient AX, Remainder DX=[0,9]
push dx ; (1)
inc cx
test ax, ax
jnz Divide
Show:
pop dx ; (1)
add dl,"0" ; [0,9] -> ["0","9"]
mov ah, 02h ; DOS.DisplayCharacter
int 21h ; -> AL
dec cx
jnz Show
mov ah, 01h ; DOS.GetKey
int 21h ; -> AL
mov ax, 4C00h ; DOS.Terminate
int 21h
end start