Adding two 8 byte numbers using assembly language
up vote
0
down vote
favorite
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 ?
assembly emu8086
New contributor
tania miah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
favorite
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 ?
assembly emu8086
New contributor
tania miah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
You can use twodddirectives instead. Remember that 8086 is a little endian architecture, so the least significant dword goes first.
– fuz
Nov 10 at 17:55
if I use two dd directives I wont be able to save the result of the addition in one variable ..
– tania miah
Nov 10 at 17:59
2
there are no variables in assembly, just memory. It doesn't matter if you reserve 8 bytes by singledqor by twodd, there will be the same 8 bytes defined in memory either way. Labels likenum1are memory "bookmarks" pointing to the first byte in memory, they are not "variables" like holding some type or guarding something, just memory address you can use as wish. So in assemblydq 123456789ABCDEF0h=dd 9ABCDEF0h, 12345678h, the resulting machine code is identical. (MASM does somewhat guard "type" of labels, but that's single assembler out of 20 available for x86, so who cares...)
– Ped7g
Nov 10 at 18:11
2
Tania, please edit the question to add all the information information needed to answer it— the error messages that are in your comment and also the assembler you are using.
– prl
Nov 10 at 19:21
yep, this question is actually quite reasonable, would you provide the extra details, which especially in assembly can make whole world of difference.. you may want to read Minimal, Complete, and Verifiable example and fix your question by that.
– Ped7g
Nov 10 at 19:30
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 ?
assembly emu8086
New contributor
tania miah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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 ?
assembly emu8086
assembly emu8086
New contributor
tania miah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
tania miah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Nov 10 at 21:04
user6910411
31.6k76592
31.6k76592
New contributor
tania miah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Nov 10 at 17:30
tania miah
61
61
New contributor
tania miah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
tania miah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
tania miah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
You can use twodddirectives instead. Remember that 8086 is a little endian architecture, so the least significant dword goes first.
– fuz
Nov 10 at 17:55
if I use two dd directives I wont be able to save the result of the addition in one variable ..
– tania miah
Nov 10 at 17:59
2
there are no variables in assembly, just memory. It doesn't matter if you reserve 8 bytes by singledqor by twodd, there will be the same 8 bytes defined in memory either way. Labels likenum1are memory "bookmarks" pointing to the first byte in memory, they are not "variables" like holding some type or guarding something, just memory address you can use as wish. So in assemblydq 123456789ABCDEF0h=dd 9ABCDEF0h, 12345678h, the resulting machine code is identical. (MASM does somewhat guard "type" of labels, but that's single assembler out of 20 available for x86, so who cares...)
– Ped7g
Nov 10 at 18:11
2
Tania, please edit the question to add all the information information needed to answer it— the error messages that are in your comment and also the assembler you are using.
– prl
Nov 10 at 19:21
yep, this question is actually quite reasonable, would you provide the extra details, which especially in assembly can make whole world of difference.. you may want to read Minimal, Complete, and Verifiable example and fix your question by that.
– Ped7g
Nov 10 at 19:30
add a comment |
You can use twodddirectives instead. Remember that 8086 is a little endian architecture, so the least significant dword goes first.
– fuz
Nov 10 at 17:55
if I use two dd directives I wont be able to save the result of the addition in one variable ..
– tania miah
Nov 10 at 17:59
2
there are no variables in assembly, just memory. It doesn't matter if you reserve 8 bytes by singledqor by twodd, there will be the same 8 bytes defined in memory either way. Labels likenum1are memory "bookmarks" pointing to the first byte in memory, they are not "variables" like holding some type or guarding something, just memory address you can use as wish. So in assemblydq 123456789ABCDEF0h=dd 9ABCDEF0h, 12345678h, the resulting machine code is identical. (MASM does somewhat guard "type" of labels, but that's single assembler out of 20 available for x86, so who cares...)
– Ped7g
Nov 10 at 18:11
2
Tania, please edit the question to add all the information information needed to answer it— the error messages that are in your comment and also the assembler you are using.
– prl
Nov 10 at 19:21
yep, this question is actually quite reasonable, would you provide the extra details, which especially in assembly can make whole world of difference.. you may want to read Minimal, Complete, and Verifiable example and fix your question by that.
– Ped7g
Nov 10 at 19:30
You can use two
dd directives instead. Remember that 8086 is a little endian architecture, so the least significant dword goes first.– fuz
Nov 10 at 17:55
You can use two
dd directives instead. Remember that 8086 is a little endian architecture, so the least significant dword goes first.– fuz
Nov 10 at 17:55
if I use two dd directives I wont be able to save the result of the addition in one variable ..
– tania miah
Nov 10 at 17:59
if I use two dd directives I wont be able to save the result of the addition in one variable ..
– tania miah
Nov 10 at 17:59
2
2
there are no variables in assembly, just memory. It doesn't matter if you reserve 8 bytes by single
dq or by two dd, there will be the same 8 bytes defined in memory either way. Labels like num1 are memory "bookmarks" pointing to the first byte in memory, they are not "variables" like holding some type or guarding something, just memory address you can use as wish. So in assembly dq 123456789ABCDEF0h = dd 9ABCDEF0h, 12345678h, the resulting machine code is identical. (MASM does somewhat guard "type" of labels, but that's single assembler out of 20 available for x86, so who cares...)– Ped7g
Nov 10 at 18:11
there are no variables in assembly, just memory. It doesn't matter if you reserve 8 bytes by single
dq or by two dd, there will be the same 8 bytes defined in memory either way. Labels like num1 are memory "bookmarks" pointing to the first byte in memory, they are not "variables" like holding some type or guarding something, just memory address you can use as wish. So in assembly dq 123456789ABCDEF0h = dd 9ABCDEF0h, 12345678h, the resulting machine code is identical. (MASM does somewhat guard "type" of labels, but that's single assembler out of 20 available for x86, so who cares...)– Ped7g
Nov 10 at 18:11
2
2
Tania, please edit the question to add all the information information needed to answer it— the error messages that are in your comment and also the assembler you are using.
– prl
Nov 10 at 19:21
Tania, please edit the question to add all the information information needed to answer it— the error messages that are in your comment and also the assembler you are using.
– prl
Nov 10 at 19:21
yep, this question is actually quite reasonable, would you provide the extra details, which especially in assembly can make whole world of difference.. you may want to read Minimal, Complete, and Verifiable example and fix your question by that.
– Ped7g
Nov 10 at 19:30
yep, this question is actually quite reasonable, would you provide the extra details, which especially in assembly can make whole world of difference.. you may want to read Minimal, Complete, and Verifiable example and fix your question by that.
– Ped7g
Nov 10 at 19:30
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
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
add a comment |
up vote
1
down vote
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
add a comment |
up vote
1
down vote
up vote
1
down vote
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
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
answered Nov 11 at 15:51
Sep Roland
11.4k21843
11.4k21843
add a comment |
add a comment |
tania miah is a new contributor. Be nice, and check out our Code of Conduct.
tania miah is a new contributor. Be nice, and check out our Code of Conduct.
tania miah is a new contributor. Be nice, and check out our Code of Conduct.
tania miah is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241590%2fadding-two-8-byte-numbers-using-assembly-language%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can use two
dddirectives instead. Remember that 8086 is a little endian architecture, so the least significant dword goes first.– fuz
Nov 10 at 17:55
if I use two dd directives I wont be able to save the result of the addition in one variable ..
– tania miah
Nov 10 at 17:59
2
there are no variables in assembly, just memory. It doesn't matter if you reserve 8 bytes by single
dqor by twodd, there will be the same 8 bytes defined in memory either way. Labels likenum1are memory "bookmarks" pointing to the first byte in memory, they are not "variables" like holding some type or guarding something, just memory address you can use as wish. So in assemblydq 123456789ABCDEF0h=dd 9ABCDEF0h, 12345678h, the resulting machine code is identical. (MASM does somewhat guard "type" of labels, but that's single assembler out of 20 available for x86, so who cares...)– Ped7g
Nov 10 at 18:11
2
Tania, please edit the question to add all the information information needed to answer it— the error messages that are in your comment and also the assembler you are using.
– prl
Nov 10 at 19:21
yep, this question is actually quite reasonable, would you provide the extra details, which especially in assembly can make whole world of difference.. you may want to read Minimal, Complete, and Verifiable example and fix your question by that.
– Ped7g
Nov 10 at 19:30