-1

so I'm saving an integer in one of my registers that is between 0 and 9, I'm trying to replace this value in the register with it's binary representation.

I was thinking using a shift method to do it but I'm not quite sure,

could anyone point me in the right direction?

Thanks

Belgin Fish
  • 19,187
  • 41
  • 102
  • 131
  • 1
    you mean a literal string of ascii `0` and `1` characters? because what's IN the register is already the binary representation of the number. – Marc B Nov 25 '13 at 17:51
  • Yeah sorry, that's what I mean. – Belgin Fish Nov 25 '13 at 17:54
  • 1
    You are heading the wrong way. You should treat anything in a register as just a value and it is up to you how you interpret it. It may be unicode code point of some character, it may be ascii character, it may be unsigned value, it may be signed value. It is up to you how you interpret it. 'A' can be treated as 65(dec) or as 0x41 (hex) or 1000001 binary. It is matter of presentation. – Artur Nov 25 '13 at 17:57

3 Answers3

2

You cannot replace a value in a register by its binary representation because it's already in binary.

If you want to get the individual bits of your number so you can print them, for example, you can just shift and test:

;AX holds your number. x86 architecture. MS DOS.
          mov cx,16
          or ax,ax
GetBit:   js BitOne     ;We test for SIGN bit here (MSb)
          push ax
          mov ax,0e30h  ;putchar ('0')
          int 10h
          pop ax
          jmp EndBit
BitOne:   push ax
          mov ax,0e31h  ;putchar ('1')
          int 10h
EndBit:   shl ax,1      ;shift left
          loop GetBit
          ret
mcleod_ideafix
  • 11,128
  • 2
  • 24
  • 32
  • Yeah, looking over the question again, i'm thinking i may have misunderstood what it was really asking. +1, despite the 16-bit example (which won't even run in a 64-bit OS, outside of a VM). – cHao Nov 25 '13 at 18:24
  • You don't need to branch; you can get `'0'` or `'1'` from `'0' + bit`. Also, `shl bx, 1` puts the bit shifted out into CF, where you can `mov al, '0'` / `adc al, 0`. (Using a separate register lets you avoid push/pop inside the loop). – Peter Cordes Jul 22 '22 at 17:42
  • [Creating an x86 assembler program that converts an integer to a 16-bit binary string of 0's and 1's](https://stackoverflow.com/a/40811986) shows a loop that uses `shl` and `adc` to create `'0'` or `'1'` ASCII characters, storing them to an array. – Peter Cordes Jul 22 '22 at 17:50
1

ASCII, and by extension nearly every other text encoding in common use, uses the codes 48 (0x30) through 57 (0x39) to represent digits 0-9. If you subtract 48 from the character code, you get the digit value...and if you add 48 to the digit value, you get the character code.

(You can usually use '0' as an alias for 48, and it usually makes the intention clearer.)

You could also and the code with 0x0f to get the digit value, if you're absolutely sure it represents a digit. oring with 0x30 will turn a number from 0-9 back into a character code. Subtraction and addition have the semantic benefit of symmetry, though; you use the same value, just a little differently, to convert both ways.

(If you're not sure your register contains a value between 0 and 9 inclusive, by the way, basically any math to convert it to an ASCII digit will mess up. Similarly, converting to a digit value will mess up for any character codes that don't represent an ASCII digit. Know that the value is in the right range before "converting" it, or you will see wackiness.)

cHao
  • 84,970
  • 20
  • 145
  • 172
0

Well, if you have '0' is your register and want to get 0 (binary zero), you only need to subtract '0' from the register: sub reg,'0', where reg is the register of your choice. sub reg,48 or sub reg,0x30 should work too.

Example 1:

mov al,'0' ; al contains ASCII value '0': 48 in decimal, 0x30 in hexadecimal.
sub al,'0' ; subtract 48 decimal or 0x30 hexadecimal. result: al contains 0.

Example 2:

mov al,'9' ; al contains ASCII value '9': 57 in decimal, 0x39 in hexadecimal.
sub al,'0' ; subtract 48 decimal or 0x30 hexadecimal. result: al contains 9.
nrz
  • 10,435
  • 4
  • 39
  • 71