1

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]
falsekein
  • 11
  • 1
  • 1
    You need the `mov dx,0` **inside** the loop. – Jester Nov 09 '18 at 22:14
  • @Jester: why? (Explaing why would make a good answer.) – Jongware Nov 09 '18 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. – Jongware Nov 09 '18 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 09 '18 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 09 '18 at 22:34
  • Sorry for my awful english: I'm trying to convert an integer in base 10 to base 2, via arithmetic operations – falsekein Nov 09 '18 at 22:37
  • Your assembler already did that conversion. `14` is stored in binary. – Jester Nov 09 '18 at 22:48
  • Not in my emulator. It's stored in decimal. And even if it were stored in binary, I'm supposed to do this via arithmetic operations – falsekein Nov 09 '18 at 23:01
  • It is binary. What you can do is convert it to a sequence of separate digits. Consult whatever assignment you have for the exact expected output format. – Jester Nov 09 '18 at 23:04
  • I'm supposed to write a program converts a decimal number into a binary number. Thank you for your assistance – falsekein Nov 09 '18 at 23:17
  • "receive an integer and convert it to binary" normally means to input an ascii string that represents an integer number, and then converting that string into a binary number, which uses a multiply instead of a divide. – rcgldr Nov 10 '18 at 00:14
  • 1. [`mov dx, 0` is not the best way to set it to 0](https://stackoverflow.com/q/33666617/995714). 2. `mov cx, 2; div cx` is the worst way to divide by 2. Use `shr cx, 1` instead. 3. Use [`test ax, ax` instead of `cmp ax,0`](https://stackoverflow.com/q/33721204/995714) – phuclv Nov 10 '18 at 00:59
  • *"Not in my emulator. It's stored in decimal."* that's not possible, computer memory can store only bits (0 or 1), so it's stored in binary. What you see in emulator is formatted debug output, so the emulator does format the binary value back into decimal or hexa for you, but on the HW level the value stored is encoded in bits and it has no other information along it, so 14 is stored as eight bits: 0000_1110 (the `dw` does produce 16 bits, so add eight more containing only zeroes). Any decimal/hexa/etc.. formatting is done by the code using that value, but the value itself is only 8 bits. – Ped7g Nov 10 '18 at 06:03
  • Thank you all for your input! I'm a huge noob at assembly. I feel like I'm capable of doing this now – falsekein Nov 10 '18 at 10:55

0 Answers0