C. Smith
C. Smith

Reputation: 77

Calculate result of formula

Can someone help me solve this problem in Assembly language in signed and unsigned representation?

My task is to calculate the result of the following term:

(a*a+b)/(b+b)+c

where a is a word, b is a byte , and c a doubleword

I tried this:

assume CS:code, DS:data
data segment
    a dw 1
    b db 2
    c dd 3
    x dd ?
data ends
code segment
    start:
        mov ax, data
        mov ds, ax

        mov ax, a   ; AX = a
        mul a       ; AX = a*a
        add ax, 2   ; AX = a*a + 2

        mov bl, b   ; BX = b
        add bl, b   ; BX = b*b  
        div bl      ; a*a+b/b+b
        mov ax, word ptr c+2
        mov dx, word ptr c
        add ax, word ptr c
        mov word ptr x+2, dx
        mov word ptr x, ax

        mov ax, 4c00h
        int 21h
    code ends
end start

Upvotes: 0

Views: 90

Answers (1)

Fifoernik
Fifoernik

Reputation: 9899

mov ax, a   ; AX = a
mul a       ; AX = a*a

Wrong comment: The result of this mul is in DX:AX

add ax, 2   ; AX = a*a + 2

I don't think you're supposed to add b as an immediate!

mov bl, b   ; BX = b
add bl, b   ; BX = b*b  

Wrong comment: It's an addition.

div bl      ; a*a+b/b+b

You would better make this a word division by zeroing the BH register prior to div bx. Also where have the brackets gone in your comment?

mov ax, word ptr c+2
mov dx, word ptr c
add ax, word ptr c

Here you're doing it all wrong. The result of the new word division will be in AX. So add the low word of c to AX and add with carry the high word of c to an empty DX register.

mov word ptr x+2, dx
mov word ptr x, ax

Seems OK.

Upvotes: 1

Related Questions