Assembly - Division giving weird result
up vote
1
down vote
favorite
I'm using a x86 emulator and I'm trying to write a short program that receives an integer and converts it to binary. However, I run it step by step and check the memory (and the ax register as it is updated) I can see that the ax register evolves as it follows: 14 -> 7 -> 3 -> 32769 (Instead of 1). Why is this happening? I've tried using EAX and ECX instead of ax and cx but it still wont give me the correct result. Am I missing something really obvious?
value: dw 14
binary: dw 0
mov ax,0
mov cx,0
mov dx,0
;CONVERTING 14 TO BINARY
CONVERSION:
mov ax,[valor]
mov cx,2
div cx
push dx
mov [valor],ax
cmp ax,0
jne CONVERSION
pop [binary]
assembly x86
|
show 9 more comments
up vote
1
down vote
favorite
I'm using a x86 emulator and I'm trying to write a short program that receives an integer and converts it to binary. However, I run it step by step and check the memory (and the ax register as it is updated) I can see that the ax register evolves as it follows: 14 -> 7 -> 3 -> 32769 (Instead of 1). Why is this happening? I've tried using EAX and ECX instead of ax and cx but it still wont give me the correct result. Am I missing something really obvious?
value: dw 14
binary: dw 0
mov ax,0
mov cx,0
mov dx,0
;CONVERTING 14 TO BINARY
CONVERSION:
mov ax,[valor]
mov cx,2
div cx
push dx
mov [valor],ax
cmp ax,0
jne CONVERSION
pop [binary]
assembly x86
1
You need themov dx,0
inside the loop.
– Jester
Nov 9 at 22:14
@Jester: why? (Explaing why would make a good answer.)
– usr2564301
Nov 9 at 22:19
Yourpush dx
may be executed many more times than you are popping it (only once). Best make sure you pop everything that you push. For this toy program it won't matter -- unkess you are going to try larger values next -- but it's a good general rule to keep in mind for anything larger than this.
– usr2564301
Nov 9 at 22:21
Addingmov dx,0
to the loop made the div work properly! But I just realized that by doing pop into [binary].. isn't giving me the right result either..!
– falsekein
Nov 9 at 22:32
We don't really know what you are trying to do. Note thatdw 14
is already in binary. Maybe you mean a text representation of zeroes and ones?
– Jester
Nov 9 at 22:34
|
show 9 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm using a x86 emulator and I'm trying to write a short program that receives an integer and converts it to binary. However, I run it step by step and check the memory (and the ax register as it is updated) I can see that the ax register evolves as it follows: 14 -> 7 -> 3 -> 32769 (Instead of 1). Why is this happening? I've tried using EAX and ECX instead of ax and cx but it still wont give me the correct result. Am I missing something really obvious?
value: dw 14
binary: dw 0
mov ax,0
mov cx,0
mov dx,0
;CONVERTING 14 TO BINARY
CONVERSION:
mov ax,[valor]
mov cx,2
div cx
push dx
mov [valor],ax
cmp ax,0
jne CONVERSION
pop [binary]
assembly x86
I'm using a x86 emulator and I'm trying to write a short program that receives an integer and converts it to binary. However, I run it step by step and check the memory (and the ax register as it is updated) I can see that the ax register evolves as it follows: 14 -> 7 -> 3 -> 32769 (Instead of 1). Why is this happening? I've tried using EAX and ECX instead of ax and cx but it still wont give me the correct result. Am I missing something really obvious?
value: dw 14
binary: dw 0
mov ax,0
mov cx,0
mov dx,0
;CONVERTING 14 TO BINARY
CONVERSION:
mov ax,[valor]
mov cx,2
div cx
push dx
mov [valor],ax
cmp ax,0
jne CONVERSION
pop [binary]
assembly x86
assembly x86
asked Nov 9 at 22:11
falsekein
61
61
1
You need themov dx,0
inside the loop.
– Jester
Nov 9 at 22:14
@Jester: why? (Explaing why would make a good answer.)
– usr2564301
Nov 9 at 22:19
Yourpush dx
may be executed many more times than you are popping it (only once). Best make sure you pop everything that you push. For this toy program it won't matter -- unkess you are going to try larger values next -- but it's a good general rule to keep in mind for anything larger than this.
– usr2564301
Nov 9 at 22:21
Addingmov dx,0
to the loop made the div work properly! But I just realized that by doing pop into [binary].. isn't giving me the right result either..!
– falsekein
Nov 9 at 22:32
We don't really know what you are trying to do. Note thatdw 14
is already in binary. Maybe you mean a text representation of zeroes and ones?
– Jester
Nov 9 at 22:34
|
show 9 more comments
1
You need themov dx,0
inside the loop.
– Jester
Nov 9 at 22:14
@Jester: why? (Explaing why would make a good answer.)
– usr2564301
Nov 9 at 22:19
Yourpush dx
may be executed many more times than you are popping it (only once). Best make sure you pop everything that you push. For this toy program it won't matter -- unkess you are going to try larger values next -- but it's a good general rule to keep in mind for anything larger than this.
– usr2564301
Nov 9 at 22:21
Addingmov dx,0
to the loop made the div work properly! But I just realized that by doing pop into [binary].. isn't giving me the right result either..!
– falsekein
Nov 9 at 22:32
We don't really know what you are trying to do. Note thatdw 14
is already in binary. Maybe you mean a text representation of zeroes and ones?
– Jester
Nov 9 at 22:34
1
1
You need the
mov dx,0
inside the loop.– Jester
Nov 9 at 22:14
You need the
mov dx,0
inside the loop.– Jester
Nov 9 at 22:14
@Jester: why? (Explaing why would make a good answer.)
– usr2564301
Nov 9 at 22:19
@Jester: why? (Explaing why would make a good answer.)
– usr2564301
Nov 9 at 22:19
Your
push dx
may be executed many more times than you are popping it (only once). Best make sure you pop everything that you push. For this toy program it won't matter -- unkess you are going to try larger values next -- but it's a good general rule to keep in mind for anything larger than this.– usr2564301
Nov 9 at 22:21
Your
push dx
may be executed many more times than you are popping it (only once). Best make sure you pop everything that you push. For this toy program it won't matter -- unkess you are going to try larger values next -- but it's a good general rule to keep in mind for anything larger than this.– usr2564301
Nov 9 at 22:21
Adding
mov dx,0
to the loop made the div work properly! But I just realized that by doing pop into [binary].. isn't giving me the right result either..!– falsekein
Nov 9 at 22:32
Adding
mov dx,0
to the loop made the div work properly! But I just realized that by doing pop into [binary].. isn't giving me the right result either..!– falsekein
Nov 9 at 22:32
We don't really know what you are trying to do. Note that
dw 14
is already in binary. Maybe you mean a text representation of zeroes and ones?– Jester
Nov 9 at 22:34
We don't really know what you are trying to do. Note that
dw 14
is already in binary. Maybe you mean a text representation of zeroes and ones?– Jester
Nov 9 at 22:34
|
show 9 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53233910%2fassembly-division-giving-weird-result%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
1
You need the
mov dx,0
inside the loop.– Jester
Nov 9 at 22:14
@Jester: why? (Explaing why would make a good answer.)
– usr2564301
Nov 9 at 22:19
Your
push dx
may be executed many more times than you are popping it (only once). Best make sure you pop everything that you push. For this toy program it won't matter -- unkess you are going to try larger values next -- but it's a good general rule to keep in mind for anything larger than this.– usr2564301
Nov 9 at 22:21
Adding
mov dx,0
to the loop made the div work properly! But I just realized that by doing pop into [binary].. isn't giving me the right result either..!– falsekein
Nov 9 at 22:32
We don't really know what you are trying to do. Note that
dw 14
is already in binary. Maybe you mean a text representation of zeroes and ones?– Jester
Nov 9 at 22:34