如何在汇编语言TASM中保留负数? 86

时间:2014-11-19 11:49:21

标签: assembly tasm

;我有这个问题,我应该用给定的数字解决它有人帮忙! ; 13。 (A + B + C * d)/(9-a)的 ; A,C,d字节; B-双字

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 个答案:

答案 0 :(得分:0)

您的代码包含多个错误:

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

我不确定我是否在代码中发现了所有错误。