0

The way I've been trying to convert the value is by a division/remainder loop(divide by 2/16/8, record the remainder to the right spot in an array, loop back with the new value, repeat until you display the array at the end), but I was wondering if there is a simpler way to do it.

My professor told me "Don't worry about it if it works", but that's not quite what I was hoping for. Through googling I've seen plenty of examples that don't use the division, but the code wasn't commented so I wasn't able to learn from it.

Is there a simpler way to do this for someone fairly new to MASM or should I just listen to my professor and let it be?

Algorn120
  • 21
  • 4
  • 1
    Yes it's easy to convert to bases that are powers of two since computers work in binary. You just shift and mask the appropriate number of bits. Obviously for binary you just process 1 bit every time, for hex 4 and octal 3. – Jester Oct 12 '17 at 01:50
  • Alright, thanks! I haven't used masking before but I'm fairly confident on shift. I'm going to read up on masking and try it out. Would you be okay with me asking for clarification later if I get stuck or should I make a whole new question? – Algorn120 Oct 12 '17 at 01:52
  • Don't get confused. A `mask` is nothing but another variable that holds a value whose bits in memory provided the wanted pattern of `1`s and `0`s to do something you need (e.g. turn on and off the bits). – David C. Rankin Oct 12 '17 at 01:55
  • @DavidC.Rankin I'm looking at a section in my textbook that briefly describes a mask but I'm not sure how to choose it. The book's example assumes that they're copying something from AL to a device that resets when it's bits are 0 or 3 in the control byte, so they use 11110110b as the mask. How do I know at which bits a device resets? Also I'm still fairly new to this site and know that there are some strict commenting rules, so if I'm breaking any by asking a new question here please let me know and I'll fix it. – Algorn120 Oct 12 '17 at 02:02
  • Well, that kind of the trick... What you would do in your case is take a conversion from decimal to hex, do it with the division/remainder method, and as @Jester indicated, look at what bits get flipped in your intermediate conversion. e.g. If you have `1111` before one step and it always `0101` after, then you know your mask could be `1010` for an XOR with the original to get the wanted result. So the `mask` could be just an `unsigned char` variable holding `1010` (which is `10` decimal or `0xa` hex) If you could apply that 4-bytes at a time, then `mask` could be `0xaaaa`. – David C. Rankin Oct 12 '17 at 02:08
  • 3
    Have a look at [C: Convert from decimal to hexadecimal using bitwise operations](https://stackoverflow.com/questions/9759140/c-convert-from-decimal-to-hexadecimal-using-bitwise-operations) and pages like [Decimal to Hexadecimal using Bitwise, Bit Shift Operations in Java](http://eddmann.com/posts/decimal-to-hexadecimal-using-bitwise-bit-shift-operations-in-java/) The principles will be the same. – David C. Rankin Oct 12 '17 at 02:17
  • @Algorn120: the delete-comment button is all the way to the right, after your name and the time. It shows up on mouseover. (And BTW, if your input is a string of decimal digits, you have to multiply + add to get a binary integer before you can chop it up into 3 or 4 bit chunks (octal or hex). Changing the low decimal digit can affect all binary bits of an integer. – Peter Cordes Oct 12 '17 at 02:23
  • Thanks! This helped a ton. Is there a way to mark a comment that answered my question? – Algorn120 Oct 12 '17 at 02:26
  • No, answers are supposed to be posted as answers. Or if it was just the link to the other questions that helped, we can close this as a duplicate of those. – Peter Cordes Oct 12 '17 at 02:26
  • 1
    @PeterCordes Yeah I didn't know that the same principles applied from one language to another, but that makes sense since it's just a numbering system. His link was enough to help, feel free to close this as a duplicate – Algorn120 Oct 12 '17 at 02:30
  • I found some other duplicates, including some that have decent-looking x86 asm loops. The first duplicate has a loop that puts characters into a buffer, so it can make only a single print system call (much more efficient than printing characters one at a time). For hex, handling 0-9 vs. a-f an be done with a table lookup, a branch, or a branchless conditional add (`cmovcc`). Octal is of course easy. – Peter Cordes Oct 12 '17 at 02:42
  • And yeah, usually the answer to "how do I do X in assembly???" is "the same as you would in C". :P If you're stuck, or curious, you can write a small function in C and look at optimized compiler output to see what tricks the compiler comes up with. Try it on http://gcc.godbolt.org/. – Peter Cordes Oct 12 '17 at 02:44
  • BTW, you are not converting decimal value, but binary value. If you do use Irvine's `call ReadDec`, it will return in `eax` 32 bit value (the inputted string encoding decimal value from user was already converted by the library). Having 32 bit value in register is natively binary, as it's stored in bits, and bit can have only 0/1 value. It's not even practical to do any calculation with x86 computer with anything else than binary values (also floating point use powers of two encoding too, just more tricky one, with group of bits for sign/exponent/mantissa, still the information is in *bits*). – Ped7g Oct 12 '17 at 07:49

0 Answers0