tania miah
tania miah

Reputation: 15

Adding two 8 byte numbers using assembly language

This is my code for a program that adds two 8 byte numbers.

.model small 
.100h 
.data
num1 dq 1234567812345678h
num2 dq 1234567854636732h
num3 dq ?
.code
mov ax,@data
mov ds,ax
mov ax,num1
add ax,num2
mov bx,num1+2
adc bx,num2+2
mov cx,num1+4
adc cx,num2+4
mov dx,num1+6
adc dx,num2+6

mov num3,ax
mov num3+2,bx
mov num3+4,cx
mov num3+6,dx 

end

For some reason it says that there is an error in defining my variables:

(3) illegal instruction: num1 dq 1111111123145678h or wrong parameters. 
(4) illegal instruction: num2 dq 1111111123145678h or wrong parameters. 
(5) illegal instruction: num3 dq ? or wrong parameters. 
(9) wrong parameters: MOV ax,num1 
(9) probably no zero prefix for hex; or no 'h' suffix; or wrong addressing; or undefined var: num1 

Does anyone have an idea about whats wrong with it ?

Upvotes: 0

Views: 4347

Answers (1)

Sep Roland
Sep Roland

Reputation: 39166

num1 dq 1234567812345678h
num2 dq 1234567854636732h
num3 dq ?

it says that there is an error in defining my variables

The fact that you're splitting up the calculation in chunks of 16 bits doesn't match too well with the ability to specify a 64-bit immediate in the dq directive. I could even imagine for the dq directive to not be available at all.

You can always specify those large 64-bit numbers using their constituing smaller parts. You just need to be aware that X86 is a little endian architecture and therefore the least significant portion of the number goes in the lowest memory address:

Using byte size portions:

12_34_56_78_54_63_67_32h
                     ^ least significant part

num2    db           32h, 67h, 63h, 54h, 78h, 56h, 34h, 12h
                     ^ lowest memory address

Using word size portions:

1234_5678_5463_6732h
               ^ least significant part

num2    dw     6732h, 5463h, 5678h, 1234h
               ^ lowest memory address

In your program this becomes:

num1    dw     5678h, 1234h, 5678h, 1234h
num2    dw     6732h, 5463h, 5678h, 1234h
num3    dw     4 dup 0

Your addition works but it's not necessary to use that many registers. You can easily code this task using a single register:

mov     ax, num1
add     ax, num2
mov     num3, ax
mov     ax, num1+2
adc     ax, num2+2
mov     num3+2, ax
mov     ax, num1+4
adc     ax, num2+4
mov     num3+4, ax
mov     ax, num1+6
adc     ax, num2+6
mov     num3+6, ax

Now this begs for a loop of some kind.

    mov     bx, offset num1 ;num1, num2, and num3 are consecutive in memory
    clc                     ;Because there's no simple ADD first
More:
    mov     ax, [bx]        ;Load word from num1
    adc     ax, [bx+8]      ;Plus word from num2
    mov     [bx+16], ax     ;Store in word from num3
    add     bx, 2           ;Go to next word
    cmp     bx, num2        ;num2 immediately follows num1 in memory
    jb      More

Upvotes: 1

Related Questions