How to keep negative numbers in assembly language TASM ? x86

869 Views Asked by At

;I have this problem and I should solve it with the given numbers pls somebody help!!! ;13. (a+b+c*d)/(9-a) ;a,c,d-byte; b-doubleword

ASSUME cs:code, 

ds:data

DATA SEGMENT
a db 11
b dd 1
c db -2
d db 2

res1 dw ?
finalres dw ?

data ends

code segment 

start:

mov ax, data
mov ds, ax

mov al, a
cbw
mov bl,9
cbw
sub bx,ax
mov res1, ax

mov al, c
cbw
mul d
mov cl,a
cbw
add ax,cx

mov bx, word ptr b
mov cx, word ptr b+2
add bx, ax
adc cx, dx
mov ax, bx
mov dx ,cx
mov cx, res1
cwd
div res1
mov finalres, ax

mov ax, 4C00h
int 21h

code ends

end start
1

There are 1 best solutions below

1
On

Your code contains multiple errors:

mov al, a
cbw
mov bl,9
  ## This cbw will do a conversion AL -> AX
  ## cbw/cwd instructions always influence the AX
  ## register. It is not possible to use cbw for
  ## the BX register!
  ##
  ## BTW: The value of the BH part of the BX
  ## register is undefined here!
cbw
sub bx,ax
mov res1, ax
mov al, c
cbw
  ## Mul is an unsigned multiplication!
  ## Imul would do a signed one!
mul d
mov cl,a
  ## Again you try to use cbw for another register
  ## than AX -> This will only destroy the AX
  ## register!
cbw
add ax,cx
  ## Because the "add ax,cx" may generate a carry
  ## you'll have to do an "adc dx, 0" here!
mov bx, word ptr b
mov cx, word ptr b+2
add bx, ax
adc cx, dx
  ## Why didn't you do an "add ax, bx" and an
  ## "adc dx, cx" - you would not need the next
  ## two instructions in this case?
  ## (However this is not a real error.)
mov ax, bx
mov dx, cx
  ## This instruction is useless
  ## unless you use "(i)div cx" below!
mov cx, res1
  ## Why do you do this "cwd" here?
  ## The high 16 bits of the 32-bit word "b"
  ## will get lost when doing so!
  ##
  ## All operations on the "dx" register above
  ## are also useless when doing this here!
cwd
  ## "div" will do an unsigned division. For a
  ## signed division use "idiv"
div res1
mov finalres, ax

I'm not sure if I found all mistakes in your code.